我有以下JSP文件和Servlet文件
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>ABC Corporation</title>
<h1>Terminal Login</h1>
</head>
<body>
<form name="login" action="/WebAccount/LoginServlet?" method="post" />
Username: <input type="text" name="username" value=""/>
Password: <input type="text" name="password" value=""/>
<input type="submit" value="LOGIN"/>
Not User? Register Here: <input type="submit" action="/WebAccount/register.jsp" value="REGISTER">
</body>
</html>
Servlet代码:
class LoginServlet extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, SQLException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String username = request.getParameter("Username").toString();
String password = request.getParameter("Password").toString();
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/account";
Connection conn = DriverManager.getConnection(url, "root", "school");
Statement statement = (Statement) conn.createStatement();
ResultSet rs = statement.executeQuery("SELECT * from Users where username='" + username + "' and password='" + password + "';");
String user;
String pass;
while (rs.next()) {
user = rs.getString(username).toString();
pass = rs.getString(password).toString();
if (username.equals(user) && password.equals(pass)) {
response.sendRedirect("http://www.google.com");
conn.close();
}
}
if (!rs.next()) {
out.println("Login failed");
conn.close();
}
} catch (SQLException ex) {
throw new ServletException("Cannot Connect", ex);
} catch (ClassNotFoundException ex) {
throw new ServletException("Login failed", ex);
} finally {
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (SQLException ex) {
Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (SQLException ex) {
Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
我在处理servlet时收到此错误:
显示java.lang.NullPointerException
LoginServlet.processRequest(LoginServlet.java:38)
LoginServlet.doPost(LoginServlet.java:104)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
有任何错误吗?
答案 0 :(得分:0)
究竟在&#34; LoginServlet.java,第38行&#34;?它看起来像&#34; out&#34;可能是null?
如果问题确实存在于回应中,那么#34;作家&#34;对象,这里有一个可能的解释:
http://www.coderanch.com/t/484052/Tomcat/Tomcat-Returning-NullPointer-upon-closing
谢谢问题是response.getWriter()对象被声明为 在servlet中是全局的,因此它在所有线程中都很常见。如果一个 线程关闭它,它用于在其他线程中给出NullPtrException。
答案 1 :(得分:0)
显示java.lang.NullPointerException
在LoginServlet.processRequest(LoginServlet.java:38)
只需阅读跟踪的第1行即可。在LoginServlet
方法内,processRequest()
类的第38行的某些内容为null
,而您的代码却没有预料到它。
好吧,我会试试我的魔法球。您声明了2个输入字段如下:
Username: <input type="text" name="username" value=""/>
Password: <input type="text" name="password" value=""/>
因此,参数名称分别为username
和password
。
但是,您尝试按如下方式访问它:
String username = request.getParameter("Username").toString();
String password = request.getParameter("Password").toString();
请注意大写的第1个字符!这不匹配。 getParameter()
会在此处返回null
,而toString()
上的(不必要的btw)null
调用将导致NullPointerEXception
。
相应修复:
String username = request.getParameter("username");
String password = request.getParameter("password");
有关使用servlet的更多提示,您可能会发现我们的servlets wiki page也很有用。
答案 2 :(得分:0)
错误消息告诉您究竟在哪一行引发异常。阅读错误消息。它包含有用的信息。
我可以通过检查此行将导致异常的代码告诉您,因为输入字段名为username
(小写u),并且您读取(因此为空)参数Username
(大写U):
String username = request.getParameter("Username").toString();
顺便说一句,您为什么要在toString()
上致电String
?