Next we will use cookie technology to make a product browsing record case,
Step 1: Create a JSP page to simulate the database and store the list of items
CodeAs follows:
JSP page:
<% @ Page Language =" Java "Contenttype =" Text/html; charsets = UTF-8 "Pageencoding =" UTF-8 "%> <% @ Page import =" Java. util .* "%> <! Doctype HTML public" -// W3C // dtd html 4.01 transitional // en "" Http://www.w3.org/TR/html4/loose.dtd "> <HTML> Content-Type "Content =" Text/html; charsets = UTF-8 "> <Title> insert title here </title> /Myday07/addcookie? Id = 1 "> JAVA basics </a> </LI> <li> <a href =" /Myday07/addcookie? Id = 2 "> Javaweb </a> </LI> <li> <a href =" /Myday07/addcookie? Id = 3 "> DHTML </a> </LI> <li> <a href =" /Myday07/addcookie? Id = 4 "> Database </a> </LI> <li> <a href =" /Myday07/addcookie? Id = 5 "> Struts </a> </LI> <li> <a href =" /Myday07/addcookie? Id = 6 "> Spring </a> </LI> <li> <a href =" /Myday07/addcookie? Id = 7 "> Hibernate </a> </LI> </ul> /Myday07/clearcookie "> Clear data </a> </H5> <% string [] arr = {" Java Basics "," Javaweb "," DHTML "," Database "," Struts "," Spring ","Hibernate "}; // Obtain all cookies Cookie [] cookies = request. getcookies (); // Search for product browsing records in cookies String visitlist = Null ; If (Cookies! = Null ){ For (Cookie: cookies ){ If (Cookie. getname (). Equals (" Visitlist ") {Visitlist = cookie. getvalue (); Break ;}} If (Visitlist =Null ){ // The product list does not exist. Out . Println (" None ");} Else {String [] existids = visitlist. Split (" , "); For (String existid: existids ){ Int Id = integer. parseint (existid ); Out . Println (ARR [ID-1] +" <Br/> ") ;}}%> <HR/> </body>
Step 2: Use the addcookie page to process the browsing record request of the product and write down the access record of the product. The Code is as follows:
Package CN. itcast. Cookie; Import Java. Io. ioexception; Import Javax. servlet. servletexception; Import Javax. servlet. http. Cookie; Import Javax. servlet. http. httpservlet; Import Javax. servlet. http. httpservletrequest; Import Javax. servlet. http. httpservletresponse; Public Class AddcookieExtends Httpservlet { // Obtain the item currently accessed by the user and update it to the item browsing record list Public Void Doget (httpservletrequest request, httpservletresponse response) Throws Servletexception, ioexception { // Automatically refresh Response. setheader (" Refresh "," 2; url =/myday07/products1.jsp "); // 1. Obtain the ID of the product to be viewed by the current user String id = request. getparameter (" ID "); // 2. Obtain the user's browsing record list Cookie [] cookies = request. getcookies (); // Query the access record list name: visitlist ----- 1, 3, 5, 6 String visitlist = Null ; If (Cookies! = Null ){ // Cookie exists For (Cookie: cookies ){ If (Cookie. getname (). Equals (" Visitlist ") {Visitlist = cookie. getvalue (); Break ;}} If (Visitlist = Null ){// The list does not exist. The current item is the first item. Cookie = New Cookie (" Visitlist ", ID); cookie. setmaxage (60*60); response. addcookie (cookie); response. getwriter (). println (" Add success ");} Else { // 3. Determine whether the current product is in the list String [] existids = visitlist. Split (" , "); For (String exsitid: existids ){ If (Exsitid. Equals (ID )){ // The ID is in the list. Response. getwriter (). println ("Add success "); Return ;}} // 4. If the current item is not in the list, add it to the list. Cookie = New Cookie (" Visitlist ", Visitlist +" , "+ Id); cookie. setmaxage (60*60); response. addcookie (cookie); response. getwriter (). println (" Add success ");}} Else { // Indicates that there is no cookie for the site Cookie = New Cookie (" Visitlist ", ID); cookie. setmaxage (60*60); // set the cookie validity period to 1 hour response. addcookie (cookie); response. getwriter (). println (" Add success ");}} Public Void Dopost (httpservletrequest request, httpservletresponse response) Throws Servletexception, ioexception {doget (request, response );
}}Step 3: Write a clear access record page, clear page, and clearcookie page. The Code is as follows:The principle is to set the cookie validity period to 0, so that the cookie will be cleared.
Package CN. itcast. Cookie; Import Java. Io. ioexception;Import Javax. servlet. servletexception; Import Javax. servlet. http. Cookie; Import Javax. servlet. http. httpservlet; Import Javax. servlet. http. httpservletrequest; Import Javax. servlet. http. httpservletresponse; Public Class Clearcookie Extends Httpservlet { Public Void Doget (httpservletrequest request, httpservletresponse response)Throws Servletexception, ioexception {cookie = New Cookie (" Visitlist "," "); Cookie. setmaxage (0); response. addcookie (cookie); response. sendredirect (" /Myday07/products1.jsp ");} Public Void Dopost (httpservletrequest request, httpservletresponse response) Throws Servletexception, ioexception {doget (request, response );}}