session实现购物

来源:互联网 发布:灰色西装配马甲 知乎 编辑:程序博客网 时间:2024/06/05 09:57

ListBooksServlet.java

 

package com.jingtian.controller;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 ListBooksServlet 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.print("本站有如下商品<br/>");Map <String,Book>map=Db.getAll();for(Map.Entry<String, Book> entry:map.entrySet()){Book book=entry.getValue();String str="<a target=\"_blank\" href='"+request.getContextPath()+"/servlet/BuyServlet?id="+book.getId()+"'>"+book.getName()+"购买"+"</a><br/>";out.print(str);}System.out.println("a"+request.getContextPath()+"b");}}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;}}


 

BuyServlet.java

 

package com.jingtian.controller;import java.io.IOException;import java.io.PrintWriter;import java.util.LinkedList;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 BuyServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("UTF-8");response.setContentType("text/html;charset=UTF-8");PrintWriter out = response.getWriter();String id=request.getParameter("id");Book book=(Book)Db.getAll().get(id);HttpSession session=request.getSession();//用session中得到用户购买的商品集合List <Book> list=(List)session.getAttribute("list");if(list==null){list=new LinkedList<Book>();session.setAttribute("list", list);}list.add(book);response.sendRedirect(request.getContextPath()+"/servlet/ListCartServlet");}}

 

ListCartServlet.java

 

package com.jingtian.controller;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("您没有购买任何商品");return;}List<Book> list=(List) session.getAttribute("list");out.write("你购买了如下商品"+"<br/>");for(Book book:list){out.write(book.getName()+"<br/>");}}}


 


 

 


 

原创粉丝点击