0
votes

Whilst I was developing this website in Eclipse Kepler and a local Apache Tomcat 7 server I was able to set a session variable to determine if a user was logged in to the website.

After deploying the website to my remote server the session variable is no longer being set after providing the correct login details!

My remote server runs Ubuntu server 12.04, Apache Tomcat 7.

I use an AJAX form to submit the login details for checking and if they are correct I redirect to the admin page using Javascript.

This is the check on the session variable in the Controller. The AJAX call checks for "success" before forwarding to the admin page, which works if I don't check the session variable, that's how I know the session variable isn't getting set.

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String form         = request.getParameter("form");
    // check login details
    if(form.equals("loginForm")){
        String username = request.getParameter("username").trim();
        String password = request.getParameter("password").trim();

        password = loginService.hashPassword(password);
        boolean isValidUser = loginService.checkUser(username, password);

        if(isValidUser){
            // set session
            HttpSession session = request.getSession();

            session.setAttribute("loggedIn", "true");
            if(session.getAttribute("loggedIn") != null){
                out.print("success");
            }

        }else{
            out.print("Incorrect login details.");
        }
    }
}

The AJAX call:

$.ajax(
    {
        type: "POST",
        url : "HomeController",
        data : postData,
        success: function(data) 
        {
            if(data == "success"){
                window.location.href = "page/admin";
            }
        },
        error: function(jqXHR, textStatus, errorThrown) 
        {
            $("#loginResult").html("<p>ss"+errorThrown+textStatus+jqXHR+"</p>");
            }
        });
}else{
    $("#loginResult").html("<p>Unable to login: ensure details are correct.</p>");
}

