如何得到商品浏览过的信息
package cn.csdn.web.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.net.URLEncoder;
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;
import cd.csdn.web.domain.Goods;
import cn.csdn.web.service.GoodsService;
import cn.csdn.web.service.GoodsServiceImpl;
public class GoodsListServlet extends HttpServlet {
//由这个servlet创建首页
private GoodsService gService=new GoodsServiceImpl();
String str="";
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//显示所有的商品信息
List<Goods> entities=gService.findAll();
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-type", "text/html;charset=UTF-8");
PrintWriter out=response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>Heyley的网站首页</TITLE></HEAD>");
out.println(" <BODY>");
out.print("<table border=\"1\">");
out.print("<tr>");
out.print("<th>序号</th><th>名称</th><th>价格</th>");
out.print("</tr>");
for(Goods entity:entities){
out.print("<tr>");
out.print("<td>"+entity.getId()+"</td><td><a href=\"detail.do?id="+entity.getId()+"\">"+entity.getName()+"</td><td>"+entity.getPrice()+"</td>");
out.print("</tr>");
}
out.print("</table>");
//创建一个cookie
//str=GoodsDetailServlet.str();
//str=URLEncoder.encode(str,"utf-8");
//Cookie ck=new Cookie("lastTime",str);
//Cookie ck1=new Cookie("abc","123");
//设置有效时间为1天
//ck.setMaxAge(3600*24);
//ck.setPath("/2011-10-24/list.do");
//response.addCookie(ck);
//response.addCookie(ck1);
//显示浏览过的商品信息
//声明浏览过的商品的value值
String value=null;
//获取cookie
Cookie cookies[]=request.getCookies();
//遍历cookie信息
for(int i=0;cookies!=null&&i<cookies.length;i++){
//浏览过商品的cookie cookie name value
if("goodsHistory".equals(cookies[i].getName())){
value=cookies[i].getValue();
}
}
//判断 value 1 2_1 3_2_1 最大值是3个
out.println("你最近看过的产品有:");
if(value!=null){
//第一步:拆分
String ids[]=value.split("\\_");
out.print("<table>");
out.print("<tr><th>商品名称</th></tr>");
for(int i=0;i<ids.length;i++){
out.print("<tr>");
out.print("<td>");
out.print(gService.findByid(Integer.parseInt(ids[i])).getName());
out.print("</td>");
out.print("</tr>");
}
out.print("</table>");
}
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
效果:
序号 | 名称 | 价格 |
---|---|---|
1 | 惠普 | 4000.0 |
2 | 诺基亚nc99 | 3500.0 |
3 | 三星 | 2000.0 |
当点击 惠普时:
package cn.csdn.web.servlet;
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;
import cd.csdn.web.domain.Goods;
import cn.csdn.web.service.GoodsService;
import cn.csdn.web.service.GoodsServiceImpl;
public class GoodsDetailServlet extends HttpServlet {
private GoodsService gService=new GoodsServiceImpl();
//static String name="";
//static int a=0;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id=request.getParameter("id");
Integer numId=Integer.parseInt(id);
Goods entity=gService.findByid(numId);
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-type", "text/html;charset=UTF-8");
PrintWriter out=response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>Heyley的网站首页</TITLE></HEAD>");
out.println(" <BODY>");
out.print("<table border=\"1\">");
out.print("<tr>");
out.print("<th>序号</th><th>名称</th><th>价格</th><th>图片</th><th>描述信息</th>");
out.print("</tr>");
out.print("<td>"+entity.getId()+"</td><td>"+entity.getName()+"</td><td>"+entity.getPrice()+"</td>"+"<td>"+"<img src="+entity.getImg()+">"+"</td>"+"<td>"+entity.getRemark()+"</td>");
out.print("</table>");
out.println(" </BODY>");
out.println("</HTML>");
/*name=entity.getName();
name=URLEncoder.encode(name,"utf-8");
Cookie cka=new Cookie("abc"+a,name);
response.addCookie(cka);
a++;*/
//发送cookie信息
String value=makeCookieValue(request,id);
//产生一个Cookie对象
Cookie cookie=new Cookie("goodsHistory",value);
cookie.setMaxAge(60*60);
//cookie.setPath("/2011-10-24");
//相应
response.addCookie(cookie);
out.flush();
out.close();
}
private String makeCookieValue(HttpServletRequest request, String id) {
//声明返回值变量
String goodsHistory=null;
//第一步:获取cookie
Cookie cookie[]=request.getCookies();
for(int i=0;cookie!=null&&i<cookie.length;i++){
if("goodsHistory".equals(cookie[i].getName())){
goodsHistory=cookie[i].getValue();
}
}
//第一次访问234
if(goodsHistory==null){
goodsHistory=id;
}else{
LinkedList<String> list=new LinkedList<String>(Arrays.asList(goodsHistory.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 ids:list){
sb.append(ids+"_");
}
sb.deleteCharAt(sb.length()-1);
goodsHistory=sb.toString();
}
return goodsHistory;
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
记录浏览过的商品:
序号 | 名称 | 价格 | 图片 | 描述信息 |
---|---|---|---|---|
1 | 惠普 | 4000.0 | null |