您的位置:首页 > 其它

模拟显示已浏览商品

2016-01-07 16:44 246 查看
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//显示商品  已浏览商品
public class IndexServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter write = response.getWriter();
//输出网站商品
write.print("网站书籍如下<br/>");
for(Map.Entry<String, Book> entry : Db.getAll().entrySet()){
Book book = entry.getValue();
write.print("<a href='/Servlet/ShowBook?id="+book.getId()+"' target='_blank'>"+book.getName()+"</a></br>");
}

//显示用户曾看过的商品
write.print("<br/><br/><br/>您曾经看过什么商品<br/>");
Cookie cookies[] = request.getCookies();
for(int i = 0;cookies!=null&&i<cookies.length;i++){
if(cookies[i].getName().equals("bookHistory")){
String[] ids = cookies[i].getValue().split("\\,");
for(String id : ids){
write.print(Db.getAll().get(id).getName()+"<br/>");
}
}
}

}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}

}
//模仿数据库
class Db{
private static Map<String,Book> map = new LinkedHashMap<>();
static {
map.put("1", new Book("1","Head First Java","author1","nice"));
map.put("2", new Book("2","Head First jsp&servlet","author2","nice"));
map.put("3", new Book("3","think in java","author3","nice"));
map.put("4", new Book("4","javaweb","author4","nice"));
map.put("5", new Book("5","android","author5","nice"));
}
public static Map<String,Book> getAll(){
return map;
}
}
//book实体类
class Book{
private String id;
private String name;
private String author;
private String description;

public Book() {
super();
}
public Book(String id, String name, String author, String description) {
super();
this.id = id;
this.name = name;
this.author = author;
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 getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.LinkedList;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//显示商品详细信息

public class ShowBook extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter write = response.getWriter();
// 显示商品
String id = request.getParameter("id");
Book book = new Book();
book = Db.getAll().get(id);
write.print(book.getId() + "<br/>");
write.print(book.getName() + "<br/>");
write.print(book.getAuthor() + "<br/>");
write.print(book.getDescription() + "<br/>");
// 回写cookie
String cookieValue = bulidCookie(request, id);
Cookie cookie = new Cookie("bookHistory", cookieValue);
cookie.setMaxAge(1 * 30 * 24 * 60 * 60);
cookie.setPath("/Servlet");
response.addCookie(cookie);
}

// 创建cookie
private String bulidCookie(HttpServletRequest request, String id) {
String bookHistory = null;
Cookie cookies[] = request.getCookies();
for (int i = 0; cookies != null && i < cookies.length; i++) {
if (cookies[i].getName().equals("bookHistory")) {
bookHistory = cookies[i].getValue();
}
}
if (bookHistory == null) {
return id;
}
LinkedList<String> list = new LinkedList<>(Arrays.asList(bookHistory.split("\\,")));

/*if (list.contains(id)) {
list.remove(id);
list.addFirst(id);
} else {
if (list.size() >= 3) {
list.removeLast();
list.addFirst(id);
} else {
list.addFirst(id);
}
} */
//以下代码实现追加cookie逻辑
if (list.contains(id)) {
list.remove(id);
} else if (list.size() >= 3) {
list.removeLast();
}

list.addFirst(id);

StringBuffer sb = new StringBuffer();
for (String bookId : list) {
sb.append(bookId + ",");
}

return sb.deleteCharAt(sb.length() - 1).toString();
}

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: