0
package Servlet;

import Manager.UserManager;
import Model.User;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import utils.IConstants;
import utils.StringUtils;

public 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 {String email = request.getParameter("email");
    String password = request.getParameter("password");

    if (StringUtils.isStringEmpty(email) || StringUtils.isStringEmpty(password)) {

        RequestDispatcher rd = request.getRequestDispatcher("/index.html");
        rd.forward(request, response);

    } else {

        UserManager uMgr = new UserManager();
        User user = uMgr.loginUser(email, password);
        if (user == null) {
            RequestDispatcher rd = request.getRequestDispatcher("/index.html");
            rd.forward(request, response);
        } 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("/genUserHome.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>

}

When we run the code and we try and login, we keep getting this: server error 500: "java.lang.nullpointerexception"

Were trying to pass the contents of the HTML text fields into a function that searches the db for the email and the password and returns a user class.

If that user class has a type of "admin" the login should take him to the admin page however it doesn't.

All the nullpointexceptions seem to be linked to passing the email and password to the user manager however ive tested setting specific values that match the db to the variable email and password however I am still getting the same error.

Help greatly appreciated.

Output stack from glassfish server: http://pastebin.com/YC6GtviP

Chillah
  • 11
  • 1
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – sinclair Jan 27 '16 at 14:38
  • 1
    What happens on line 34 of `UserManager.java`? It would help if you could add the `loginUser()` method to the question too. – Mike Jan 27 '16 at 15:19

1 Answers1

0

You got lots of SQL errors because the database cannot be found. The underlying problem could be this:

java.io.FileNotFoundException: C:\Users\112419442\Desktop\BootStrapLogin\sql\createdb.txt

Which prevents the creation of the database.

sinclair
  • 2,812
  • 4
  • 24
  • 53
  • Hey thanks for the help, I had initially posted the incorrect output, from a previous iteration. This is our current output. Thanks in advance (new pastebin link) – Chillah Jan 27 '16 at 14:25