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.
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
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.
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.
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>