Cookies are client-side technology, and the program writes each user's data to the user's browser in the form of a cookie. When users use the browser to access Web resources in the server, they will go with their own data. In this way, Web resources deal with the user's own data.
Home: Show user last access time public class CookieDemo1 extends HttpServlet {public void doget (HttpServletRequest request, HTTPSERVL
Etresponse response) throws Servletexception, IOException {response.setcharacterencoding ("UTF-8");
Response.setcontenttype ("Text/html;charset=utf-8"); Response.getwriter (). Write ("Your last access Time is:");
lastaccesstime=2323878327//Your last access time Cookie cookies[] = request.getcookies ();
for (int i=0;cookies!=null&& i<cookies.length;i++) {Cookie cookie = cookies[i];
if (Cookie.getname (). Equals ("LastAccessTime")) {Long time = Long.parselong (Cookie.getvalue ());
Date d = new date (time);
Response.getwriter (). Write (d.tolocalestring ());
The time cookie for this visit is sent to the browser = new Cookie ("LastAccessTime", System.currenttimemillis () + "");
Cookie.setmaxage (0);
Cookie.setpath ("/test");
Response.addcookie (cookie); public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {doget (request, response); }
}
Represents the home public class CookieDemo2 extends HttpServlet {public void doget (HttpServletRequest request, HttpServletResponse
Response) throws Servletexception, IOException {response.setcharacterencoding ("UTF-8");
Response.setcontenttype ("Text/html;charset=utf-8");
PrintWriter out = Response.getwriter ();
Show All Products Out.write ("This site has the following products <br/><br/>");
Set<map.entry<string, book>> set = Db.getallbook (). EntrySet ();
For (map.entry<string, book> me:set) {String id = me.getkey ();
Book book = Me.getvalue (); Out.write ("<a href= '/test/servlet/cookiedemo3?id=" +id+ "' target= ' _blank ' >" +book.getname () + "</a><
Br/> ");
//Show the Product Out.write ("<br/><br/><br/> you have seen the following product:<br/><br/>");
Cookie cookies[] = request.getcookies (); for (int i=0;cookies!=null && i<cookies.length;i++) {if (Cookies[i].getname (). Equals ("Bookhistory")) {// Bookhistory=1_2_3 String value = cookies[i]. GetValue (); String ids[] = Value.split ("\\_");
_ for (String id:ids) {Book book = (book) Db.getallbook (). get (ID);
Out.write (Book.getname () + "<br/>"); }}} public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexcept
Ion, IOException {doget (request, response);
The class db{private static map<string,book> Map = new Linkedhashmap ();
static{map.put ("1", New book ("1", "Javaweb development", "Author1", "a Good Books"));
Map.put ("2", New book ("2", "JDBC Development", "Author1", "a Good Books"));
Map.put ("3", New book ("3", "Sprng development", "Author2", "a Good Books"));
Map.put ("4", New book ("4", "Hibernate development", "Author1", "a Good Books"));
Map.put ("5", New book ("5", "Strurs development", "Author2", "a Good Books"));
public static map Getallbook () {return map;
Class book{private String ID;
private String name;
Private String author;
Private String discription; Public book () {} public book (string ID, string name, string author, string discription) {
Super ();
This.id = ID;
THIS.name = name;
This.author = author;
This.discription = discription;
Public String GetId () {return id;
public void SetId (String id) {this.id = ID;
Public String GetName () {return name;
public void SetName (String name) {this.name = name;
Public String Getauthor () {return author;
} public void Setauthor (String author) {this.author = author;
Public String getdiscription () {return discription;
} public void Setdiscription (String discription) {this.discription = discription;
}
}
Display Product Details public class CookieDemo3 extends HttpServlet {public void doget (HttpServletRequest request, HTTPSERVLETRESP
Onse response) throws Servletexception, IOException {response.setcharacterencoding ("UTF-8");
Response.setcontenttype ("Text/html;charset=utf-8");
PrintWriter out = Response.getwriter ();
Displays the product information String ID = request.getparameter ("id"), depending on the ID of the user band;
Book book = [book] Db.getallbook (). get (ID);
Out.write (Book.getid () + "<br/>");
Out.write (Book.getname () + "<br/>");
Out.write (Book.getauthor () + "<br/>");
Out.write (book.getdiscription () + "<br/>");
Send a cookie to the user String value = makebookhistory (Request,id);
Cookie cookie = new Cookie ("Bookhistory", value);
Cookie.setmaxage (3600);
Response.addcookie (cookie); //Create a cookie value sent to the user private string makebookhistory (HttpServletRequest request, string id) {String bookhistory =
Null
Cookie cookies[] = request.getcookies (); for (int i=0;cookies!=null && I<cookies.length;i++) {if (Cookies[i].getname (). Equals ("Bookhistory")) {bookhistory = Cookies[i].getvalue (); }//bookhistory=null 3 bookhistory=3//bookhistory=1 3 bookhistory=3_1//bookhistory=1_4_5 3
Bookhistory=3_1_4//bookhistory=1_3_5 3 bookhistory=3_1_5 if (bookhistory==null) {bookhistory = ID;
return bookhistory; }//bookhistory=1_3_5 linkedlist<string> list = new LinkedList (Arrays.aslist (Bookhistory.split ("\\_"));
list[1,3,5] 3/*if (List.contains (id)) {list.remove (ID);
List.addfirst (ID);
}else{if (list.size () >=3) {//list[1,4,5] 3 list.removelast ();
List.addfirst (ID);
}else{List.addfirst (ID);
}*/if (List.contains (ID)) List.remove (ID);
else{if (list.size () >=3) list.removelast ();
} list.addfirst (ID);
StringBuffer sb = new StringBuffer ();
for (String bid:list) {Sb.append (bid+ "_");
} Sb.deletecharat (Sb.length ()-1); REturn sb.tostring ();
public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
Doget (request, response);
}
}