处理 servlet 时出错
我有以下 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)
有任何错误吗?
I have the following JSP File and Servlet file
<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 Code:
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>
}
I am getting this error while processing the 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)
Any mistakes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
错误消息准确地告诉您异常是在哪一行抛出的。阅读错误消息。它包含有用的信息。
我可以通过检查代码告诉您,此行将导致异常,因为输入字段名为
username
(小写 u),并且您读取(因此为 null)参数Username(大写 U):
顺便说一句,为什么要在
String
上调用toString()
?The error message tells you exactly at which line the exception is thrown. Read the error message. It contains useful information.
I can tell you just by examining the code that this line will cause an exception since the input field is named
username
(lowercase u), and you read the (thus null) parameterUsername
(uppercase U):BTW, why are you calling
toString()
on aString
?那么“LoginServlet.java,第 38 行”到底是什么?看起来“out”可能为空?
如果问题确实出在响应“writer”对象上,则以下是一种可能的解释:
So what exactly is at "LoginServlet.java, line 38"? It looks like "out" might be null?
If the problem is indeed with the response "writer" object, here's one possible explanation:
只需阅读跟踪的第一行。
LoginServlet
类的第 38 行(在processRequest()
方法内)的某些内容为null
,而您的代码并不期望它是这样。好吧,我要试试我的魔法球。您按如下方式声明了 2 个输入字段:
因此,分别使用参数名称
username
和password
。但是,您尝试按如下方式访问它:
请注意大写的第一个字符!这不匹配。
getParameter()
将在此处返回null
,并且对null
的(不必要的)toString()
调用将导致一个 NullPointerEXception。相应地修复它:
有关使用 servlet 的更多提示,您可能会发现我们的 servlets wiki 页面也很有用。
Just read the 1st line of the trace. Something at line 38 of
LoginServlet
class, inside theprocessRequest()
method, isnull
while your code didn't expect it to be.Well, I'll try my magic ball. You declared the 2 input fields as follows:
So, with the parameter names
username
andpassword
respectively.However, you're attempting to access it as follows:
Note the uppercased 1st character! This does not match. The
getParameter()
would returnnull
here and the (unnecessary btw)toString()
call onnull
will cause aNullPointerEXception
.Fix it accordingly:
For some more hints about using servlets, you may find our servlets wiki page useful as well.