会话管理技术实战(购物车案例)
会话管理技术实战(购物车案例)**[1].创建Book封装图书信息:Book[2].创建BookDB,模拟数据库,里面是保存图书[3].提供图书的购买页面:ListBookServlet[4].添加购物车功能:PurcharseServlet[5].回显购物车图书信息:CartServlet**如果浏览器禁用了cookie,咱们的购物车功能就不能实现了,因为session...
·
会话管理技术实战(购物车案例)
**[1].创建Book封装图书信息:Book
[2].创建BookDB,模拟数据库,里面是保存图书
[3].提供图书的购买页面:ListBookServlet
[4].添加购物车功能:PurcharseServlet
[5].回显购物车图书信息:CartServlet**
如果浏览器禁用了cookie,咱们的购物车功能就不能实现了,因为session是基于cookie实现的。
解决方案:提示用户开启cookie
1.Book.java(封装图书信息)
package cn.itcast.chapter05.session.example01;
public class Book {
private String id;
private String name;
public Book() {
}
public Book(String id, String name) {
this.id = id;
this.name = name;
}
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;
}
}
2.BookDB.java(模拟数据库功能)
package cn.itcast.chapter05.session.example01;
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","javaWeb"));
map.put("2", new Book("2","jdbc入门"));
map.put("3", new Book("3","java基础"));
map.put("4", new Book("4","struts框架"));
map.put("5", new Book("5","hibernate框架"));
}
//1.得到所有图书
public static Collection<Book> getAll(){
return map.values();
}
//2.根据map的key,也就是图书的id得到某本图书
public static Book getBook(String id) {
return map.get(id);
}
}
3.ListBookServlet.java(图书购买页面)
package cn.itcast.chapter05.session.example01;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 提供一个购买图书的页面
*/
public class ListBookServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ListBookServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//处理响应中文的乱码
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 name=b.getName();
String id=b.getId();
//提供一个购买图书的链接
String url="<a href='/chapter05/purcharse?id="+id+"'>点击购买</a>";
out.print("图书名称: "+name+" "+url+"<br/><br/>");
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
4.添加购物车 PurcharseServlet
package cn.itcast.chapter05.session.example01;
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 PurcharseServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public PurcharseServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
//1.获取图书信息
String id=request.getParameter("id");
//判断id是否存在
if(id==null) {//说明没有购买图书
response.sendRedirect("/chapter05/list");//回到购买图书的页面
return;
}
//2.图书已经购买了
Book book=BookDB.getBook(id);
//3.把图书添加到购物车
HttpSession session=request.getSession();
List<Book> list=(List<Book>) session.getAttribute("cart");
//4.判断购物车是否存在:是不是首次购买图书
if(list==null) {//首次购买book
list=new ArrayList<Book>();
session.setAttribute("cart", list);
}
//5.把图书添加到购物车
list.add(book);
//6.把请求跳转到CartServlet
//session基于自己创建的cookie来完成效果,在浏览器关闭后,cookie对象还存在,就能找到session对象
Cookie cookie=new Cookie("JESSIONID",session.getId());
cookie.setMaxAge(60*60*24);
cookie.setPath("/chapter05");
response.addCookie(cookie);
//跳转到CartServlet里面
response.sendRedirect("/chapter05/cart");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
5.回显购物车图书信息: CartServlet.java
package cn.itcast.chapter05.session.example01;
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;
/**
* Servlet implementation class CartServlet
*/
public class CartServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();
//1.得到购物车
List<Book> cart=null;
//2.得到用户自己的session
HttpSession session=request.getSession(false);
//3.自己定义一个flag,判断购物车里面是否有图书
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/>");
}
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
实现:
1.启动服务器:访问http://localhost:8080/chapter05/list
2.随便点击购买基本图书 这里我点击javaWeb 和jdbc入门(这里跳转到了CartServlet)
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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>chapter05</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>
<description></description>
<display-name>LastAccessServlet</display-name>
<servlet-name>LastAccessServlet</servlet-name>
<servlet-class>cn.itcast.chapter05.cookie.example.LastAccessServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LastAccessServlet</servlet-name>
<url-pattern>/last</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>ListBookServlet</display-name>
<servlet-name>ListBookServlet</servlet-name>
<servlet-class>cn.itcast.chapter05.session.example01.ListBookServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ListBookServlet</servlet-name>
<url-pattern>/list</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>PurcharseServlet</display-name>
<servlet-name>PurcharseServlet</servlet-name>
<servlet-class>cn.itcast.chapter05.session.example01.PurcharseServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PurcharseServlet</servlet-name>
<url-pattern>/purcharse</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>CartServlet</display-name>
<servlet-name>CartServlet</servlet-name>
<servlet-class>cn.itcast.chapter05.session.example01.CartServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CartServlet</servlet-name>
<url-pattern>/cart</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>IndexServlet</display-name>
<servlet-name>IndexServlet</servlet-name>
<servlet-class>cn.itcast.chapter05.example02.IndexServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>IndexServlet</servlet-name>
<url-pattern>/index</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>LoginServlet</display-name>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>cn.itcast.chapter05.example02.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>LogOutServlet</display-name>
<servlet-name>LogOutServlet</servlet-name>
<servlet-class>cn.itcast.chapter05.example02.LogOutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LogOutServlet</servlet-name>
<url-pattern>/logOut</url-pattern>
</servlet-mapping>
</web-app>
更多推荐
所有评论(0)