Product Browsing details record the item ID and save it in the cookie package com.kt.servlet_qt;import java.io.ioexception;import java.io.printwriter;import java.util.arraylist;import java.util.hashset;import java.util.list; import java.util.set;import javax.servlet.servletexception;import javax.servlet.http.cookie; import javax.servlet.http.httpservlet;import javax.servlet.http.httpservletrequest;import javax.servlet.http.httpservletresponse;import com.kt.bean.easybuy_shangpin;import com.kt.bean.easybuy_shangpinfenlei;import com.kt.dao.impl.easybuyimpl;public class Merchandisetype extends httpservlet {public void doget (HttpServletRequest Request, httpservletresponse response) throws servletexception, ioexception { Printwriter out = response.getwriter (); String uid = request.getparameter ("UID"); String uname = request.getparameter ("uname"); Easybuyimpl&nBsp;e = new easybuyimpl (); Easybuy_shangpin all_id_shangpin = e.all_id_shangpin (Integer.parseint (UID));// get the merchandise idint &NBSP;ID&NBSP;=&NBSP;ALL_ID_SHANGPIN.GETEP_ID ();// Save goods idcookie cookie1 = null;// Initial creation of Cookiecookie1 = new cookie ("cookis_id", ""); Cookie1.setvalue (id + ",");// How many seconds expires Cookie1.setmaxage (7 * 24 * 60 * 60);// added to the client Response.addcookie ( COOKIE1);// First get the cookie whether there is a value cookie[] ck = request.getcookies ();// to Judge if (ck ! = null) {// The variable that defines the accumulated value is empty string value = "";// loop Cookie takes value for (cookie COOKIE&NBSP;:&NBSP;CK) {// Determine if there is cookis_id name if (Cookie.getname (). Equals ("cookis_id")) {// Get the latest cookie value value = cookie.getvalue ();// divide the cookie into (",") split string[] split = Value.split (",");// creates a set set, ("Because Set can automatically recognize duplicate values") set<string> set = new hashset<string> ();// will get the value loop into the set to determine if there is the same number for (int i = 0; i < split.length; i++) {set.add (split[split.length - (i + 1));} empties the defined value, making the next stitch value = "";// create an array string[] array = new string[] {};// converts the value of a set collection into an array string[] toarray = (string[]) set.toarray (array);// Here you have to decide how many new records you want to show if (toarray.length < 4) {// Create collection List<string> list = new ArrayList<String> ();// loop toarray array adds values to the list collection for (int i = 0; i < toarray.length; i++) {list.add (Toarray[i]);} Cycle to determine if the value received is the same if there is the removal of for (Int i = 0; i < list.size (); i++) {if (Integer.parseint (List.get (i)) == id) {list.remove (i);}} finally get the data for the list collection to splice for (string string : list) {value += string + ",";} Then add the latest data to the Cookie in Cookie1 = new cookie ("cookis_id", value + id + ",");} else {// if it is larger than the number of data bars you want to display// create a collection list<string> list = new arraylist <String> ();// loop toarray array adds values to the list collection for (int i = 0; i < toarray.length; i++) {list.add (toarray[toarray.length - (i + 1));} Define a receive removed String li = list.get (0);// remove list.remove (0);// loop to determine if there is the same, and perform value stitching up-to-date data for (string string : list) {if (Integer.parseint (String) == id) {string = li;} if (Integer.parseint (String) != id) {value += string + ",";}} Add latest data Cookis_idcookie1 = new cookie ("cookis_id", value + id + ",");} How many seconds expires Cookie1.setmaxage (7 * 24 * 60 * 60);// added to client Response.addcookie (COOKIE1);}} // Product Categories Search All List<easybuy_shangpinfenlei> all_shangpinfenlei = e.all_ Shangpinfenlei (); Request.getsession (). SetAttribute ("All_shangpinfenlei", All_shangpinfenlei); request.getsession (). SetAttribute ("uname", uname); Request.getsession (). SetAttribute ("All_id_shangpin", all_id_shangpin ); Request.getrequestdispatcher ("product-view.jsp"). Forward (Request,response); Out.flush (); Out.close ();}} Get the cookie to get the value and pass it to the JSP interface package com.kt.servlet_qt;import java.io.ioexception;import java.io.printwriter;import java.util.arraylist;import java.util.hashset;import java.util.list; import java.util.set;import javax.servlet.servletexception;import javax.servlet.http.cookie; import javax.servlet.http.httpservlet;import javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import com.kt.bean.easybuy_shangpin;import com.kt.bean.easybuy_shangpinfenlei;import com.kt.bean.easybuy_xinwen;import Com.kt.dao.impl.easybuyimpl;public class blank extends httpservlet {public void doget (Httpservletrequest request, httpservletresponse response) throws Servletexception, ioexception {printwriter out = response.getwriter ();// Gets the value stored by the cookie cookie[] cookies = request.getcookies ();if (cookies != null) { Determine if a cookie is empty// create a collection arraylist<easybuy_shangpin> all_cookle_shangpin = new ArrayList<Easybuy_Shangpin> ();// defines a variable string value = "" that uses the decomposition cookie;// The cyclic Cookie determines if there is cookis_idfor (cookie cookie : cookies) {if (Cookie.getname (). Equals ("cookis_id")) {// receives the value of the cookie Value = cookie.getvalue ();// then takes ("," ) decomposition to get data idstring[] split = value.split (",");// Loop added to all_cooKle_shangpin the latest data to the JSP page in the collection for (int i = 0; i < split.length; i++) {all_cookle_shangpin.add (E.all_cookle_shangpin (Integer.parseint (split[split.length- (i + 1)]). Get (0));// split[split.length-// (i +// 1)] refers to the data from the latest start}// Save to session request.getsession (). SetAttribute ("All_cookle_shangpin", All_cookle_shangpin);}} Use forward request.getrequestdispatcher ("index.jsp"). Forward (Request, response); Out.flush (); Out.close ();}}
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7F/6B/wKioL1ceI8Cj9wW5AAC86HoUwfs782.png "style=" float: none; "title=" Cookie "alt=" Wkiol1cei8cj9ww5aac86houwfs782.png "/>
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7F/6B/wKioL1ceI8CR0OzeAAAxKLbAqpY416.png "style=" float: none; "Title=" JSP gets Data "alt=" Wkiol1cei8cr0ozeaaaxklbaqpy416.png "/>
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/7F/6B/wKioL1ceI8HzzBUhAAAmE0Phzz0576.png "style=" float: none; "title=" browsing Record "alt=" Wkiol1cei8hzzbuhaaame0phzz0576.png "/>
Java Cookie record recently viewed product record (with latest five browsing history)