url 重定向时的会话变量
我有一个在 tomcat 服务器上运行的应用程序,该应用程序被重定向到另一个网站进行密码身份验证(并存储一些数据),然后重定向回我的 tomcat 服务器
EX
1 到 A
A 到 B
B 至 2
其中 1 和 2 是我的申请中的页面
A 和 B 是其他应用程序上的页面
我在页面 1 上设置会话变量
HttpSession session = request.getSession(true);
session.setAttribute("loginUser", "loginUser");
,并在页面 2 上使用它,
String loginUser= session.getAttribute("loginUser");
但在 2 上出现空指针异常
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您在同一上下文(应用程序)中执行重定向,请使用
RequestDispatcher
执行forward
(传递您的request
和>响应
)。这样,您就可以完成会话。否则,我的建议是不要将
loginUser
存储在会话上,而是将用户 id 变量作为一些智能(且混乱)字符串传递给其他应用程序,编写一种机制来检索登录会话状态的用户,并从那里继续。这称为单点登录。If you're doing a redirect in the same context (Application), rather use
RequestDispatcher
to do aforward
(passing yourrequest
andresponse
). That way, you pass your session through.Otherwise, my suggestion is to not store the
loginUser
on the session but pass user id variable as some intelligent (and confused) string to the other application, write a mechanism to retrieve the user logged in session state and carry on from there. This is called, Single-Sign On.发生这种情况可能是因为
“会话是暂时的,当出现以下情况之一时,其生命周期就会结束:
请参阅http://docs.oracle.com/cd/E15523_01/web.1111/e13712/sessions.htm
it may be happening because of
'A session is transient, and its lifetime ends when one of the following occurs:
refer http://docs.oracle.com/cd/E15523_01/web.1111/e13712/sessions.htm