目录
实现步骤
1. 创建 Book 封装图书信息
2. 创建 BookDB ,模拟数据库,里面保存图书
3. 提供图书的购买页面:listBookServlet
4. 添加购物车:purcharseServlet
5. 回显购物车信息:cartServlet
导航栏
Book
package chapter05.session;
public class Book {
public String id;
public String name;
//alt + shift + s
//构造有参 Generate constructors using fields...
public Book(String id, String name) {
this.id = id;
this.name = name;
}
//添加get、set方法 generate getters and setters
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;
}
}
BookDB
模拟数据库
package chapter05.session;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
public class BookDB {
//模拟数据库
private static Map<String,Book> map =new LinkedHashMap<String,Book>();
static {
map.put("1", new Book("1","第一战场指挥官"));
//选中需要复制的,alt+ctrl+pgDn
map.put("2", new Book("2","开端"));
map.put("3", new Book("3","海岛基建日常"));
map.put("4", new Book("4","吾友皆大佬"));
map.put("5", new Book("5","荣誉老王"));
map.put("6", new Book("6","赚钱真的好难哦!"));
}
//1.提供一个得到图书的方法
public static Collection<Book> getAll(){
return map.values();
}
//2.根据map的key,也就是图书id,得到图书
public static Book getBook(String id) {
return map.get(id);
}
}
listBookServlet
package chapter05.session;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 提供一个图书的界面
*/
@WebServlet("/listBookServlet")
public class listBookServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
// 处理中文乱码
response.setContentType("text/html;charset=utf-8");
//1.得到所有图书
Collection<Book> books = BookDB.getAll();
//2.图书回显到页面
PrintWriter out = response.getWriter();
out.print("以下是本网站所售卖的所有图书:"+"<br/><br/>");
//3.遍历集合,图书显示
for(Book b :books) {
//4.得到图书的名称
String url = "purcharseServlet?id="+b.getId();
out.print("图书名称 : 《"+b.getName()+"》 "+"<a href ='"+url+"'>点击购买</a><br>");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
purcharseServlet
package chapter05.session;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
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;
/**
* 添加购物车
*/
@WebServlet("/purcharseServlet")
public class purcharseServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
// 处理中文乱码
response.setContentType("text/html;charset=utf-8");
//1.获取图书信息
String id = request.getParameter("id");
//判断id是否存在
if(id==null) {
//没有 购买图书
response.sendRedirect("listBookServlet");
return;
}
System.out.println();
//2.图书已经购买
Book book = BookDB.getBook(id);
//3.把图书添加至购物车
HttpSession session = request.getSession();
List<Book> list = (List<Book>) session.getAttribute("cart");
//4.判断购物车是否存在:是不是首次购买图书
if(list==null) {//首次购买
//创建购物车
list = new ArrayList<Book>();
//把购物车放进session
session.setAttribute("cart", list);
}
//5.把图书添加到购物车
list.add(book);
//6.把请求跳转到CartServlet
//session基于自己创建的cookie完成效果:在浏览器关闭后,cookie对象还存在/目的
Cookie cookie = new Cookie("JESSIONID", session.getId());
cookie.setMaxAge(60*30);//1h
cookie.setPath("/chapter05.session");
response.addCookie(cookie);
//跳转到cartServlet里面
response.sendRedirect("/chapter03/cartServlet");
//response.sendRedirect("cartServlet");
/**
* String url = "cartServlet";
response.sendRedirect(url);
*/
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
cartServlet
package chapter05.session;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* 回显图书信息
*/
@WebServlet("/cartServlet")
public class cartServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
// 处理中文乱码
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
//1.得到购物车
List<Book> cart = null;
//2.得到用户自己的session
HttpSession session = request.getSession(false);
//3.自己定义一个标识,判断购物车里面是否有图书
boolean cartFlag = true;
if(session==null) {//购物车没有图书
cartFlag = false;
}
else {
cart = (List<Book>) session.getAttribute("cart");
if(cart==null) {
cartFlag=false;
}
}
//4.判断购物车有商品
if(!cartFlag) {//没有书
out.print("对不起,您的购物车没有图书,请去购买。");
}
else {
//有图书,遍历图书
out.print("购买图书如下: "+"<br/>");
for(Book b:cart) {
out.print("购买的图书:"+b.getName()+"<br/>");
}
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>chapter03</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>book1</servlet-name>
<servlet-class>chapter05.session.listBookServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>book1</servlet-name>
<url-pattern>/test17</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>book2</servlet-name>
<servlet-class>chapter05.session.purcharseServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>book2</servlet-name>
<url-pattern>/test18</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>book3</servlet-name>
<servlet-class>chapter05.session.cartServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>book3</servlet-name>
<url-pattern>/test19</url-pattern>
</servlet-mapping>
</web-app>
运行效果
版权声明:本文为weixin_54348877原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。