简体   繁体   English

如何将session.attribute从jsp保留到servlet

[英]How to keep session.attribute from jsp to servlet

I have a Login Servlet where i set the attribute user logged: 我有一个Login Servlet,在其中我设置了属性用户登录:

protected void doPost(HttpServletRequest request, HttpServletResponse response)
                    throws ServletException, IOException 
                              ... OMITTED
    request.getSession().setAttribute("user", user_name);
    request.getRequestDispatcher("index.jsp").forward(request, response);
                              ... OMITTED
    }

From the index.jsp I go to my quit.jsp, until here i can access to the user logged through getAttribute() . 从index.jsp转到quit.jsp,直到这里我可以访问通过getAttribute()登录的用户。 But in my quit.jsp, I ask the user logged just for his password and call the Quit Servlet. 但是,在我的quit.jsp中,我要求用户仅登录以输入密码,然后调用Quit Servlet。 The problem is that in the Quit Servlet when I try to access to the current user logged with getAttribute() , the attribute "user" is null. 问题是当我尝试访问使用getAttribute()登录的当前用户时,在退出Servlet中,属性“ user”为null。 So my question is: how can I use a persistent attribute created in a servlet which will be used in jsp and servlets? 所以我的问题是:如何使用在servlet中创建的将在jsp和servlet中使用的持久属性?

In a JSP file, the HttpServletRequest is available with variable name request by default. JSP文件中,默认情况下, HttpServletRequest可与变量名request使用。 You can use it same way in the JSP file too. 您也可以在JSP文件中使用相同的方式。

So 所以

request.getSession().setAttribute("user", user_name);

will be available in JSP file also. 也将在JSP文件中提供。

<%
request.getSession().setAttribute("user", user_name);
%>

Your problem might not be so easy to solve. 您的问题可能不是那么容易解决。 I have had a similar situation so I can give you some investigation pointers: 我遇到过类似的情况,因此我可以给您一些调查提示:

The cause of your null data is that when your session (where the attribute is stored) dies, times out, is re-created because the same user issues a new Login POST then your custom session data dies with it, unless you store it referenced as a session_id in a persistent storage such as a Cookie on the user browser, or a database for each user: but this can be quite complex to implement. 空数据的原因是,当您的会话(存储属性的会话)死亡时,超时会重新创建,因为同一用户发出新的Login POST,然后您的自定义会话数据会随之死亡,除非您存储了引用的数据作为session_id在持久性存储(例如用户浏览器中的Cookie或每个用户的数据库)中的实现,但这实现起来可能非常复杂。

As a short cut: 作为捷径:

Check out this method documentation in HttpServlet: getThreadLocalRequest().getSession(false); 在HttpServlet中查看此方法文档:getThreadLocalRequest()。getSession(false);

and this as well which might solve your problem: 这也可能解决您的问题:

request.getSession().setMaxInactiveInterval(3600); 。request.getSession()的setMaxInactiveInterval(3600); // Max intervals in seconds between calls prior to invalidate session. //无效会话之前的两次呼叫之间的最大间隔(以秒为单位)。 This means that after one hour you will need to auto-logout your user (or force him to re-login) to prevent a null being encountered. 这意味着一小时后,您将需要自动注销您的用户(或强迫他重新登录),以防止遇到空值。

a logout would call: 注销将调用:

session.removeAttribute("attribute"); session.removeAttribute( “属性”); session.invalidate(); session.invalidate();

Also note that you mention having 2 distinct servlets (defined in Tomcat web.xml ??). 还要注意,您提到有2个不同的servlet(在Tomcat web.xml中定义)。 One is Login the other is Quit, they both respond to POST/GET. 一个是登录,另一个是退出,它们都响应POST / GET。 I suppose they have two different implementations and I think that they do not share the same session object. 我想它们有两个不同的实现,并且我认为它们不共享相同的会话对象。 So in this case you might have to implement the Login + Quit actions in the same servlet with a proper session invalidation time interval. 因此,在这种情况下,您可能必须在具有适当会话无效时间间隔的同一个servlet中实现Login + Quit操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM