JSP 用户名密码传值错误,帮忙看下。。~~articleFlat.jspJava codeform actionchecklogin.jsp methodpo
JSP 用户名密码传值错误,帮忙看下。。~~
articleFlat.jsp
Java code<form action="checklogin.jsp" method="post"> <input type="hidden" name="action" value="login" /> <div class="fastlg cl"> <div class="y pns"> <table cellspacing="0" cellpadding="0"> <tbody> <tr> <td> <label for="username"> 用户名 </label> </td> <td> <input type="text" name="username" /> </td> <td class="fastlg_l"> <label for="ls_cookietime"> <input type="checkbox" name="cookietime" id="ls_cookietime" class="pc" value="2592000" tabindex="903"> 自动登录 </label> </td> </tr> <tr> <td> <label> <label for="password"> 密码 </label> </label> </td> <td> <input type="password" name="password" /> </td> <td class="fastlg_l"> <button type="submit" class="pn pnc vm" tabindex="904"> <em>登录</em> </button> </td> <td class="cl"> <a href="register.jsp" class="reglink xi2 xw1"><span>立即注册</span> </a> </td> </tr> </tbody> </table> <input type="hidden" name="quickforward" value="yes"> <input type="hidden" name="handlekey" value="ls"> </div> </div> </form>
checklogin.jsp
Java code<% String action = request.getParameter("action"); String userId = new String(request.getParameter("username")); String password1 = new String(request.getParameter("password"));System.out.println(action+" "+userId+" "+password1); Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/bbs", "root", "admin"); Statement stmt = con.createStatement(); String sql = "select * from user where user = '" + userId + "';"; System.out.println(sql); ResultSet rs = stmt.executeQuery(sql); if (rs.next()) { String password = new String(rs.getString("password")); if (password.equals(password1)) { session.setAttribute("userId", userId); System.out.println("登陆成功"); response.sendRedirect("complete.jsp"); } else { response.sendRedirect("articleFlat.jsp"); } }else { response.sendRedirect("articleFlat.jsp"); }%>
数据库里存了几个用户名和密码,比方说有一个scott,tiger,输入之后会报空指针错误
2012-8-14 10:44:09 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
at java.lang.String.<init>(String.java:147)
at org.apache.jsp.complete_jsp._jspService(complete_jsp.java:68)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:619)
org.apache.jasper.JasperException: An exception occurred processing JSP page /checklogin.jsp at line 6
4: <%
5: String action = request.getParameter("action");
6: String userId = new String(request.getParameter("username"));
7: String password1 = new String(request.getParameter("password"));
8: System.out.println(action+" "+userId+" "+password1);
9: Class.forName("com.mysql.jdbc.Driver");
可是控制台后面显示action,userId,password1是有值的,并且会显示登陆成功。可是如果是随便输入用户名密码的话,就会直接跳转到articleFlat.jsp页面,这是怎么回事。。。
[解决办法]java.lang.NullPointerException
at java.lang.String.<init>(String.java:147)
at org.apache.jsp.complete_jsp._jspService(complete_jsp.java:68)
查看一下tomcat\work子目录中的complete_jsp.java 的68行是什么代码 ,然后到你的complete.jsp中进行处理 ,因为报空指针啦
[解决办法]由于 response.sendRedirect("complete.jsp");是重定向,重定向方式产生了一个新的请求,经过一次重定向后,request内的对象将无法使用。所以你执行request.getParameter("username"));为空指针异常
[解决办法]由于 response.sendRedirect("complete.jsp");是重定向,重定向方式产生了一个新的请求,经过一次重定向后,request内的对象将无法使用。所以你执行request.getParameter("username"));为空指针异常
我的异常网推荐解决方案:An exception occurred processing JSP page,http://www.myexception.cn/j2se/33144.html