简体   繁体   中英

response.sendRedirect Error in jsp

I'm a newbie/hobbyist programmer in Java and I'm writing some code to help me add records to, search, and update my extensive movie collection database. I've already written the code for adding and searching records using JSPs and it works fine. However, I'm running into an issue with the code to update a record. I'm getting the following error in my JSP, which seems to be referencing the response.sendRedirect() method I used:


org.apache.jasper.JasperException: An exception occurred processing JSP page /updateRecord.jsp at line 63

63: response.sendRedirect("updaterecordsuccess.html");


The thing is that I used basically the same code, except for a sql update string, in another JSP and it works fine. The full code for the JSP page giving the error is below. The response.sendRedirect method is in the last line of the code. I think I've checked everything, but am unable to figure it out.

 <%@ page contentType="text/html; charset=UTF-8" language="java" import="java.sql.*"    import="java.text.ParseException" import="java.text.SimpleDateFormat" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<%
  Connection conn = null;

  Class.forName("com.mysql.jdbc.Driver").newInstance();
  conn = DriverManager.getConnection("jdbc:mysql://localhost/VideoDB?  user=user&password=password");

  PreparedStatement psUpdateRecord=null;
  String sqlUpdateRecord=null;


  String title=request.getParameter("title");
  String sDateVwd=request.getParameter("sDateVwd");
  int rating=Integer.parseInt(request.getParameter("rating"));
  String comments=request.getParameter("comments");

  try {

        java.util.Date utilDateVwd = new SimpleDateFormat("dd MMM   yyyy").parse(sDateVwd);
        java.sql.Date sqlDateVwd = new java.sql.Date(utilDateVwd.getTime());

    try {

     sqlUpdateRecord="UPDATE vidtb SET date_vwd = ?, rating = ?, comments = ? WHERE  title = ?";
     psUpdateRecord=conn.prepareStatement(sqlUpdateRecord);
     psUpdateRecord.setDate(1,sqlDateVwd);
     psUpdateRecord.setInt(2,rating);
     psUpdateRecord.setString(3,comments);
     psUpdateRecord.setString(4,title);       
     psUpdateRecord.executeUpdate();

      } finally {
    }

  } 
  catch (ParseException e) {
    e.printStackTrace();
  }

  catch(Exception e)
  {
      response.sendRedirect("rateRecord.jsp");//// On error it will send back to   rateRecord.jsp page
  }

    try{
      if(psUpdateRecord!=null)
      {
       psUpdateRecord.close();
      }

      if(conn!=null)
      {
       conn.close();
      }
    }
    catch(Exception e)
    {
      e.printStackTrace(); 
    }

response.sendRedirect("updaterecordsuccess.html");

%>

My bad! I was casually working on another computer at home and decided to try to do an update to a record in the database, and it worked. I guess the browser on my other computer was caching the bad results. So all's good with the code. Thanks for the suggestions anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM