SQL异常不会重定向到.jsp页面

时间:2017-05-09 17:51:04

标签: javascript java jsp

在我的catch块中由于某种原因没有将输出错误消息重定向回我的jsp页面。默认的tomcats异常报告显示为HTTP状态500.错误消息在控制台窗口中正确显示。但它没有在jsp页面上显示错误消息,如下所述。我错过了什么?

register.jsp:

<p><font color="#ff0000"><c:out value="${status}"/></font></p>
<form action="<%=context%>/RegisterServlet" method='post'>
...
...
...

RegisterServlet:

   try (Statement st = con.createStatement()) {
            System.out.println(sql);
            st.executeUpdate(sql);

           } catch(SQLException sqle ) {
                System.out.println("Unable to register user: "+sqle.getMessage());
                //Check if we are getting duplicate key exception on userName
                if(sqle.getMessage().indexOf("Duplicate entry")!=-1) {
                    System.out.println("User already exists");
                    strUserMsg = "User name "+request.getParameter("userid")+" already " +
                            "exists. Please try another user name.";
                } else { //If other SQLException than dup key exception
                    strUserMsg = "Unable to register user "+request.getParameter("userid")+
                    ". Please try again later.";
                }
                setRequestAttributes(request);
                request.setAttribute("status", strUserMsg);
                rd.forward(request, response);
                response.sendRedirect("register.jsp");

            } catch(Exception e) {//If it goes into Exception other than SQLException
                System.out.println("Unable to register user: "+e.getMessage());
                strUserMsg = "Unable to register user "+request.getParameter("userid")
                +". Please try again later.";
                setRequestAttributes(request);
                request.setAttribute("status", strUserMsg);
                rd.forward(request, response);
                response.sendRedirect("register.jsp");

            }

下面的堆栈跟踪:

exception

org.apache.jasper.JasperException: java.lang.NumberFormatException: For input string: ""
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:555)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:476)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.focus.servlets.RegisterServlet.doPost(RegisterServlet.java:113)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

root cause


Unable to register user: Duplicate entry 'test_user' for key 'PRIMARY'
User already exists
May 09, 2017 1:45:40 PM org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.NumberFormatException: For input string: ""
   java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
java.lang.Integer.parseInt(Integer.java:592)
java.lang.Integer.parseInt(Integer.java:615)
org.apache.jsp.common.register_jsp._jspService(register_jsp.java:227)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.focus.servlets.RegisterServlet.doPost(RegisterServlet.java:113)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

2 个答案:

答案 0 :(得分:0)

我不太确定我完全理解,但是如果你抓住正在发生的异常,该异常的默认处理程序(在jsp上显示)将被覆盖,应用程序将继续运行。

答案 1 :(得分:0)

好的,所以我明白了。我的代码中的其他地方出现了错误。完全不同于我刚发布的内容。