Session example-use of simple Shopping Cart
As in the previous article, servlet is used for processing requests and displaying pages.
The functions are as follows:
1. display all items on the website
2. After you click "buy", you can remember the items you have selected.
3. multiple sessions are shared with one session.
4. Data Sharing after the browser disables cookies
Home page:
Package CN. itcast. shopping; import Java. io. ioexception; import Java. io. printwriter; import Java. io. serializable; import Java. util. hashmap; import Java. util. map; import javax. servlet. servletexception; import javax. servlet. HTTP. httpservlet; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; // The homepage that displays all products of the Website: public class indexservlet extends httpservlet {public void doget (H Ttpservletrequest request, httpservletresponse response) throws servletexception, ioexception {response. setcontenttype ("text/html; charset = UTF-8"); response. setcharacterencoding ("UTF-8"); printwriter out = response. getwriter (); // For the first access request. getsession (); // 1. show all items on the website. print ("This website has the following books: <br/>"); Map <string, book> map = dB. getmap (); For (map. entry <string, book> entry: map. entryset () {book = entry. Getvalue (); string url = "/day07/servlet/buyservlet? Id = "+ book. GETID (); url = response. encodeurl (URL); // get the URL after rewriting. print (book. getname () + "<a href = '" + URL + "'> purchase </a> <br/>") ;}} public void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {doget (request, response) ;}} class dB {Private Static Map <string, book> map = new hashmap (); static {map. put ("1", new book ("1", "javaweb Development", "Lao Zhang"); map. put ("2", new book ("2", "JDBC Development", "Lao Li"); map. put ("3", new book ("3", "struts2 Development", "Lao Zhang"); map. put ("4", new book ("4", "Spring Development", "Lao Li"); map. put ("5", new book ("5", "hibernate Development", "Lao Zhang");} public static map getmap () {return map ;}} class book implements serializable {private string ID; private string name; private string author; Public book () {super (); // todo auto-generated constructor stub} public book (string ID, string name, string author) {super (); this. id = ID; this. name = Name; this. author = author;} 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 ;}}
Purchase page:
Package CN. itcast. shopping; import Java. io. ioexception; import Java. util. arraylist; import Java. util. list; 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 javax. servlet. HTTP. httpsession; // purchase public class buyservlet extends httpservlet {public void doget (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {// 1. obtain the desired book string id = request. getparameter ("ID"); book = (book) dB. getmap (). get (ID); // 2. add the books bought by the user to the session for managing all the books in the collection httpsession session = request. getsession (); // cookiecookie that overwrites the JSESSIONID of the server. Cookie = new cookie ("JSESSIONID", session. GETID (); cookie. setmaxage (30*60); cookie. setpath ("/day07"); response. addcookie (cookie); List list = (list) session. getattribute ("list"); If (list = NULL) {list = new arraylist (); Session. setattribute ("list", list);} List. add (book); // 3. jump to the shopping cart display list // request. getrequestdispatcher ("/servlet/listcartservlet "). forward (request, response); // automatically carries the session ID string url = response. encoderedirecturl ("/day07/servlet/listcartservlet"); // rewrite response by implementing the URL. sendredirect (URL);} public void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {doget (request, response );}}
Shopping Cart list display page:
Package CN. itcast. shopping; import Java. io. ioexception; import Java. io. printwriter; import Java. util. list; import javax. servlet. servletexception; import javax. servlet. HTTP. httpservlet; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; // display all books bought by the user (the shopping Carlist page) public class listcartservlet extends httpservlet {public void doget (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {response. setcontenttype ("text/html; charset = UTF-8"); response. setcharacterencoding ("UTF-8"); printwriter out = response. getwriter (); out. print ("you have purchased the following products: <br/>"); // first, check whether the browser has brought sessionid in the form of a cookie, if no, check whether the URL address contains sessionidlist <book> List = (list) request. getsession (). getattribute ("list"); For (Book: List) {out. print (book. getname () + "<br/>") ;}} public void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {doget (request, response );}}