この記事では、Cookieを使用して最近閲覧された本を参照用に表示する関連する方法を共有しています。特定のコンテンツは次のとおりです
1.ベンパッケージ
java.io.serializableをインポートします。パブリッククラスの本は、Serializable {private string id;プライベート文字列名;プライベート文字列価格。プライベート文字列AUTH;プライベート文字列パブリッシュ;プライベート文字列の説明; public book(){} public Book(string id、string name、string price、string auth、string publish、string description){super(); this.id = id; this.name = name; this.price = price; this.auth = auth; this.publish = publish; this.description = description; } public string getDescription(){return description; } public void setDescription(string description){this.description = description; } 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 rice){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; }} 2。DAOパッケージ
java.util.linkedhashmap; Import java.util.map; cn.huiyu.ben.bookをインポートします。 public class bookdao {private static map <string、book> bookmap = new linkedhashmap <string、book>(); private bookdao(){} static {bookmap.put( "1"、new book( "1"、 "1111"、 "11.0"、 "zqwang"、 "111 press"、 "111111111")); bookmap.put( "2"、new Book( "2"、 "2222"、 "22.0"、 "zqwang"、 "22222222222")); bookmap.put( "3"、new Book( "3"、 "3333"、 "33.0"、 "zqwang"、 "33333333333")); } public static Map <string、book> getBooks(){return bookmap; } public static book getBook(string id){return bookmap.get(id); }} 3.サーブレット
public void doget(httpservletrequest request、httpservletresponse応答)servletexception、ioexception {respons.setcontenttype( "text/html; charset = utf-8"); // 1。データベース内のすべての書籍をクエリしてマップ<文字列、book> map = bookdao.getbooks()を表示します。 for(map.entry <string、book> entry:map.entryset()){book book = entry.getValue(); Response.getWriter()。write( "<a href = '"+request.getContextPath()+"/servlet/bookinfoservlet?id ="+book.getId()+"'>"+book.getName()+"</a> <br>"); } Response.getWriter()。write( "<hr>"); // 2。 Cookie [] cs = request.getCookies()の前に読んだ本を表示します。 Cookie findc = null; if(cs!= null){for(cookie c:cs){if( "last" .equals(c.getname())){findc = c; }}} if(findc == null){respons.getWriter()。write( "本を読んでいない!"); } else {response.getWriter()。write( "browsed:<br>"); string [] ids = findc.getValue()。split( "、"); for(string id:ids){book book = bookdao.getbook(id); Response.getWriter()。write(book.getName()+"<br>"); }}} 4.サーブレット
public void doget(httpservletrequest request、httpservletresponse応答)servletexception、ioexception {respons.setcontenttype( "text/html; charset = utf-8"); // 1。本のIDを読み取り、データベースをクエリして本を見つけ、book string id = request.getParameter( "id")の詳細情報を出力します。 book book = bookdao.getbook(id); if(book == null){response.getWriter()。write( "この本は見つかりません!");戻る; } else {response.getWriter()。write( "<h1> title:"+book.getName()+"</h1>"); Response.getWriter()。write( "<h3>著者:"+book.getauth()+"</h3>"); Response.getWriter()。write( "<h3>販売価格:"+book.getPrice()+"</h3>"); Response.getWriter()。write( "<h3> publisher:"+book.getpublish()+"</h3>"); Response.getWriter()。write( "<h3>説明:"+book.getDescription()+"</h3>"); } // 2。あなたが読んだ最後の本を保存するためにクッキーを送る// --- 1-> 1 // 1-2,1-> 2,1 // 2,1--3,2,1-> 3,2,1 // 3,2,1-> 3,2,1-> 4,3,2-> 4,3,2 // 4,3,2 -3,4,2-> 3,4,2 String IDS = "" "; cookie [] cs = request.getCookies(); Cookie findc = null; if(cs!= null){for(cookie c:cs){if( "last" .equals(c.getname())){findc = c; }}} if(findc == null){// IDS += book.getId();の前に本を読んでいない説明記録; } else {//以前に歴史で読まれた本の説明記録であり、歴史文字列[] olds = findc.getValue()。 stringbuffer buffer = new StringBuffer(); buffer.append(book.getid()+"、"); for(int i = 0; i <olds.length && buffer.tostring()。split( "、")。length <3; i ++){string olds [i]; if(!old.equals(book.getid())){buffer.append(old+"、"); }} ids = buffer.substring(0、buffer.length()-1); } cookie lastc = new Cookie( "last"、ids); LastC.SetMaxage(3600*24*30); lastc.setPath(request.getContextPath()); Response.AddCookie(lastc); }上記はこの記事に関するすべてです。誰もがCookieを使用して最近視聴された本を表示する方法を学ぶことが役立つことを願っています。