Error while processing the servlet
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, HttpServl开发者_如何学PythonetResponse 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?
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) parameter Username
(uppercase U):
String username = request.getParameter("Username").toString();
BTW, why are you calling toString()
on a String
?
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:
http://www.coderanch.com/t/484052/Tomcat/Tomcat-Returning-NullPointer-upon-closing
Thanks The problem was response.getWriter() object was declared as global in servlet, so it became common to all threads. and if one thread closes it, it used to give NullPtrException in other threads.
java.lang.NullPointerException
at LoginServlet.processRequest(LoginServlet.java:38)
Just read the 1st line of the trace. Something at line 38 of LoginServlet
class, inside the processRequest()
method, is null
while your code didn't expect it to be.
Well, I'll try my magic ball. You declared the 2 input fields as follows:
Username: <input type="text" name="username" value=""/>
Password: <input type="text" name="password" value=""/>
So, with the parameter names username
and password
respectively.
However, you're attempting to access it as follows:
String username = request.getParameter("Username").toString();
String password = request.getParameter("Password").toString();
Note the uppercased 1st character! This does not match. The getParameter()
would return null
here and the (unnecessary btw) toString()
call on null
will cause a NullPointerEXception
.
Fix it accordingly:
String username = request.getParameter("username");
String password = request.getParameter("password");
For some more hints about using servlets, you may find our servlets wiki page useful as well.
精彩评论