20160328 Javaweb Cookie Little Practice

Source: Internet
Author: User

Browsing with cookies for historical records:

Because it is a simple demonstration, the database is replaced directly with JavaBean.

Data storage classes:

Package Com.dzq.dao;import Java.util.*;import Com.dzq.domain.bookbean;public class Bookdao {private static map< String, bookbean> bookmap=new linkedhashmap<string, bookbean> ();p rivate Bookdao () {}static {bookMap.put ("1", New Bookbean ("1", "Kingdoms", "99.0", "belly", "Hippo Publishing", "The Man's Story")), Bookmap.put ("2", New Bookbean ("2", "West Royal Street", "99.0", "Belly squeak", "Hippo Publishing", "The man's Story"); Bookmap.put ("3", New Bookbean ("3", "negligent turn", "99.0", "tripe", "Hippo Publishing", "The Man's Story")); Bookmap.put ("4", New Bookbean ("4", "Pa pa", "99.0", "Ze Zechun", "Hippo Publishing", "The Man's Story");} public static map<string,bookbean> Getbooks () {return bookmap;} public static Bookbean GetBook (String ID) {return bookmap.get (ID);}}

JavaBean class:

Package Com.dzq.domain;import Java.io.serializable;public class Bookbean implements serializable{private String ID;  private string Name;private string Price;private string auth;private string Publish;private string Discribe;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 GetPrice () {return price;} public void Setprice (String price) {this.price = Price;} Public String GetAuth () {return auth;} public void Setauth (String auth) {This.auth = auth;} Public String Getpublish () {return publish;} public void Setpublish (String publish) {this.publish = publish;} Public String Getdiscribe () {return discribe;} public void Setdiscribe (String discribe) {this.discribe = Discribe;} Public Bookbean () {}public Bookbean (string ID, string name, string price, string auth,string Publish, String discribe) {thi S.id = Id;this.name = Name;this.price = Price;this.auth = Auth;this.publish =Publish;this.discribe = Discribe;}} 

Import Java.io.ioexception;import Java.util.map;import Javax.servlet.servletexception;import Javax.servlet.annotation.webservlet;import Javax.servlet.http.cookie;import Javax.servlet.http.HttpServlet; Import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Com.dzq.dao.bookdao;import Com.dzq.domain.BookBean; @WebServlet ("/booklistservlet") public class Booklistservlet Extends HttpServlet {private static final long serialversionuid = 1l;protected void doget (HttpServletRequest request, Http Servletresponse response) throws Servletexception, IOException {response.setcontenttype ("Text/html;charset=utf-8") ;//query All books and show,map<string,bookbean> Map=bookdao.getbooks (); for (map.entry<string, bookbean> Entry: Map.entryset ()) {Bookbean book=entry.getvalue (); Response.getwriter (). Write ("<a href= ' bookinfoservlet?id=" + Book.getid () + "' >" +book.getname () + "</a><br/>");} Response.getwriter (). Write ("

Package Com.dzq.servlet;import Java.io.ioexception;import Javax.servlet.servletexception;import Javax.servlet.annotation.webservlet;import Javax.servlet.http.cookie;import Javax.servlet.http.HttpServlet; Import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Com.dzq.dao.bookdao;import Com.dzq.domain.BookBean; @WebServlet ("/bookinfoservlet") public class Bookinfoservlet   Extends HttpServlet {private static final long serialversionuid = 1L; protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { Response.setcontenttype ("Text/html;charset=utf-8"); String id=request.getparameter ("id"); Bookbean Book=bookdao.getbook (ID); if (book==null) {response.getwriter (). Write ("Cannot find the book");} Else{response.getwriter (). Write ("

Features: Display the history of the three book information, and in accordance with the order of browsing, the latest browsing in the top row

20160328 Javaweb Cookie Little Practice

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.