Java:如何处理servlet中的多个会话

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11415166/
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 05:00:46  来源:igfitidea点击:

Java : How to deal with multiple sessions in the servlet

javasessionjakarta-eeservletssession-cookies

提问by peter

I am new to Java EE. I have a site which requires a user to log in, and after the user logs in I would like the user to see his/her own item (e.g: shopping cart).

我是 Java EE 的新手。我有一个需要用户登录的网站,在用户登录后,我希望用户看到他/她自己的项目(例如:购物车)。

So that means I have to use a session to accomplish that. But how do I deal with multiple sessions?

所以这意味着我必须使用会话来完成它。但是我如何处理多个会话?

For example multiple users login to the system as well as to the same servlet? However, can I keep multiple sessions in one servlet? Do I keep a record of each of them? How does it work?

例如,多个用户登录到系统以及同一个 servlet?但是,我可以在一个 servlet 中保留多个会话吗?我是否对他们每个人都进行了记录?它是如何工作的?

Can someone give an example please ?

有人可以举个例子吗?

回答by Tomasz Nurkiewicz

In servlet you have access to HttpServletRequestwhich provides you with a getSession()method. This methods returns session object (essentialy a key-value map), different for each user.

在 servlet 中,您可以访问HttpServletRequest它为您提供getSession()方法。此方法返回会话对象(本质上是键值映射),每个用户都不同。

If you put something into that map (like shopping cart), you can retrieve it later - but only when the same user accesses the application again (not necessarily the same servlet).

如果您将某些内容放入该地图(如购物车),您可以稍后检索它 - 但只有在同一用户再次访问该应用程序时(不一定是同一个 servlet)。

public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws IOException, ServletException {

    HttpSession session = request.getSession();
    session.getAttribute("cart");
    //...
    session.setAttribute("cart", whateverYouWant);

Sessions are maintained in the servlet container and looked up by session id (typically a cookie). You don't have to implement anything yourself.

会话在 servlet 容器中维护并通过会话 ID(通常是 cookie)查找。您不必自己实现任何东西。

回答by Ian McLaird

Yes you can. The servlet container will keep track of them for you, so you shouldn't have to do that bookkeeping yourself. The Sessionobject can be obtained from the HttpServletRequestin your servlet code. Since your code only has to concern itself with a single request at a time, there's generally not much pain in dealing with multiple sessions.

是的你可以。servlet 容器将为您跟踪它们,因此您不必自己进行簿记。该Session对象可以从HttpServletRequest您的 servlet 代码中获得。由于您的代码一次只需要关注一个请求,因此处理多个会话通常不会有太大的痛苦。

回答by Dhruvam Gupta

To deal with multiple users login in Servlets, create a session for each login as

为了处理 Servlet 中的多个用户登录,为每个登录创建一个会话作为

HttpSession session = request.getSession();

public HttpSession getSession()returns the current session associated with this request, or if the request does not have a session, creates one.

public HttpSession getSession()返回与此请求关联的当前会话,或者如果请求没有会话,则创建一个。

Each session will identify the users uniquely.

每个会话将唯一标识用户。

Now if user wants to change some data that is in databasethen after creating session, set attribute with that session with the information which can uniquely identify the user in database, for e.g. email_id as:

现在,如果用户想要更改数据库中的某些数据,则在创建会话后,使用可以唯一标识数据库中用户的信息为该会话设置属性,例如 email_id 为:

session.setAttribute("id",email_id of user);

This attribute can be retrieved later in another Servlet/JSP as:

此属性可以稍后在另一个 Servlet/JSP 中检索为:

String email_id = session.getAttribute("id");

This attribute will help in identifying the user who has sent request to server to update data in database or to do something else. For more methods related to the session, refer the link: http://www.javatpoint.com/http-session-in-session-tracking

此属性将有助于识别向服务器发送请求以更新数据库中的数据或执行其他操作的用户。更多session相关方法请参考链接:http: //www.javatpoint.com/http-session-in-session-tracking