会话丢失并在每个 servlet 请求中创建为新的 [英] Session is lost and created as new in every servlet request

查看:20
本文介绍了会话丢失并在每个 servlet 请求中创建为新的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个大问题.每次我向服务器发出新请求时,我的当前会话都会消失.

I have this big issue. My current session is gone every time I made a new request to Server.

我检查了很多地方.我找不到问题所在.我也包括web.xml 中的 session-config 在 tomcat 和应用程序中.我还允许我的浏览器接受 cookie.在每个浏览器中测试.它不工作.

I have checked in a lot of places. I can't find what's the problem. I also have included session-config in web.xml both in tomcat and application. I also enabled to accept cookies to my browsers. Tested in every browser. It's not working.

我只是在使用 JSP/Servlet 开发一个简单的 java ee 应用程序.我只有在服务器机器上部署到 tomcat 后才面临这个问题.

I am just developing a simple java ee applcation using JSP/Servlet. I am facing the problem only after I have deployed to tomcat in server machine.

推荐答案

多年后,我再也没有在这里发布答案.当时我很忙,忘记了这个问题.但是,今天我像往常一样在 Stackoverflow 中寻找解决方案,看到这个通知提到我从这个问题中得到了分数.似乎其他开发人员也面临同样的问题.所以,我试着回忆我是如何解决这个问题的.是的,我通过手动放回会话 ID 来跟踪/维护会话 ID 解决了这个问题.

After years, I never posted the answer back here. At that time I was busy and forgot about this question. But, today I am looking for a solution in Stackoverflow as usual and saw this notification mentioning I am getting points from this Question. Seems like other developers are facing the same issue. So, I tried to recall how I solved the issue. And yes, I solved by manually put back the session id to track/maintain the session id.

请看我手动将 jsessionid 放回 servlet 中的代码.

Please see the code that I manually put back jsessionid inside the servlet.

HttpSession session = request.getSession();
if (request.getParameter("JSESSIONID") != null) {
    Cookie userCookie = new Cookie("JSESSIONID", request.getParameter("JSESSIONID"));
    response.addCookie(userCookie);
} else {
    String sessionId = session.getId();
    Cookie userCookie = new Cookie("JSESSIONID", sessionId);
    response.addCookie(userCookie);
}

这篇关于会话丢失并在每个 servlet 请求中创建为新的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