Login Servlet Requires Two Tries

126 views Asked by At

(preface: total java nube)

I have an new web application with an /index.jsp which is going through a Login servlet and then forwarded onto a /WEB-INF/index.jsp if everything checks out during the Login servlet.

Servlet:

import java.io.*;
import com.<co>.<app>.User;
import java.sql.SQLException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.ServletException;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "Login", urlPatterns = { "/Login" })
public class Login extends HttpServlet {

    public Login() { super(); }
    private static String userid = null;
    private static String passwd = null;
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        userid = request.getParameter("userid");
        passwd = request.getParameter("passwd");
        if (passwd.length()==0){ passwd = null; }
        else { passwd.trim(); }

        if (userid != null && passwd != null && passwd.length() != 0)
        {
            System.out.print("Going to see if "+userid+" can authenticate"+"\n");
            int added = 0;
            boolean existinguser = User.ExistingUser(userid);
            boolean authenticated = false;
            try {
                authenticated = User.AuthenticateUser(userid, passwd);
            } catch (NamingException e) {
                response.sendRedirect("/<app>/?error");
            }
            System.out.print("Is "+userid+" an existing user? "+existinguser+"\n");
            System.out.print(userid+" authenticated equals "+authenticated+"\n");
            if (existinguser == true)
            {
                System.out.print("passed the existing IF \n");
                if (authenticated == true)
                {                   
                    System.out.print("passed the authenticated IF \n");
                    String user = User.GetUserID(userid);
                    String role = User.GetUserRole(userid);
                    Cookie userCookie = new Cookie("user",user);
                    Cookie roleCookie = new Cookie("role", role);
                    userCookie.setMaxAge(30*60);
                    roleCookie.setMaxAge(30*60);
                    response.addCookie(userCookie);
                    response.addCookie(roleCookie);
                    System.out.print("just added the cookies \n");
                    RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/index.jsp");
                    System.out.print("setup the forward \n");
                    dispatcher.forward(request, response);
                    System.out.print("WHY THE !@#$ AM I LOGGING THIS??? \n");
                }
                else
                {
                    response.sendRedirect("/<app>/?error");
                }
            }
            else
            {
                try {
                    added = User.AddToApplication(userid);
                } catch (SQLException e) {
                    response.sendRedirect("/<app>/?contactsupport");
                }
                if (added == 1)
                {
                    response.sendRedirect("/<app>/?error");
                }
                else
                {
                    response.sendRedirect("/<app>/?error");
                }
            }
        }
        else
        {
            response.sendRedirect("/<app>/?error");
        }
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

Here is the full console log:

Going to see if <user> can authenticate
Is <user> an existing user? true
<user> authenticated equals true
passed the existing IF 
passed the authenticated IF 
just added the cookies 
setup the forward.. goodbye! 
WHY THE HELL AM I LOGGING THIS???

For some reason, the first time the user tries to log in it logs all of this information but doesn't end up forwarding to the dashboard home page. The second time the user tries to log in, it again logs all of this to the console output, including the WHY THE HELL AM I LOGGING THIS??? line but does redirect to the dashboard home page.

What gives? Please help? More code can be posted if necessary.

EDIT:
I figured out what is causing it to force me to try twice.. in the header of each dashboard JSP page is the following:

<c:choose>
    <c:when test="${cookie.role.value==null}">
        <c:redirect url="${baseURL}/?what" />
    </c:when>
    <c:otherwise></c:otherwise>
</c:choose>

However, I have moved everything within the dashboard to be pre-processed by a controller which is verifying that the user is still a user and they are still logged in. After setting up the Login servlet to forward to the dashboard's home controller.. Everything worked as planned.

0

There are 0 answers