Tomcat log file:

    • [02/Sep/2014:12:37:21 +0100] "GET /Driving-Instructor-Gary/page/admin HTTP/1.1" 200 -
    • [02/Sep/2014:12:37:24 +0100] "GET /Driving-Instructor-Gary/page/login HTTP/1.1" 200 7959
    • [02/Sep/2014:12:37:24 +0100] "GET /Driving-Instructor-Gary/css/main.css HTTP/1.1" 304 -
    • [02/Sep/2014:12:37:31 +0100] "POST /Driving-Instructor-Gary/HomeController HTTP/1.1" 200 11
    • [02/Sep/2014:12:37:32 +0100] "POST /Driving-Instructor-Gary/HomeController HTTP/1.1" 200 11
    • [02/Sep/2014:12:37:32 +0100] "POST /Driving-Instructor-Gary/HomeController HTTP/1.1" 200 11
    • [02/Sep/2014:12:37:32 +0100] "POST /Driving-Instructor-Gary/HomeController HTTP/1.1" 200 11
    • [02/Sep/2014:12:37:33 +0100] "POST /Driving-Instructor-Gary/HomeController HTTP/1.1" 200 11
    • [02/Sep/2014:12:37:33 +0100] "POST /Driving-Instructor-Gary/HomeController HTTP/1.1" 200 11
    • [02/Sep/2014:12:37:33 +0100] "POST /Driving-Instructor-Gary/HomeController HTTP/1.1" 200 11
    • [02/Sep/2014:12:37:33 +0100] "POST /Driving-Instructor-Gary/HomeController HTTP/1.1" 200 11
    • [02/Sep/2014:12:37:33 +0100] "POST /Driving-Instructor-Gary/HomeController HTTP/1.1" 200 11
    • [02/Sep/2014:12:37:33 +0100] "POST /Driving-Instructor-Gary/HomeController HTTP/1.1" 200 11
    • [02/Sep/2014:12:38:03 +0100] "GET /Driving-Instructor-Gary/page/login HTTP/1.1" 200 7959
    • [02/Sep/2014:12:38:03 +0100] "GET /Driving-Instructor-Gary/js/bootstrap.min.js HTTP/1.1" 304 -
    • [02/Sep/2014:12:38:03 +0100] "GET /Driving-Instructor-Gary/js/jquery.js HTTP/1.1" 304 -
    • [02/Sep/2014:12:38:03 +0100] "GET /Driving-Instructor-Gary/css/main.css HTTP/1.1" 304 -
    • [02/Sep/2014:12:38:03 +0100] "GET /Driving-Instructor-Gary/css/bootstrap.min.css HTTP/1.1" 304 -
    • [02/Sep/2014:12:38:04 +0100] "GET /Driving-Instructor-Gary/img/favicon-16.png HTTP/1.1" 304 -
    • [02/Sep/2014:12:38:11 +0100] "POST /Driving-Instructor-Gary/HomeController HTTP/1.1" 200 11
    • [02/Sep/2014:12:38:12 +0100] "POST /Driving-Instructor-Gary/HomeController HTTP/1.1" 200 11
    • [02/Sep/2014:12:38:12 +0100] "POST /Driving-Instructor-Gary/HomeController HTTP/1.1" 200 11
      • [02/Sep/2014:12:38:12 +0100] "POST /Driving-Instructor-Gary/HomeController HTTP/1.1" 200 11
    • [02/Sep/2014:12:38:12 +0100] "POST /Driving-Instructor-Gary/HomeController HTTP/1.1" 200 11
      • [02/Sep/2014:12:38:13 +0100] "POST /Driving-Instructor-Gary/HomeController HTTP/1.1" 200 11
    • [02/Sep/2014:12:38:13 +0100] "POST /Driving-Instructor-Gary/HomeController HTTP/1.1" 200 11
    • [02/Sep/2014:12:38:13 +0100] "POST /Driving-Instructor-Gary/HomeController HTTP/1.1" 200 11
    • [02/Sep/2014:12:38:14 +0100] "POST /Driving-Instructor-Gary/HomeController HTTP/1.1" 200 11
    • [02/Sep/2014:12:38:14 +0100] "POST /Driving-Instructor-Gary/HomeController HTTP/1.1" 200 11
    • [02/Sep/2014:12:38:14 +0100] "POST /Driving-Instructor-Gary/HomeController HTTP/1.1" 200 11
    • [02/Sep/2014:12:38:14 +0100] "POST /Driving-Instructor-Gary/HomeController HTTP/1.1" 200 11
    • [02/Sep/2014:12:38:15 +0100] "POST /Driving-Instructor-Gary/HomeController HTTP/1.1" 200 11
    • [02/Sep/2014:12:38:15 +0100] "POST /Driving-Instructor-Gary/HomeController HTTP/1.1" 200 11
    • [02/Sep/2014:12:38:15 +0100] "POST /Driving-Instructor-Gary/HomeController HTTP/1.1" 200 11
    • [02/Sep/2014:12:38:15 +0100] "POST /Driving-Instructor-Gary/HomeController HTTP/1.1" 200 11
    • [02/Sep/2014:12:38:16 +0100] "POST /Driving-Instructor-Gary/HomeController HTTP/1.1" 200 11
    • [02/Sep/2014:12:38:16 +0100] "POST /Driving-Instructor-Gary/HomeController HTTP/1.1" 200 11
    • [02/Sep/2014:12:38:16 +0100] "POST /Driving-Instructor-Gary/HomeController HTTP/1.1" 200 11
    • [02/Sep/2014:12:38:17 +0100] "POST /Driving-Instructor-Gary/HomeController HTTP/1.1" 200 11
    • [02/Sep/2014:12:38:17 +0100] "POST /Driving-Instructor-Gary/HomeController HTTP/1.1" 200 11
    • [02/Sep/2014:12:38:17 +0100] "POST /Driving-Instructor-Gary/HomeController HTTP/1.1" 200 11
    • [02/Sep/2014:12:38:17 +0100] "POST /Driving-Instructor-Gary/HomeController HTTP/1.1" 200 11
    • [02/Sep/2014:12:38:17 +0100] "POST /Driving-Instructor-Gary/HomeController HTTP/1.1" 200 11
    • [02/Sep/2014:12:38:18 +0100] "POST /Driving-Instructor-Gary/HomeController HTTP/1.1" 200 11

Why is the session variable not getting set on my remote server?

EDIT:

I have realized it has something to do with the URL of the login page.

Currently I have registered a domain name that points to my server on port 80 (I use virtual hosting), this is forwarded to Tomcat7 via mod_jk.

The url for the login page that I need for the website is www.hostname.co.uk/page/login. Login fails using this URL.

If I use the URL for the absolute file location on my server: hostformyserver.co.uk:8080/Driving-Instructor-Gary/page/login. I am able to login successfully.

Why is this effecting the login process?

1
the window.location.href = "page/admin"; won't work, you need to point it to your server url, something like: myserver.com/page/admin - fmodos

1 Answers

0
votes

I am not sure about this answer:

Just try this:

     String logString=(String)session.getAttribute("loggedIn");
     if(logString.equals("true")){
                out.print("success");
            }