Interface of Session
Kill session
Nature of Cookies
The interface of the cookie
Then summarize--cookie, session of past life
Example of a simple custom cookie
1, session of the interface, with some code to understand.
2, kill the session-the meaning of killing is to invalidate the session (in the JVM purged??). )。
- Kill immediately, there are two known ways,
- Set "term" in the code for a specific session
3. Characteristics of Cookies
4, the interface of the cookie
The way to get cookie--from a customer request is a little more modern:
cookie[] cookies = request.getcookies (); if (cookies = null) { for(Cookie cookie:cookies) { if(Cookie.getname () . Equals ("Jsessionid")) { System.out.println ("jsessionid=" +Cookie.getvalue ()); } Cookie.setmaxage (0); Response.addcookie (cookie); } }
5, again summarize--cookie, session of past life
- Cookie before the world this life.
The creation of a cookie: The client sends a request to the server, the server returns response and Set-cookie
In this way, there is a corresponding cookie in the client,
Subsequent cookies: Each time the client sends a request to the server, It will carry this cookie
Essentially, a cookie is a small piece of information exchanged between the client and the server.
- The past life of the session
Session generation: Usually accompanied by a getsession statement //Error here
Follow-up of Session:
- We use session to manage sessions, store information
- We have to go through the jsessionid to find the corresponding session.
- We have to kill the session at the right time.
In essence, a session is an object that exists in the server-side JVM.
6. Simple Custom Cookie Example
Usually, my habit is to write a Hello program first to run through the environment.
<!DOCTYPE HTML><HTML><Head> <MetaCharSet= "Us-ascii"> <title>Home Page</title></Head><Body><formAction= "Cookietest.do"Method= "POST">Username:<inputtype= "text"name= "username"> <inputtype= "Submit"value= "Submit"></form></Body></HTML>
<?XML version= "1.0" encoding= "Iso-8859-1"?><Web-appxmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"version= "2.4"> <servlet> <Servlet-name>CookieTest</Servlet-name> <Servlet-class>Com.example.web.CookieTest</Servlet-class> </servlet> <servlet-mapping> <Servlet-name>CookieTest</Servlet-name> <Url-pattern>/cookietest.do</Url-pattern> </servlet-mapping></Web-app>
PackageCom.example.web;Importjavax.servlet.ServletException;Importjavax.servlet.http.*;Importjava.io.IOException; Public classCookieTestextendsHttpServlet {@Overrideprotected voidDoPost (httpservletrequest req, HttpServletResponse resp)throwsservletexception, IOException {resp.setcontenttype ("Text/html"); Resp.getwriter (). println ("Hello"); }}
After a successful run, start writing code ~
PackageCom.example.web;ImportJavax.servlet.RequestDispatcher;Importjavax.servlet.ServletException;Importjavax.servlet.http.*;Importjava.io.IOException; Public classCookieTestextendsHttpServlet {@Overrideprotected voidDoPost (httpservletrequest req, HttpServletResponse resp)throwsservletexception, IOException {resp.setcontenttype ("Text/html"); String name= Req.getparameter ("username"); Cookie Cookie=NewCookie ("username"), name); Cookie.setmaxage (30*60);//if not set, the cookie's lifetime is defaulted to "Close browser"Resp.addcookie (cookie); RequestDispatcher View= Req.getrequestdispatcher ("result.jsp"); View.forward (req, resp); }}
result.jsp
Checkcookie.do
<?XML version= "1.0" encoding= "Iso-8859-1"?><Web-appxmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"version= "2.4"> <servlet> <Servlet-name>CookieTest</Servlet-name> <Servlet-class>Com.example.web.CookieTest</Servlet-class> </servlet> <servlet-mapping> <Servlet-name>CookieTest</Servlet-name> <Url-pattern>/cookietest.do</Url-pattern> </servlet-mapping> <servlet> <Servlet-name>Checkcookie</Servlet-name> <Servlet-class>Com.example.web.CheckCookie</Servlet-class> </servlet> <servlet-mapping> <Servlet-name>Checkcookie</Servlet-name> <Url-pattern>/checkcookie.do</Url-pattern> </servlet-mapping></Web-app>
PackageCom.example.web;Importjavax.servlet.ServletException;Importjavax.servlet.http.*;Importjava.io.IOException;ImportJava.io.PrintWriter; Public classCheckcookieextendsHttpServlet {@Overrideprotected voidDoget (httpservletrequest req, HttpServletResponse resp)throwsservletexception, IOException {resp.setcontenttype ("Text/html"); PrintWriter PW=Resp.getwriter (); Cookie[] Cookies=req.getcookies (); if(Cookies! =NULL) { for(Cookie cookie:cookies) {if(Cookie.getname (). Equals ("username") {pw.println ("Hello" +Cookie.getvalue ()); Break; } } } }}
"Head first Servlets and JSP" notes 13:session & Cookies