cookie实现浏览记录部分逻辑代码的问题?
工具类模拟数据库中的书籍信息:
public class BookDB {
private static Map<String, Book> books = new HashMap<String, Book>();
static {
books.put("1", new Book("1","Java","99.9","javaer","no.1 language"));
books.put("2", new Book("2","C++","9.9","c++er","no.2 language"));
books.put("3", new Book("3","PHP","0.9","phper","useless"));
books.put("4", new Book("4","JavaScript","19.9","JSer","666"));
books.put("5", new Book("5","swift","9.9","appler","iphone"));
}
public static Map<String, Book> findAllBooks() {
return books;
}
public static Book findBookById(String id) {
return books.get(id);
}
}
显示所有书籍的servlet:
public class ShowAllBook extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.write("<h1>书籍列表</h1>");
Map<String, Book> books = BookDB.findAllBooks();
for (Map.Entry<String, Book> book : books.entrySet()) {
out.write("<a href='"+request.getContextPath()+"/showBookDetail?id="+book.getKey()+"' target='_blank'>"
+book.getValue().getName()+"</a><br/>");
}
out.write("您最近浏览的历史:<br/>");
Cookie[] cookies = request.getCookies();
for (int i=0; cookies!=null&&i<cookies.length; i++) {
if ("historyBookId".equals(cookies[i].getName())) {
String[] strs = cookies[i].getValue().split("-");
for (int j=0; j<strs.length; j++) {
out.write(BookDB.findBookById(strs[j]).getName() + "<br/>");
}
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
点击书名跳转到ShowBookDetail的servlet中:
public class ShowBookDetail extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
String id = request.getParameter("id");
out.println(BookDB.findBookById(id).getName());
String hisId = makeId(id, request);
Cookie c = new Cookie("historyBookId", hisId);
c.setPath(request.getContextPath());
response.addCookie(c);
}
private String makeId(String id, HttpServletRequest request) {
Cookie[] c = request.getCookies();
if (c == null) {
return id;
}
//返回的id的内容
String result = "";
for (int i=0; i<c.length; i++) {
if ("historyBookId".equals(c[i].getName())) {
if (c[i].getValue().indexOf(id) == -1) {
result = id + "-" + c[i].getValue();
break;
} else if (c[i].getValue().indexOf(id) == 0) {
result = c[i].getValue();
break;
} else {
result = id;
String[] str = c[i].getValue().split("-");
for (int j=0; j<str.length; j++) {
//遍历cookie(historyBookId)里是否有传入的id
if (!id.equals(str[j])) {
result += "-" + str[j];
}
}
break;
}
}
}
if (result.length() <= 5) {
return result;
} else {
return result.substring(0, 5);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
在ShowBookDetail代码中,判断之前cookie里的id是否包含了现点击书名传过来的id,里面if判断如果没有,进行的操作。我比较纳闷如果里面已经存在了这个id的话,代码中并没有写该怎么处理,为什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Cookie并不提供修改、删除操作。
如果要修改某个Cookie,只需要新建一个同名的Cookie,添加到response中覆盖原来的Cookie。
这段代码已经将新的ID和旧的ID拼接成新的字符串,保存在cookie中。
不知道你问的是不是这个问题。