I'm trying to redirect from current page (index.jsp) after user logs in to another page. I get no error for this and can't really understand what the problem is. Here is my code of the servlet:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.getWriter().append("Served at: ").append(request.getContextPath());
usernameInput = request.getParameter("username");
passwordInput = request.getParameter("password");
Connection conn = null;
try {
// Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
// Open a connection
conn = DriverManager.getConnection(DB_URL, USER, PASS);
// Execute SQL query
String sql;
sql = "SELECT * FROM `users` where Username=? and Password=?";
PreparedStatement prepStmt = conn.prepareStatement(sql);
prepStmt.setString(1, usernameInput);
prepStmt.setString(2, passwordInput);
ResultSet rs = prepStmt.executeQuery();
if (rs.next()) {
System.out.println("User login is valid in DB");
//Here is where I try to log in. I tryed both the commented and uncommented way to log in but none seems to work.
response.sendRedirect(request.getContextPath() + "resources/main.jsp");
/*RequestDispatcher reqDisp = request.getRequestDispatcher("../WebContent/resources/main.jsp");
reqDisp.forward(request, response);*/
}
} catch (Exception e) {
System.out.println("validateLogon: Error while validating password: " + e.getMessage());
try {
throw e;
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} finally {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I can verify that it enters in the specific section of code since I see in the consele the message printed. If I could guess the problem, it may be I'm not giving the correct path for the main.jsp page. Here is how my project is structured.
Any ideas??Thanks!