问题描述
- 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.
解决方案
如果你输入一个错误的Id那么你的managementlogin就为null , 那么你的managementlogin.getName()就报错相当于null.getName()当然会报错
解决方案二:
managementlogin = managementServicelogin.getoneManagementbyID(loginuserid);
if(managementlogin!=null&&!(managementlogin.getName() == null) ){
先判断对象是否为空,然后再调对象的属性
时间: 2024-10-01 06:17:58