Five, ServletContext, session management

ServletContext

What is the Servlet context?

  Also called Servlet context: ServletContext. After starting container, the container for each web application to create an object that implements ServletContext interface requirements, the object known as the servlet context.

  When the WEB server starts, it creates a shared storage area for each WEB application (for each directory under webapps is an application program).

  ServletContext also called "common area", that is, with a WEB application, all of the Servlet and JSP can share the same area.

 

ServletContext Features:

  1, ServletContext lifecycle: WEB created when the server starts, destroyed when the server shuts down.

  2, uniqueness, a web application corresponding to a servlet context

  3, there has been, as long as the container is not closed, the application is not unloaded, servlet context will always exist.

 

How to get Servlet context?

  GenericServlet, ServletConfig, HttpSession, FilterConfig provides a method GetServletContext () to get the Servlet context.

  // current through the object class Servlet give ServletContext object associated with it, i.e. context
      ServletContext application = this.getServletContext();

 

ServletContext context effects:

  1, binding data

    setAttribute, removeAttribute, getAttribute satisfied under the conditions of use, the priority of using a short life cycle (request <session <ServletContext)

  2, the initialization parameter read global

    First, such a configuration in web.xml

    <! - Configure the global initialization parameters ->
    <context-param>
      <param-name>company</param-name>
      <Param-value> YORK thunderbolt game </ param-value>
    </context-param>
 
 
What is the difference (private area) and ServletContext (public areas) of HttpSession that?
1, different scope: HttpSession is for each client a separate browser. 
         ServletContext is there a WEB for each application 
to create HttpSession first accesses the server, the server calls request.getSession (), the access intervals over 30 minutes to destroy: 2, different survival time. 
         ServletContext created when the server is started and destroyed when the server is stopped.
 
 
=======================================================================================================================================
 
Session Management
 
What is a session?
  Browser requests a server, the server receives the request, after the process, give a response, which is a session.
 
What is session management (tracking)?
  In many times during the session browser with the server, the server has to record some information about the client browser, this is called session tracking.
 
The role of the session?
   Because the HTTP protocol is a "stateless" protocol (protocol type disconnected) , i.e. to the server after the client browser response, it will disconnect from the network. Next time the client browser will re-establish a network connection request to the server. In fact, the HTTP protocol is to ensure server performance. And in this case, the default server will not record information about the client's browser.
  So session tracking is used to maintain client and server communication .
 
How session management?
  Cookie: The state saved in the browser, there is all the data each time the client sent a request to the server with requests sent to the server, modify it and then sent back to the client saved this model is called Cookie.
  Session: The state saved on the server, and marked a number for this set of data, only the number sent back to the client. When a client sends a request to the server only needs to send over this number, the server finds the corresponding pattern data for such administration in accordance with this number.
 
=======================================================================================================================================

What is a cookie?

  A small amount of data temporarily stored in the server browser side.

Cookie works:

  When the browser to access the server, the server will send small amounts of data to the browser (using the set-cookie header); browser will preserve data temporarily; data when the browser accesses the server again, it will save the prior transmission to the server (using cookie header)

 

cookie lifetime: By default, the browser cookie will be stored in the memory inside. As long as the browser does not close, cookie would have been saved, close the browser cookie is deleted.

  cookie.setMaxAge(int seconds)

    seconds> 0, the browser cookie will be stored on the hard disk (storage file format), over time the browser cookie is deleted

    seconds <0, which is the default, cookie will be stored in memory. 

    seconds = 0, delete the cookie immediately

 

Coding problem: cookie can only hold a legal ASCII characters, if it is required to convert the Chinese to Chinese legal ASCII string.

  String URLEncoder.encode(String str,String charset)

  String URLDecoder.decode(String str,String charset)

 

cookie restrictions

  • The user may be prohibited
  • You can only save a small amount of data (about 4K)
  • Limit the number of stores (about a few hundred)
  • Only store a string for the Chinese need to be encoded.

=======================================================================================================================================
 
What is the HttpSession?
  Server-side to save the state created a special object.
 
HttpSession principle:
  When the browser accesses the server, the server creates a session object (the object has a unique ID, commonly known as sessionId), the server will sessionId sent to the browser (the default way to send in a cookie), the browser will sessionId preserved (in memory); when the browser accesses the server again, sessionId will be sent to the server, find the corresponding session object based sessionId.
 
How to get HttpSession?
  HttpSession s=request.getSession(boolean flag)
    flag to true, the first request to see them there sessionId, if not, create a session object; if so, according to the sessionId to find the corresponding session object (found on the return, could not find it creates a new session object)
    When the flag is false, the first request to see them there sessionId, if not, does not create a session object; if so, according to the sessionId to find the corresponding session object (find returns, returns can not find null)
    When the flag is empty, HttpSession s = request.getSession () is equivalent to the above argument is true

 

 HttpSession when it was created, when it was destroyed?

Creation: the first request to the server, the server calls the Servlet request.getSession () is created when the method.

Destruction: If the browser interval more than 30 minutes no access to the server, the server will destroy the corresponding HttpSession object. (30 minutes by default is configurable.)

<session-config>
  <session-timeout>30</session-timeout>
</session-config>
  
  
 
 

Guess you like

Origin www.cnblogs.com/danielJinyu/p/11364056.html