java 写的登录验证问题

public class LoginCheck extends HttpServlet {

/**
 * Constructor of the object.
 */
public LoginCheck() {
    super();
}


public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setCharacterEncoding("UTF-8");
    request.setCharacterEncoding("utf-8");
    response.setContentType("text/html; charset=utf-8");
    PrintWriter out = response.getWriter();
    String userid = request.getParameter("userid");
    String password = request.getParameter("password");
    System.out.println(userid+password+"====================");

    if(request.getSession().getAttribute("loginUser")!=null){
        request.getRequestDispatcher("/index.html").forward(request, response);
    }else{
        Integer loginuserid=Integer.parseInt(userid);
        Management managementlogin = null ;
        ManagementService managementServicelogin = new ManagementService();
        managementlogin = managementServicelogin.getoneManagementbyID(loginuserid);
        if(!(managementlogin.getName() == null) ){
            String Chechpwd = managementlogin.getPwd();


             if(Chechpwd.equals(password)){
                    request.getSession().setAttribute("loginUser", managementlogin);
                    request.getRequestDispatcher("/index.html").forward(request, response);
                }else{
                    request.getRequestDispatcher("/login.jsp").forward(request, response);
                }



        }else{
            request.getRequestDispatcher("/login.jsp").forward(request, response);
        }
    }



}


public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    this.doGet(request, response);


}

}

 当输入正确的用户ID和密码时候,成功登录,当输入一个错误的用户id时候报错:type Exception report

message 

description The server encountered an internal error that prevented it from fulfilling this request.

exception 

java.lang.NullPointerException
    com.jclg.control.LoginCheck.doGet(LoginCheck.java:41)
    com.jclg.control.LoginCheck.doPost(LoginCheck.java:66)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:643)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:723)


note The full stack trace of the root cause is available in the Apache Tomcat/6.0.43 logs.


2个回答

如果你输入一个错误的Id那么你的managementlogin就为null , 那么你的managementlogin.getName()就报错相当于null.getName()当然会报错

 managementlogin = managementServicelogin.getoneManagementbyID(loginuserid);
        if(managementlogin!=null&&!(managementlogin.getName() == null) ){

先判断对象是否为空,然后再调对象的属性

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问