网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

教你使用cookie法,查看最近看过的书

教你使用cookie法,查看最近看过的书
import java.io.Serializable;

创新互联公司专业为企业提供柳北网站建设、柳北做网站、柳北网站设计、柳北网站制作等企业网站建设、网页设计与制作、柳北企业网站模板建站服务,10多年柳北做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

public class Book implements Serializable {
private String id;
private String name;
private String price;
private String auth;
private String publish;
private String description;

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 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 void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
//1.获取要看的书的id,查询数据库找出书,输出书的详细信息
String id = request.getParameter("id");
Book book = BookDao.getBook(id);
if(book==null){
response.getWriter().write("找不到这本书!");
return;
}else{
response.getWriter().write("

书名:"+book.getName()+"

");
response.getWriter().write("

作者:"+book.getAuth()+"

");
response.getWriter().write("

售价:"+book.getPrice()+"

");
response.getWriter().write("

出版社:"+book.getPublish()+"

");
response.getWriter().write("

描述信息:"+book.getDescription()+"

");
}

//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
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{
//说明之前有历史看过的书的记录,需要根据历史记录算一个新的记录出来
String [] olds = findC.getValue().split(",");
StringBuffer buffer = new StringBuffer();
buffer.append(book.getId()+",");
for(int i = 0;iString old = 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(36002430);
lastC.setPath(request.getContextPath());
response.addCookie(lastC);
}教你使用cookie法,查看最近看过的书


文章名称:教你使用cookie法,查看最近看过的书
本文网址:http://bjjierui.cn/article/gpoooh.html

其他资讯