이 기사는 쿠키를 사용하여 최근에 본 참조 책을 표시하는 관련 방법을 공유합니다. 특정 내용은 다음과 같습니다
1. 벤 패키지
java.io.serializable import; 공개 수업 도서는 직렬화 가능한 {private String id; 개인 문자열 이름; 개인 문자열 가격; 개인 문자열 인증; 개인 문자열 게시; 개인 문자열 설명; public book () {} public book (문자열 ID, 문자열 이름, 문자열 가격, 문자열 인증, 문자열 게시, 문자열 설명) {super (); this.id = id; this.name = 이름; this.price = 가격; this.auth = auth; this.publish = 게시; this.description = 설명; } public String getDescription () {return description; } public void setDescription (문자열 설명) {this.Description = 설명; } public String getId () {return id; } public void setId (String id) {this.id = id; } public String getName () {return name; } public void setName (문자열 이름) {this.name = 이름; } public String getPrice () {반환 가격; } public void setPrice (문자열 가격) {this.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 패키지
import java.util.linkedhashmap; import java.util.map; cn.huiyu.ben.book import; 공개 클래스 BookDao {private static map <String, book> bookmap = new LinkedHashMap <String, book> (); private bookdao () {} static {bookmap.put ( "1", 새 책 ( "1", "1111", "11.0", "Zqwang", "111 Press", "111111111")); BookMap.put ( "2", 새 책 ( "2", "2222", "22.0", "Zqwang", "222222222")); BookMap.put ( "3", 새 책 ( "3", "3333", "33.0", "Zqwang", "3333333333")); } public static map <string, book> getbooks () {return bookmap; } public static book getbook (String Id) {return bookmap.get (id); }} 3. 서비스
public void doget (httpservletrequest request, httpservletreponse response)는 servletexception, ioexception {response.setContentType ( "text/html; charset = utf-8"); // 1. 데이터베이스의 모든 책을 쿼리하여 map <string, book> map = bookdao.getBooks (); for (map.entry <string, book> entry : map.entryset ()) {book book = entry.getValue (); response.getWriter (). writ } response.getWriter (). 쓰기 ( "<hr>"); // 2. 쿠키 전에 읽은 책을 보여주십시오 [] cs = request.getCookies (); 쿠키 findc = null; if (cs! = null) {for (쿠키 c : cs) {if ( "last".equals (c.getName ()) {findc = c; }}} if (findc == null) {response.getWriter (). 쓰기 ( "책을 읽지 않았습니다!"); } else {response.getWriter (). 쓰기 ( "당신이 탐색 한 책 : <br>"); 문자열 [] ids = findc.getValue (). split ( ","); for (string id : ids) {book book = bookdao.getbook (id); response.getWriter (). 쓰기 (book.getName ()+"<br>"); }}} 4. 서비스
public void doget (httpservletrequest request, httpservletreponse response)는 servletexception, ioexception {response.setContentType ( "text/html; charset = utf-8"); // 1. 책의 ID를 읽고, 데이터베이스를 쿼리하여 책을 찾은 다음 책의 자세한 정보를 출력하십시오. id = request.getParameter ( "id"); Book Book = Bookdao.getBook (ID); if (book == null) {response.getWriter (). 쓰기 ( "이 책을 찾을 수 없습니다!"); 반품; } else {response.getWriter (). 쓰기 ( "<h1> 제목 :"+book.getName ()+"</h1>"); response.getWriter (). 쓰기 ( "<H3> 저자 :"+book.getAuth ()+"</h3>"); response.getWriter (). 쓰기 ( "<H3> 판매 가격 :"+book.getPrice ()+"</h3>"); response.getWriter (). 쓰기 ( "<H3> Publisher :"+book.getPublish ()+"</h3>"); response.getWriter (). 쓰기 ( "<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 = ""; 쿠키 [] cs = request.getCookies (); 쿠키 findc = null; if (cs! = null) {for (쿠키 c : cs) {if ( "last".equals (c.getName ()) {findc = c; }}} if (findc == null) {// ids += book.getId () 앞에 책을 읽지 않은 설명 레코드; } else {// 이전 역사에서 읽은 책의 설명 레코드이며, 기록 문자열 [] olds = finds = findc.getValue (). split ( ",")에 따라 새로운 레코드를 계산해야합니다. StringBuffer buffer = new StringBuffer (); buffer.append (book.getid ()+","); for (int i = 0; i <olds.length && buffer.tostring (). split ( ","). 길이 <3; i ++) {String old = Olds [i]; if (! Old.equals (book.getId ())) {buffer.append (Old+","); }} ids = buffer.substring (0, buffer.length () -1); } 쿠키 lastc = 새로운 쿠키 ( "last", ids); lastc.setmaxage (3600*24*30); lastc.setpath (request.getContextPath ()); 응답 .addcookie (lastc); }위는이 기사에 관한 것입니다. 쿠키를 사용하여 최근에 본 책을 전시하는 방법을 배우는 것이 도움이되기를 바랍니다.