So I'm trying to do a project for a college assignment and I cannot for the life of me getting the login servlet to work. This code was given to us by our lecturer and always seems to work fine for him so when I created the servlet in Netbeans 11.3 (we're also using TomCat 9 as our server, and copied over the code it works but it doesn't go to the right page. What's supposed to happen is that if password and email are empty, it goes back to the login.jsp page, if the admin credentials are inputted it goes to the adminHome.jsp and if it is just a normal user from the database (we're using derby) it should go to shop.jsp page. As it stands, the code will not go past the first if statement - if password and email are empty go to login.jsp page. I'm at a real loss here, I cannot for the life of me get it to work and it is a big part of my project and I need it to run otherwise I'm going to lose a lot of marks.
LoginServlet.java
public class LoginServlet extends HttpServlet implements IConstants {
/**
* 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 {
String email = request.getParameter("email");
String password = request.getParameter("password");
if (StringUtils.isStringEmpty(email) || StringUtils.isStringEmpty(password)) {
RequestDispatcher rd = request.getRequestDispatcher("/login.jsp");
rd.forward(request, response);
System.out.println("This is the first breakpoint");
} else {
UserManager uMgr = new UserManager();
User user = uMgr.loginUser(email, password);
if (user == null) {
RequestDispatcher rd = request.getRequestDispatcher("/login.jsp");
rd.forward(request, response);
System.out.println("This is the second breakpoint");
} else {
request.getSession(true).setAttribute(IConstants.SESSION_KEY_USER, user);
if (user.getUserType().equals(IConstants.USER_TYPE_ADMIN)) {
RequestDispatcher rd = request.getRequestDispatcher("/adminHome.jsp");
rd.forward(request, response);
} else if (user.getUserType().equals(IConstants.USER_TYPE_GENERAL_USER)) {
RequestDispatcher rd = request.getRequestDispatcher("/shop.jsp");
rd.forward(request, response);
} else {
RequestDispatcher rd = request.getRequestDispatcher("/index.html");
rd.forward(request, response);
}
}
}
}
// <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 {
processRequest(request, response);
}
/**
* 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 {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
login.jsp
<div class="container" id="LoginPageContainer">
<!-- Log In -->
<div class="container" id="login">
<div class="login" style="width: 500px; float:left; height:500px; background:#8AA994; margin:20px">
<form class="form-signin" action="LoginServlet" method="POST">
<input type="hidden" name="action" value="add">
<br/>
<br/>
<br/>
<h3 class="form-signin-heading">You need to be logged in to complete your purchase</h3>
<br/>
<h3 class="form-signin-heading"> Please Log In </h3>
<input type="text" class="input-block-level" id="email" placeholder="Email Address">
<br/>
<br/>
<input type="password" class="input-block-level" id="password" placeholder="Password">
<br/>
<label class="checkbox">
<input type="checkbox" value="remember-me"> Remember me
</label>
<br>
<a class="forgot-password" href="#">Forgot Password?</a>
<br/>
<br/>
<input type="submit" id="SignIn" value="Login">
</form>
</div>
</div>
IConstants.java
public interface IConstants {
//User Types
public static final String USER_TYPE_ADMIN = "ADMIN";
public static final String USER_TYPE_GENERAL_USER = "GENUSER";
//Session Keys
public static final String SESSION_KEY_USER = "SKUSER";
public static final String SESSION_KEY_ALL_USERS = "SKALLUSERS";
}
I'm not sure what else there is to include, it's been created on Netbeans 11.3, TomCat 9.0.40 and it's an Ant project. I was convinced for ages that it was a TomCat issue but I've manage to get another sample project he gave us to run, I copied that code and adjusted it to suit my code and no luck, it refuses to go past the line where I've got the first breakpoint in the LoginServlet. Any help at all would be much appreciated!