Java使用cookie显示最近查看过的书
本文实例为大家分享了Java使用cookie显示最近查看过的书的相关方法,供大家参考,具体内容如下
1.ben包
importjava.io.Serializable;
publicclassBookimplementsSerializable{
privateStringid;
privateStringname;
privateStringprice;
privateStringauth;
privateStringpublish;
privateStringdescription;
publicBook(){
}
publicBook(Stringid,Stringname,Stringprice,Stringauth,
Stringpublish,Stringdescription){
super();
this.id=id;
this.name=name;
this.price=price;
this.auth=auth;
this.publish=publish;
this.description=description;
}
publicStringgetDescription(){
returndescription;
}
publicvoidsetDescription(Stringdescription){
this.description=description;
}
publicStringgetId(){
returnid;
}
publicvoidsetId(Stringid){
this.id=id;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicStringgetPrice(){
returnprice;
}
publicvoidsetPrice(Stringprice){
this.price=price;
}
publicStringgetAuth(){
returnauth;
}
publicvoidsetAuth(Stringauth){
this.auth=auth;
}
publicStringgetPublish(){
returnpublish;
}
publicvoidsetPublish(Stringpublish){
this.publish=publish;
}
}
2.Dao包
importjava.util.LinkedHashMap;
importjava.util.Map;
importcn.huiyu.ben.Book;
publicclassBookDao{
privatestaticMap<String,Book>bookMap=newLinkedHashMap<String,Book>();
privateBookDao(){
}
static{
bookMap.put("1",newBook("1","1111","11.0","zqwang","111出版社","111111111"));
bookMap.put("2",newBook("2","2222","22.0","zqwang","222出版社","222222222"));
bookMap.put("3",newBook("3","3333","33.0","zqwang","333出版社","333333333"));
}
publicstaticMap<String,Book>getBooks(){
returnbookMap;
}
publicstaticBookgetBook(Stringid){
returnbookMap.get(id);
}
}
3.servlet
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
response.setContentType("text/html;charset=utf-8");
//1.查询数据库中所有的书展示
Map<String,Book>map=BookDao.getBooks();
for(Map.Entry<String,Book>entry:map.entrySet()){
Bookbook=entry.getValue();
response.getWriter().write("<ahref='"+request.getContextPath()+"/servlet/BookInfoServlet?id="+book.getId()+"'>"+book.getName()+"</a><br>");
}
response.getWriter().write("<hr>");
//2.显示之前看过的书
Cookie[]cs=request.getCookies();
CookiefindC=null;
if(cs!=null){
for(Cookiec:cs){
if("last".equals(c.getName())){
findC=c;
}
}
}
if(findC==null){
response.getWriter().write("没有看过任何书!");
}else{
response.getWriter().write("您曾经浏览过的书:<br>");
String[]ids=findC.getValue().split(",");
for(Stringid:ids){
Bookbook=BookDao.getBook(id);
response.getWriter().write(book.getName()+"<br>");
}
}
}
4.servlet
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
response.setContentType("text/html;charset=utf-8");
//1.获取要看的书的id,查询数据库找出书,输出书的详细信息
Stringid=request.getParameter("id");
Bookbook=BookDao.getBook(id);
if(book==null){
response.getWriter().write("找不到这本书!");
return;
}else{
response.getWriter().write("<h1>书名:"+book.getName()+"</h1>");
response.getWriter().write("<h3>作者:"+book.getAuth()+"</h3>");
response.getWriter().write("<h3>售价:"+book.getPrice()+"</h3>");
response.getWriter().write("<h3>出版社:"+book.getPublish()+"</h3>");
response.getWriter().write("<h3>描述信息:"+book.getDescription()+"</h3>");
}
//2.发送cookie保存最后看过的书
//---1-->1
//1--2,1-->2,1
//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
Stringids="";
Cookie[]cs=request.getCookies();
CookiefindC=null;
if(cs!=null){
for(Cookiec:cs){
if("last".equals(c.getName())){
findC=c;
}
}
}
if(findC==null){
//说明之前没有看过书的记录
ids+=book.getId();
}else{
//说明之前有历史看过的书的记录,需要根据历史记录算一个新的记录出来
String[]olds=findC.getValue().split(",");
StringBufferbuffer=newStringBuffer();
buffer.append(book.getId()+",");
for(inti=0;i<olds.length&&buffer.toString().split(",").length<3;i++){
Stringold=olds[i];
if(!old.equals(book.getId())){
buffer.append(old+",");
}
}
ids=buffer.substring(0,buffer.length()-1);
}
CookielastC=newCookie("last",ids);
lastC.setMaxAge(3600*24*30);
lastC.setPath(request.getContextPath());
response.addCookie(lastC);
}
以上就是本文的全部内容,希望对大家学习Java使用cookie显示最近查看过的书的方法有所帮助。