简体   繁体   English

jsp中的response.sendRedirect错误

[英]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. 我是Java的新手/业余程序员,并且正在编写一些代码来帮助我向广泛的电影收藏数据库中添加记录,进行搜索和更新。 I've already written the code for adding and searching records using JSPs and it works fine. 我已经编写了使用JSP添加和搜索记录的代码,并且工作正常。 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: 我的JSP中出现以下错误,该错误似乎是在引用我使用的response.sendRedirect()方法:


org.apache.jasper.JasperException: An exception occurred processing JSP page /updateRecord.jsp at line 63 org.apache.jasper.JasperException:在第63行处理JSP页面/updateRecord.jsp时发生异常

63: response.sendRedirect("updaterecordsuccess.html"); 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. 问题是,在另一个JSP中,我使用了与sql更新字符串基本相同的代码,并且工作正常。 The full code for the JSP page giving the error is below. 给出错误的JSP页面的完整代码如下。 The response.sendRedirect method is in the last line of the code. response.sendRedirect方法位于代码的最后一行。 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. 无论如何,谢谢您的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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