当前位置:编程学习 > 网站相关 >>

显示商品历史浏览记录

废话不多说,直接上代码

package cn.itcast.cookie;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
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;
//清除cookie

public class CookieDemo3 extends HttpServlet {

     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(Map.Entry<String, Book> entry:map.entrySet()){
               Book book=entry.getValue();
               out.print("<a target=\"_blank\" href='/day07/servlet/cookieDemo4?id="+book.getId()+"'>"+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("\\,");//2,3,1
                    for(String id:ids){
                         Book book=(Book) Db.getAll().get(id);
                         out.print("<a target=\"_blank\" href='/day07/servlet/cookieDemo4?id="+book.getId()+"'>"+book.getName()+"</a><br/>");
                    }
               }
          }
     }
}

class Db{
     private static Map<String,Book> map=new LinkedHashMap<String,Book>();
     static {
          map.put("1", new Book("1","JavaWeb开发","老k","一本好书"));
          map.put("2", new Book("2","jdbc开发","老张","一本好书"));
          map.put("3", new Book("3","spring开发","老li","一本好书"));
          map.put("4", new Book("4","struts开发","老张","一本好书"));
          map.put("5", new Book("5","android开发","老bi","一本好书"));
     }
     public static Map getAll(){
          return map;
     }
    
}

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) {
          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;
     }    
}

package cn.itcast.cookie;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Date;
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;
//显示详细信息的servlet

public class Coo

补充:综合编程 , 其他综合 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,