凯发真人娱乐

教你使用cookie法,查看最近看过的书 -凯发真人娱乐

2023-12-01,,

专为编程打造,自动写代码机器人,免费开通

教你使kie法,查看最近看过的书
import java.io.serializable;

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);
}

向ai问一下细节

网站地图