开发者社区> 问答> 正文

jsp实现登陆字符串问题

想实现登陆功能,并检验用户密码是否正确。用户名和密码都为数字或字母时可以登陆,但当用户名为数字和字母的组合,密码为数字时,报空指针异常,相关代码如下
(这是一个论坛的登陆,登陆成功后跳转到消息界面)
表单代码

 <form action ="loginCheck.jsp" method ="post">
  
  <table >
  <tr>
  <td>用户名</td>
  <td><input type = "text" name = "username" /></td>
  
  </tr>
   <tr>
  <td>密&nbsp码&nbsp</td>
  <td> <input type = "password" name = "password" /></td>
  
  
  </table>
  
  <input type = "submit" value = "登陆"/>
   <a href = "register.jsp">  <input type = "button" value = "注册"/>  </a>
  </form>

JSP代码

 <%
    String username = (String)request.getParameter("username");
    String password = (String)request.getParameter("password");

    User user = DAOFactory.getUserDAOInstance().findByName(username);

    try{
   if(password.equals(user.getPassword())){
    out.print("success");
    session.setAttribute("username", username);
    response.sendRedirect("forum.jsp");
    }else{
    out.print("error");
    out.print(user.getPassword());//测试使用
    }   
    }catch(Exception e){
    e.printStackTrace();
    } 
     %>

报错信息

java.lang.NullPointerException
    at org.apache.jsp.loginCheck_jsp._jspService(loginCheck_jsp.java:105)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)

    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2522)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2511)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

展开
收起
蛮大人123 2016-03-10 18:51:28 2281 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪

    首先你两个都是String类型,所以不存在数字与字母组合就有空指针。从你的代码中没看到dao的实现,不好说,可能是数据库中没有查到有username的值,给了个空。你的判断也不是很严谨,如果不填密码这些也会出现空指针异常

    2019-07-17 18:58:10
    赞同 展开评论 打赏
问答标签:
问答地址:
问答排行榜
最热
最新

相关课程

更多

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载