使用Tomcat 7,Java MVC未设置会话变量

时间:2014-09-02 11:52:42

标签: java ajax apache session tomcat

当我在Eclipse Kepler和本地Apache Tomcat 7服务器上开发此网站时,我能够设置会话变量以确定用户是否已登录到该网站。

将网站部署到我的远程服务器后,在提供正确的登录详细信息后,不再设置会话变量!

我的远程服务器运行Ubuntu服务器12.04,Apache Tomcat 7。

我使用AJAX表单提交登录详细信息以进行检查,如果它们是正确的,我会使用Javascript重定向到管理页面。

这是对Controller中会话变量的检查。 AJAX调用检查"成功"在转发到管理页面之前,如果我不检查会话变量,该页面有效,那就是我知道会话变量没有设置。

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.");
        }
    }
}

AJAX电话:

$.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日志文件:

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

为什么我的远程服务器上没有设置会话变量?

修改

我意识到它与登录页面的URL有关。

目前我已经在端口80上注册了指向我的服务器的域名(我使用虚拟主机),这是通过mod_jk转发到Tomcat7。

我需要的网站登录页面的网址是www.hostname.co.uk/page/login。使用此URL登录失败。

如果我在服务器上使用URL作为绝对文件位置:hostformyserver.co.uk:8080/Driving-Instructor-Gary/page/login。我能够成功登录。

为什么这会影响登录过程?

1 个答案:

答案 0 :(得分:0)

我不确定这个答案:

试试这个:

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