0

i am creating a jsp page that does the following:
select record marks from database
enable editing of each record
saving the edits of record

everything works well until i click on the save button to save edits.
it saves well but unable to refresh the page to show the new updates made in the table.
it shows this error: java.lang.IllegalStateException:Cannot call sendRedirect() after the response has been committed.
please what is causing the error when i press on the save button.
the full contents of the jsp page is below:

<html>
    <head>
        <title>stage 2 - arts</title>
        <link rel="stylesheet" href="mycss/primary_subject_style.css" type="text/css"/>
    </head>
    <body>
        <center><h5>CREATIVE ARTS</h5></center>
        <div class="container">
            <div class="panel">
                <div class="for_table_head">
                    <table class="table-head">
                        <thead>
                            <tr>
                                <th><input type="hidden" name="id"></th>
                                <th style="width: 90px;">name</th>
                                <th>class</th>
                                <th>exam</th>
                                <th>total</th>
                                <th class="text-center">action</th>
                            </tr>
                        </thead>
                    </table>
                    </div>
                         <div class="for_table">
                             <table class="table">
                        <tbody>
                            <%
                             connector co=new connector();
                             Connection con=null;ResultSet rs=null; PreparedStatement pst=null;
                             con=connector.db_connect();
                             String data_search=null;
                             //str_active=request.getParameter("s_stream");
                             data_search ="select ID,Name,Creative_Arts,Creative_Arts_Exam,Creative_Arts_Total from class2 order by Name ASC";
                             String sql=data_search;
                             pst=con.prepareStatement(sql);
                             rs=pst.executeQuery();
                             while(rs.next()){
                            %>
                            <tr>
                                <td><input type="hidden" name="s_id" value='<%=rs.getString("ID")%>'/></td>
                                <td style="width: 100px;"><%=rs.getString("Name")%></td>
                                <td><%=rs.getString("Creative_Arts")%></td>
                                <td style="color: #3c78c9;"><%=rs.getString("Creative_Arts_Exam")%></td>
                                <td style="color: #047907;"><%=rs.getString("Creative_Arts_Total")%></td>
                                <td>
                                    <form action="stage2_arts.jsp" method="post" > 
                                     
                                    <button style="width:38px;height:20px;background:transparent;border:none;cursor:pointer;color:#d58007;padding-left:0;"
                                    type="submit" name="st_id" value='<%=rs.getString("ID")%>'>edit</button><br/>
                                    </form>
                                </td>
                            </tr>
                            <%
                             }
                            %>
                        </tbody>
                    </table>   
                    </div>
                           //suspected part causing the error :

                           <form action="stage2_arts.jsp" method="post">
                             <%
                             String stud_id=request.getParameter("st_id");
                             String srec="select ID,Name from class2 where ID='"+stud_id+"'";
                             pst=con.prepareStatement(srec);
                             rs=pst.executeQuery();
                             if(rs.next()){
                             %>
                            <div class="entry_box">
                            <div class="student_name">
                            <input type="hidden" name="s_id" value='<%=rs.getString("ID")%>' /><br>
                            <label>student</label><br>
                            <input type="text" name="student" value='<%=rs.getString("Name")%>' /><br>
                            </div>
                            <div class="form-group1"> 
                            <label>class score</label>
                            <input type="text" name="class_score" placeholder="enter class score"/><br>
                            </div>
                            <div class="form-group2">
                            <label>exam score</label>
                            <input type="text" name="exam_score" placeholder="enter exam score"/>
                            </div>
                             </div>
                        
                            
                            <center> <input style="margin-top:10px;"class="save_btn" type="submit" value="save" /></center>
                            </form>
                            <%
                            
                            }

                            %>
            </div>
        </div>
         <%
    String st_id=request.getParameter("s_id");
    String c_score=request.getParameter("class_score");
    String e_score=request.getParameter("exam_score");
    if(st_id!=null && c_score!=null && e_score!=null){
    Basic1_Extension b1e=new Basic1_Extension(); 
//get grading scheme
       
      double ex,exCal,getClass,getExam,setTotal; String convertExam,grade=null,remark=null; 
      //fetch scheme
      double exam_scheme=0;
     try{
    
   String sql2="select exam from score_scheme_table";
    pst=con.prepareStatement(sql2);
   rs=pst.executeQuery();
   if(rs.next()){
   exam_scheme=rs.getDouble("exam");
   }
   }catch(Exception e){
   JOptionPane.showMessageDialog(null,e);
   }finally{
  try{
  rs.close();
  pst.close();
           }
  catch(Exception e){
           }
     }
     //convert exam record to scheme value
    
     DecimalFormat df2 = new DecimalFormat(".#");
     ex = Double.valueOf(e_score);
        exCal = (ex/100)*exam_scheme;
        convertExam=String.valueOf(df2.format(exCal));
        getExam=Double.valueOf(convertExam);
        getClass=Double.valueOf(c_score);
        setTotal = getClass + getExam;
    //check and initialize grades and remarks
    // gr.grade_adaptor(setTotal,grade,remark);
   if(setTotal>=80){
            grade="A";
            remark="ADVANCE";
        }else if(setTotal>=75 && setTotal<=79.9){
            grade="P";
            remark="PROFICIENT";
        }else if(setTotal>=70 && setTotal<=74.9){
            grade="AP";
            remark="APPROACHING PROFICIENT";
        }else if(setTotal>=65 && setTotal<=69.9){
            grade="D";
            remark="DEVELOPING";
        }else if(setTotal>=0.0 && setTotal<=64.9){
            grade="B";
            remark="BEGINNING";
        }
    //convert total double data type to string
    String t_score=String.valueOf(setTotal);
    //update student record for subject

    try{
     String sql1="update class2 set Creative_Arts='"+c_score+"%"+"',Creative_Arts_Exam='"+convertExam+"%"+"',Creative_Arts_Total='"+t_score+"%"+"',Creative_Arts_T='"+t_score+"',Creative_Arts_G='"+grade+"',Creative_Arts_R='"+remark+"' where ID='"+st_id+"'";
     pst=con.prepareStatement(sql1);
     pst.execute();
    b1e.rollnumber();b1e.student_arts_position(); b1e.totalSum();b1e.personal_average();b1e.updateGrandPosition();b1e.class_average();b1e.createComments();
    response.sendRedirect("stage2_arts.jsp");
    }catch(Exception ep){
    JOptionPane.showMessageDialog(null,ep);
    }finally{
  try{
  rs.close();
  pst.close();
           }
  catch(Exception et){
           }
     }
   
     }
    
    %>     
    </body>
