您的位置:首页 > 其它

Cookie应用--显示看过的商品

2016-03-28 21:51 239 查看
package cn.itcast;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

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 CookieDemo extends HttpServlet {

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out=response.getWriter();
//输出所有商品
out.write("本站所有商品<br/>");
Map<String,Book> map=Db.getAll();
for(Entry<String, Book> entry: map.entrySet()){
Book book=entry.getValue();
out.print("<a href='/day01/servlet/CookieDemo2?id="+book.getId()+"' target='_blank'>"+book.getName()+"</a></br>");

}
//显示用户看过的商品
out.print("<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){
Book book=(Book) Db.getAll().get(id);

out.print(book.getName()+"<br/>");
}
}
}

}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public 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","java开发1","zsan1","一本好书1"));
map.put("2", new Book("2","java开发2","zsan2","一本好书2"));
map.put("3", new Book("3","java开发3","zsan3","一本好书3"));
map.put("4", new Book("4","java开发4","zsan4","一本好书4"));

}
public static Map getAll(){
return map;}

}
class Book{
private String id;
private String name;
private String author;
private String desc;

public Book() {
super();
// TODO Auto-generated constructor stub
}
public Book(String id, String name, String author, String desc) {
super();
this.id = id;
this.name = name;
this.author = author;
this.desc = desc;
}
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 getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}

}


package cn.itcast;

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

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 CookieDemo2 extends HttpServlet {

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//根据用户带过来的id显示相应的详细信息
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out=response.getWriter();
out.write("本站所有商品<br/>");
String id=request.getParameter("id");
Book book=(Book) Db.getAll().get(id);
out.write(book.getDesc());
//2.构建cookie,回写给浏览器
String cookieValue=bulidCookie(id,request);
Cookie cookie=new Cookie("bookHistory",cookieValue);
cookie.setMaxAge(30*24);
cookie.setPath("/day01");
response.addCookie(cookie);

}

private String bulidCookie(String id, HttpServletRequest request) {
// TODO Auto-generated method stub
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(cookies==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);
}
}
StringBuffer sb=new StringBuffer();
for(String bid: list){
sb.append(bid+",");

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

}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

doGet( request,response);
}

}






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