会话丢失并在每个servlet请求中创建为新的 [英] Session is lost and created as new in every servlet request
问题描述
我有这个大问题。每当我向服务器发出新请求时,我当前的会话都会消失。
I have this big issue. My current session is gone every time I made a new request to Server.
我已经检查了很多地方。我找不到问题所在。我还在tomcat和application中的web.xml中包含了
session-config。我还启用了接受我的浏览器的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 applcation。我在服务器机器中部署到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.
请参阅我在servlet中手动放回jsessionid的代码。
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屋!