基于Struts的应用程序中的会话问题
我正在做一个多用户登录应用程序,其中使用会话来存储用户对象。
HttpSession session = request.getSession(true);
session.setAttribute("user",user);
在每个页面中,我都使用 JSTL 检查用户对象是否存在于会话中。
<c:choose>
<c:when test="${not empty sessionScope.user}">
//jsp code
</c:when>
<c:otherwise>
<logic:redirect forward="welcome"/>
</c:otherwise>
</c:choose>
我的问题是,如果用户单击应用程序中的 href 链接,用户将更改为会话中的上一个用户。即它正在从缓存加载用户。如果我刷新页面,它将加载正确的用户。
我该如何解决它?
I am doing a multi user login application where I use session to store the user object.
HttpSession session = request.getSession(true);
session.setAttribute("user",user);
In every page I am checking using JSTL whether the user object is present in the session.
<c:choose>
<c:when test="${not empty sessionScope.user}">
//jsp code
</c:when>
<c:otherwise>
<logic:redirect forward="welcome"/>
</c:otherwise>
</c:choose>
My problem is that if the user clicks on a href link in the application the user changes to Previous user in the session. i.e. the it is loading the user from cache. If I refresh the page it will load the correct user.
How could I fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,Session存储在服务器上,与客户端缓存无关。
以前的用户不是从缓存加载的(如您所说),页面是从缓存加载的,因为链接通过 GET 转到服务器。如果 URL 自上次请求以来没有更改,则浏览器可以自由缓存 GET 结果。
要解决此问题,您需要使客户端缓存无效。您可以通过添加 HTTP 缓存标头指令 或 通过在页面中添加元标记。
我建议使用 HTTP 标头,因为代理通常不会读取页面的内容,并且即使客户端未将页面存储在客户端缓存中,您最终也可能会在代理服务器上得到缓存。
First of all, session is stored on server and has nothing to do with client cache.
Previous user is not loaded from cache (as you say), the page is loaded from cache since links go to the server on a GET. The browser is free to cache the result of a GET if the URL didn't change since the last time it was requested.
To fix this, you need to invalidate the client cache. You can do that by adding HTTP cache header directives or by adding meta tags in the page.
I would suggest to go with HTTP headers since proxies usually don't read the content of the page and you may end up with a cache on a proxy server even if the client does not store the page in the client cache.