</html>

please what is the cause of the error message

Rodney Nart
  • 121
  • 11
  • no please @dnault. – Rodney Nart Jul 29 '20 at 16:17
  • 1
    Don't put Java code in JSPs. It is not the 1990s. Additionally, I am not sure what you expect JOptionPane.showMessageDialog(null,ep); to do in a web browser. – Alan Hay Jul 29 '20 at 16:19
  • Check this answer: https://stackoverflow.com/questions/2123514/java-lang-illegalstateexception-cannot-forward-sendredirect-create-session – Bhuvanwaitz Jul 29 '20 at 16:27
  • @RodneyNart Did you read the linked question and answers? The root cause is the same; you're calling `response.sendRedirect` *after* you've already started sending HTML to the client. By that point, it's too late to send a redirect. – dnault Jul 29 '20 at 16:38
  • @RodneyNart - Actually the Q&A that 'dnault' found does contain the answer to your question. The first part of your JSP writes a bunch of HTML to the response. That *commits* the response. Then at the end you try to call `sendRedirect`. But you can't do that after the response is committed. – Stephen C Jul 29 '20 at 16:39
  • it does. i get the idea. so what changes can i directly do to remove the error. i am very new to jsp – Rodney Nart Jul 29 '20 at 17:41

0 Answers0