【JSP开发】session模拟购物车小例子2
程序员文章站
2024-03-20 13:29:40
...
上一篇写模拟购物车不够详细,只写了买一个商品进购物车的场景,这次提供一个完整的模拟购物车的例子(可以选取多个商品进购物车)
(上一篇的连接:http://blog.csdn.net/acmman/article/details/43450845)
购物车的主页,有商品列表,每一个商品后面都有一个"购买"按钮,用户点击商品的购
买按钮,就会将商品加入购物车(也就是将商品加入session中)
ListBookServlet:
package cn.edu.shoping;
import java.io.IOException;
import java.io.PrintWriter;
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;
//代表网站首页,列出所有书
public class ListBookServlet 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();
request.getSession();//给下面的url用
out.print("本站有如下商品:<br/>");
Map<String,Book> map=Db.getAll();
for(Map.Entry<String, Book> entry:map.entrySet()){
Book book=entry.getValue();
//为了防止用户禁止cookie
String url=response.encodeRedirectURL("/day07/BuyServlet?id="+book.getId());
out.print(book.getName()+"<a href='"+url+"' target='_blank'>购买</a><br/>");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
//模拟数据库
class Db{
private static Map<String,Book> map=new LinkedHashMap();//HashMap取
出来和存进去的顺序不是一致的(自己测试)
//类初始化代码(写在静态代码块里)
static{
map.put("1",new Book("1","JAVAWEB开发","老张","一本好书!"));
map.put("2",new Book("2","JDBC开发","老张","一本好书!"));
map.put("3",new Book("3","Spring开发","老黎","一本好书!"));
map.put("4",new Book("4","struts开发","老毕","一本好书!"));
map.put("5",new Book("5","android开发","老黎","一本好书!"));
}
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) {
super();
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;
}
}
购买的Servlet,将买到的新商品加入到List集合中,然后将List集合添加到session中
(已经有了就更新session)。
BuyServlet:
package cn.edu.shoping;
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 {
String id=request.getParameter("id");
Book book=(Book) Db.getAll().get(id);
HttpSession session=request.getSession(false);//session在主页已经得到,不需要在再创建
//手工以cookie形式发session,以解决关闭浏览器后,上次买的东西还在
String sessionid=session.getId();//详见sessio的id
Cookie cookie=new Cookie("JSESSIONID",sessionid);
cookie.setPath("/day07");
cookie.setMaxAge(30*60);
response.addCookie(cookie);
//从session中得到用户用于保存所有书的集合(购物车)
List list=(List)session.getAttribute("list");
if(list==null){
list=new ArrayList();
session.setAttribute("list", list);
}
list.add(book);
session.setAttribute("list", list);
//转发(用转发只要刷新,就会买一本书,相当于重新执行该页面,重新发送了一次指令)
//原因,转发时URL没有换,URL中带有参数,相当于又一次传参
//所以不要用转发,要用重定向
//request.getRequestDispatcher("/ListCarServlet").forward(request, response);
//重定向,为了以后项目会改名字,故项目名字最好不要写死在程序中,用request.getContextPath()代替项目名
String url=response.encodeRedirectURL(request.getContextPath()+"/ListCarServlet");
response.sendRedirect(url);
}
public void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
doGet(request,response);
}
}
购物车的显示Servlet,在session中取出用户购买的商品的List列表,然后输出在界面
上。如果用户没有购买东西,就会检测到session中对应属性是null的,然后就会提醒
用户没有购买商品。
ListCarServlet:
package cn.edu.shoping;
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 ListCarServlet 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();
if(session==null){
out.print("您没有购买任何商品!<br/>");
return;
}
out.print("您购买了如下商品:<br/>");
List<Book> list=(List) session.getAttribute("list");
for(Book book:list){
out.print(book.getName()+"<br/>");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
至此,实现了一个完整的购物网站的购物车小样例 转载请注明出处:http://blog.csdn.net/acmman