gpt4 book ai didi

java - 无法使用Tomcat 7,Java MVC设置 session 变量

转载 作者:行者123 更新时间:2023-11-28 23:34:40 25 4
gpt4 key购买 nike

当我在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]“ 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



为什么未在远程服务器上设置会话变量?

编辑:

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

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

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

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

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

最佳答案

我不确定这个答案:

尝试一下:

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

关于java - 无法使用Tomcat 7,Java MVC设置 session 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25622776/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com