java 如何在另一个 servlet 中检索一个 servlet 会话属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10464486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:09:29  来源:igfitidea点击:

How to retrieve one servlet session attributes in another servlet

javaservlets

提问by nnk

I have two servlets in my project 2nd being called from the 1st servlet. Im creating http session in first servlet and setting some attributes to that session object. How do i get the values of these attributes in second servlet?

我的项目中有两个 servlet,第二个是从第一个 servlet 调用的。我在第一个 servlet 中创建 http 会话并为该会话对象设置一些属性。如何在第二个 servlet 中获取这些属性的值?

Thanks in advance.

提前致谢。

回答by JB Nizet

Object value = request.getSession().getAttribute("nameOfTheAttributeUsedInTheFirstServlet");

If two servlets are in the same webapp, they share the same session context, and the session is the same for both servlets. Of course, remember that a session is tight to a particular user.

如果两个 servlet 位于同一个 webapp 中,则它们共享相同的会话上下文,并且两个 servlet 的会话相同。当然,请记住,会话对特定用户来说很紧张。