java使用Cookie实现,浏览过商品记录
java使⽤Cookie实现,浏览过商品记录1.ProductDao类
public Product findProductByPid(String pid) throws SQLException {
QueryRunner runner = new DataSource());
String sql = "select * from product where pid=?";
return runner.query(sql, new BeanHandler<Product>(Product.class), pid);
}
2.ProductService类
//通过pid查商品信息
public Product findProductByPid(String pid) {
try {
return productDao.findProductByPid(pid);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
3.FindProductInfoByPidServlet类
public class FindProductInfoByPidServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private ProductService productService=new ProductService();
private CategoryService categoryService=new CategoryService();
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
中元节到底是什么节日this.doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String Parameter("pid");
Product product= productService.findProductByPid(pid);
Category category= categoryService.Cid()+"");
product.setCategory(category);
request.setAttribute("product", product);
String pids=pid;
组织部工作计划
//将pid放⼊cookie中
Cookie [] cookies=  Cookies();
if(cookies!=null){
for(Cookie cookie: cookies){
if("pids".Name())){
//将值 3-2-8存放
Value();
String [] split= pids.split("-");
List<String> aslist=Arrays.asList(split);
LinkedList<String> list=new LinkedList<String>(aslist);
ains(pid)){
自动档位介绍
list.addFirst(pid);
}else{
list.addFirst(pid);
}
StringBuffer sb=new StringBuffer();
for(int i=0;i<list.size();i++){苹果鼠标
sb.(i)).append("-");
}
sb.deleteCharAt(sb.length()-1);
String();
}
}
}
Cookie  cookie=new Cookie("pids", pids);
response.addCookie(cookie);
}
}
4.FindProductByCategroyServlet类POST⽅法添加如下代码展⽰浏览记录
家长教育孩子的方法//浏览记录
List<Product> historyList=new ArrayList<Product>();
Cookie [] Cookies();
if(cookies!=null){
for(Cookie cookie : cookies){
if("pids".Name())){
String Value();
古代春交性插图String [] splits=pids.split("-");
for (int i = 0; i < splits.length; i++) {
String pid=splits[i];
Product product= productService.findProductByPid(pid);      historyList.add(product);
}
}
}
}
request.setAttribute("historyList",historyList);

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。