-1
嗨我是Servlets新手可能很容易的問題。在我將數據傳遞到表單並單擊提交按鈕後,我得到java.lang.NullPointerException
LogOut.doPost(LogOut.java:45)
任何想法如何通過此異常?Java Servlet nullPointerException登錄註銷會話
代碼的doGet:
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
PrintWriter out = response.getWriter();
response.setContentType("text/html");
HttpSession session = request.getSession(false);
out.println("<form method='post' action='LogOut'>"
+ "usr <input type='text' id='username'/>"
+ "pass <input type='password'id='password'/>"
+ "<input type='submit' value='Login'></form>");
//If the username and password is correct
if (session != null) {
String username = (String) session.getAttribute("username");
out.println("Hi " + username + "sessioon is started with id " + session.getId());
out.println("<a id='logout' value='Logout' />");
}
}
代碼的doPost
protected void doPost(HttpServletRequest request, HttpServletResponse response) {
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
String password = request.getParameter("password");
response.setContentType("text/html");
if (username.equals("usr") && password.equals("pass")) { //line 45
HttpSession sess = request.getSession();
sess.setAttribute("username", username);
} else {
out.println("Wrong input data");
}
}
哪條線是45號線? – deKajoo 2014-09-28 14:35:01
in the doPost method theres // line 45 – user3024888 2014-09-28 14:37:46
檢查request.getParameter(「username」);'或'request.getParameter(「password」);'不返回null – deKajoo 2014-09-28 14:39:21