HttpSession in the Java EE

Source: Internet
Author: User
Tags server memory

HttpSession Summary of the Java EE: ① What is a session? Session is a server-side technology, using this technology, the server at runtime can create a shared session object for each browser, because the session for the user browser exclusive, so when the browser access to the server's Web resources,                    Can put their own data in the session, when the user to access other Web resources on the server, the other Web resources from the user's own session to remove data for the user Service. How is the ②session created? When a user opens a browser and accesses a site, the server will find out if the session object has been created in the server's memory, and if it has been created, it will be found in the Web server using the SessionID value. If you do not have a Web server will assign a unique session to the browser, set a SessionID identity for this session, SessionID contains the browser information and the session is part of the Web server under the Web application, The SessionID is then encapsulated in a response object to respond to the client, the client stores the value in a cookie, and the client sends the cookie under that Web app to the Web server when the request is made. When the session is called, a cookie is passed to find the corresponding session. It is important to note that the session is an object that the client and the server share data, although the session is stored in the Web server memory and is bound to a browser. However, if you have more than one Web site under a Web server, the default is to create a different session object from a different Web site under a Web server that is accessed by a single browser. How a server implements a browser that corresponds to a session under a Web application such as:

The life cycle of ③session is 30 minutes by default this can be seen in the Web. xml file by default time of its configuration, or it can be configured to set the session lifecycle. Web server settings The default life cycle of the session

Of course, if you modify the Web server, XML will take effect for the lifetime of the session created by all Web applications under all Web servers. If you modify Web. XML, it will take effect for the lifetime of the session created by the current web App. If it conflicts then the priority set under the Web App is higher. Of course, there are two functions that can affect the session life cycle Setinactiveinterval (): Specifies the time, in seconds, between client requests before the servlet Container'll invalidate this session. Refers to the specific time parameter is that setting the servlet container will invalidate the session before the client requests. This is the time to set the session lazy, in this time the session is not accessed will be invalidated, if it is accessed then reset the time value again into the lazy state. Invalidate (): Invalidates this session then unbinds any objects bound to it. Invalidates the session and invalidates the objects bound in the session. ④session is a domain object whose data mechanism is equivalent to a map and is stored in the form of a key-value pair. You can store any object objects in the session. This value is overwritten when the duplicate value of the key name is also stored in the session. ⑤ above we have said that the session is stored on the server side, session in a session in effect, then we close the browser, the server in the browser associated with the session is automatically destroyed it? Of course not, because the session is stored on the Web server side, when the browser is closed, the browser will not send a request to the Web server, so the session is still in the end of the life cycle, still exist in the memory of the Web server. That is to say, when the session life cycle is not over, we close the browser and open the browser to the Web application under the Web server, the session still exists. So how do we get this session that was previously exclusive to my browser? is very simple, because the session and browser binding is a Jsessionid cookie, That is, we create a cookie with a cookie key named Jsessionid the first time we call the session and set the life cycle to and SessIon, and then responds to the browser through the response object, and the browser does the storage so that every time the browser sends the request it will bring this cookie. This will enable us to close the browser and then open the browser, you can still access the previous browser exclusive session. There is a need to understand the knowledge about cookies. If you don't understand, you can look at the summary of the cookie I wrote earlier: http://blog.csdn.NET/nihaowoshiyudong/article/details/53535183 Java code is as follows: ① created session and stored Jsessionid cookie [Java] View plain copy package com.yd.servlet;    Import java.io.IOException ;  Import javax.servlet.servletexception;  Import javax.servlet.annotation.webservlet;  Import javax.servlet.http.cookie;  Import javax.servlet.http.httpservlet;  Import javax.servlet.http.httpservletrequest;  Import javax.servlet.http.httpservletresponse;  Import javax.servlet.http.httpsession;   /**  * servlet implementation class CreateSession  */  @ Webservlet ("/createsession")   public class CreateSession extends HttpServlet {      private Static final Long Seriawww.saiche55.cnlVersionUID = 1l;     www.gouyiflb.cn      &nbSp    /**      * @see httpservlet#httpservlet ()  www.gouyifl.cn     */      public createsession () {          super ();    www.senta7.net      //TODO auto-generated constructor stub   & nbsp; }       /**      * @see httpservlet#doget (httpservletrequest Request, HttpServletResponse response)      */      protected void doget ( HttpServletRequest request, HttpServletResponse response) throws Servletexception, IOException {           request.setcharacterencoding ("Utf-8");          //Create the session          HttpSession session= When you get the session for the first time Request.getsession ();          <span style= "cOlor: #FF0000; " >session.setattribute ("name", "TomCat");</span>          < Span style= "color: #FF0000;" >//Storage Jsessionid This cookie makes it the same as the life cycle of the session           cookies cookie=new Cookie ("Jsessionid", Session.getid ());         //Setting Cookie lifecycle           cookie.setmaxage (30*60);           Response.addcookie (cookie);</span>         // Response.sendredirect ("/sessiontest/getsessionservlet");     }        /**      * @see httpservlet#dopost (httpservletrequest request, httpservletresponse response )      */      protected void DoPost (HttpServletRequest request, HttpServletResponse response) throws Servletexception, www.yxin7.com IOException {      www.chuangshi88.cn   /TODO auto-generated method stub           doget (Request, response);     }   }  Browser-accepted response headers

② Close the browser ③ open the browser again to send the request under the same Web application resource, the Java code is as follows: [Java] View plain copy package com.yd.servlet;    Import java.io.ioexception;  Import javax.servlet.servletexception;  Import Javax.servlet.annotation.WebServlet ;  Import javax.servlet.http.httpservlet;  Import javax.servlet.http.httpservletrequest;  Import javax.servlet.http.httpservletresponse;  Import javax.servlet.http.httpsession;   /**  * Servlet Implementation Class Getsessionservlet  */  @WebServlet ("/getsessionservlet")   public class Getsessionservlet extends HttpServlet {      private static final long Serialversionuid = 1l;  &N bsp;    www.boyuanyl.cn       /**      * @ See Httpservlet#httpservlet ()   www.ycyc66.cn/   */      Public Getsessionservlet () {          super ();         //TODO auto-generated constructor stub     }        /**      * @see httpservlet#doget (httpservletrequest request, httpservletresponse response)      */      protected void doget (HttpServletRequest request, HttpServletResponse response) throws Servletexception, IOException {          //Get Properties in session           request.setcharacterencoding ("Utf-8");       www.yunfeizao.cn    HttpSession session=request.getsession ();          <span style= "color: #FF0000;" >string name= (String) session.getattribute ("name");          SYSTEM.OUT.PRINTLN ("session content displayed after browser is closed:" +name+ "id value" +session.getid ());          //Display results   &NBSP;&NBSP;&Nbsp;    //The session content that is displayed after the browser is closed: TomCat ID value 8b49f6cbd34bfddee43ba58912fa65d9</span>     }       /**      * @see httpservlet#dopost ( HttpServletRequest request, HttpServletResponse response)      */      protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {& nbsp        /TODO auto-generated method stub           doget (Request, response);     }   }  requests information header as follows:

The Jsessionid of the request information header and the Jsessionid value of the response information header are visible. So through the Jsessionid can find the browser exclusive session, whether it is a session, as long as the session life cycle is not over, you can access to the session. Top 1 Step

HttpSession in the Java EE

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.