ListBookServlet

package cn.itcast;

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

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ListBookServlet extends HttpServlet {
   public void doGet(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException {
      /*功能说明:实际使用较多的还是通过cookie实现
       * 1,ListBookServlet列出所有商品,并显示
       * 点击本页链接,跳转到BuyServlet(带id过去)
       * 2,BuyServlet将用户点击的书(通过id取得)存入集合,
       * 并且将集合存入session,然后跳转到ListCartServlet
       * 3,ListCartServlet显示购物车,只取出session中的list用于checkout
       */
       //前三句防乱码+获取流
       response.setCharacterEncoding("UTF-8");
       response.setContentType("text/html;charset=UTF-8");
       PrintWriter out=response.getWriter();
       out.write("首页所有商品如下:<br />");
       //如果用户禁用cookie,那么首先要得cookie再对url编码!
       HttpSession session=request.getSession();
       Map<String,Book> map=Db.getAll();
       for (Map.Entry<String,Book> entry : map.entrySet()) {
         Book book=entry.getValue();
         String id=book.getId();
         String name=book.getName();
         String url="/day04/servlet/BuyServlet?id="+id;
         //如果用户禁用cookie,那么要先得session再对url编码!
         url=response.encodeURL(url);
         out.print(name+"<a href='"+url+"' target='_blank'>购买</a><br/>");
      }
   }
   public void doPost(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException {
      doGet(request, response);
   }
}
//写个Db类模拟数据库:容器双列(有检索要求)
class Db{
  //购物有序,内部维护一个map集合
  private static Map<String,Book> map=new LinkedHashMap();
  //开始就有些书
  static{
    map.put("1", new Book("1","javascript","老张","一本好书"));
    map.put("2", new Book("2","javase","老毕","一本好书"));
    map.put("3", new Book("3","javaweb","老方","一本好书"));
    map.put("4", new Book("4","android","老黎","一本好书"));
  }
  //提供一个取得所有Map方法
  public static Map getAll(){
    return map;
  }
}
//写个Book类封装数据
class Book implements Serializable{
  private String id;
  private String name;
  private String author;
  private String description;
  //生成无参构造函数,带参构造函数,Getters and Setters
  public Book(String id, String name, String author, String description) {
    super();
    this.id = id;
    this.name = name;
    this.author = author;
    this.description = description;
  }
  public Book() {
    super();
  }
  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;
  }
}


BuyServlet

package cn.itcast;

import java.io.IOException;
import java.util.ArrayList;
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 javax.servlet.http.HttpSession;
public class BuyServlet extends HttpServlet {
   public void doGet(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException {
      /*功能说明:
       * 1,ListBookServlet列出所有商品,并显示
       * 点击本页链接,跳转到BuyServlet(带id过去)
       * 2,BuyServlet将用户点击的书(通过id取得)存入集合,
       * 并且将集合存入session,然后跳转到ListCartServlet
       * 3,ListCartServlet显示购物车,只取出session中的list用于checkout
       */
      String id=request.getParameter("id");
      //通过id得用户购买的book,并用list组织,再将list加入session
      //最后再跳转到显示购物车ListCartServlet
      Book book=(Book) Db.getAll().get(id);
      HttpSession session=request.getSession();
      //下面5句代码,增加session回写的cookie的有效期
      String jsessionid=session.getId();
      Cookie cookie=new Cookie("JSESSIONID",jsessionid);
      cookie.setPath("/day04");
      cookie.setMaxAge(1*60*30);//30分钟
      response.addCookie(cookie);
      //先判断有没有list,没有再创建
      List list=(List) session.getAttribute("list");
      if (list==null) {
         list=new ArrayList<Book>();
         session.setAttribute("list", list);
      }
      list.add(book);
      //跳转到购物车,使用重定向,我帮你找别人,仅一次请求
      String url="/day04/servlet/ListCartServlet";
      //如果用户禁用cookie,那么要先得session再对url编码!
      url=response.encodeRedirectURL(url);
      response.sendRedirect(url);
      //不能用转发,我让你去找别人,会再发一次请求
      //request.getRequestDispatcher("/servlet/ListCartServlet").forward(request, response);
   }
   public void doPost(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException {
      doGet(request, response);
   }
}



ListCartServlet

package cn.itcast;

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

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class ListCartServlet 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();
       
      HttpSession session=request.getSession(false);
      if (session==null) {
         out.write("您尚未购买任何商品<br />");
         return;
      }
      out.write("您购买的商品如下:<br />");
      List<Book> list=(List) session.getAttribute("list");
      for (Book book : list) {
         String id=book.getId();
         String name=book.getName();
         String author=book.getAuthor();
         String description=book.getDescription();
         out.print(id+" "+name+" "+author+" "+description+"<br/>");
      }
   }

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

}



Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