The life cycle of servletcontext,session,request

Source: Internet
Author: User
Tags sessions server memory

1. ServletContext: When each Web application is started (configured in a Web container), The Web container will produce an instance that implements the ServletContext interface (or an instance object that implements the ServletContext interface in the servlet engine), the servlet engine instantiates the servlet, Each servlet implements the Servlet interface and the ServletConfig interface, and ServletConfig allows the servlet to accept ServletContext. ServletContext will die when the service is stopped.

2, the session cookie in the COOKIE/SESSION:JSP page "session" Here does not refer to server-side sessions, but refers to the browser, so the session cookie life cycle is not the same as the session of the server side, but with the The browser is the same, that is, as long as there is a session Cookie open, the browser closes it expires. But the value of the session Cookie is the same as the SessionId on the server side. This means that the session cookie is created with the creation of server-side sessions, but does not expire with the expiration of the conversation, but is destroyed as the client-side browser closes. As soon as the session cookie appears, the Server-side sessions are created. When the browser sends a request to the server for the first time, the Serlet engine produces the request, and when the request is submitted to the servlet, the session is generated by Httpservletrequest.getsession (true).

/////////////////////

"Cookie Introduction"

1. The server can write content to the client

2. Can only be text content, but also has the size limit, generally is 4K

3. The client can prevent the server from writing

4. Things that can only be written WebApp

5.Cookie is divided into two types:

! belongs to a window/child window (in the browser corresponding memory, with the browser survival)

! belongs to the text (life cycle)

6. A servlet/jsp set of cookies can be read by a servlet/jsp under the same path or under a subpath (path =url; path!= real file path)

7. How to create cookies? (created on the server side, deposited to the client)

Cookie Cook = new Cookie (String name,string value);

Cook.setmaxage (30);//existence time, unit: seconds, if not set, then do not save

8. How to add cookies to the client.

Response.addcookie (Cook);

9. How to read the client's cookies to the server side.

cookie[] cookies = request.getcookies ();

10. Delete the client's cookies on the server side.

cookie[] cookies = request.getcookies ();

Cookies[i].setmaxage (0);/set lifetime to 0, that is, delete

"Session Introduction"

1. A piece of memory of the server (save Key-value)

2. Corresponding to the client window (including its child window)

3. The client and server side have corresponding SessionID

4. Two ways for clients to send SessionID to the server side:

。 Cookies (Memory Cookies)

。 Rewrite URL

5. Browsers prohibit cookies, you can not use session (using cookies to implement the session)

6. If you want to use the session securely, regardless of whether the client prohibits cookies, you can only use the URL rewrite method.

7. How to get the session

HttpSession HS = Request.getsession (TRUE);

8. Add attributes to Session

Hs.setattribute (String name,object value);

9. Get a property from session

String name = Hs.getattribute (string name);

10. Delete a property from session

Hs.removeattribute (String name);

"Cookie VS Session comparison"

1.Cookie is saved on the client, session is saved on the server side,

Security:cookie<session;

The 2.Cookie lifecycle is cumulative, starting at the time of creation and not affected by the host machine; the session lifecycle is interval, and the life cycle is complete without access to the set value, and the main body machine will make the session life cycle end;

3.Cookie through the network in the client and server-side transmission, while the session save on the server side do not need to transfer, but need to save a SessionID browser;

#ServletContext用法

1. On the server side, all clients accessing the server share

2. Get ServletContext:this.getServletContext (); (This-->servlet Subclass)

3. Add attributes: setattribute (String name,object obj);

4. Get attributes: (Object) getattribute (String name);

5. Delete attribute: RemoveAttribute (String name);

6. Life cycle: The attributes in ServletContext from the start, to the end of the server shutdown, in view of their long-term presence in the server memory, generally do not put too much data into it;

/////////////////////

3. Request:

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.