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?
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?