This article describes the implementation method of a Session-based shopping store in Java Web development. Share it with everyone for your reference, the details are as follows:
package cn.com.shopping;import java.io.IOException;import java.util.ArrayList;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;//Complete the purchase public class BuyServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String id=request.getParameter("id "); Book book=(Book)Db.getAll().get(id); //Add the session solution when turning off cookies //The solution when blocking session HttpSession session=request.getSession(false); // Get the user's collection of all books saved from the session (shopping cart) List list=(List)session.getAttribute("list"); if(list==null) { list=new ArrayList(); session.setAttribute("list", list); } list.add(book); String url=response.encodeRedirectURL("/Session/SessionCountDemo"); response.sendRedirect(url); } protected void doPost( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); }}package cn.com.shopping;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;import javax.servlet.http.HttpSession;//Display book public class ListBookServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected 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(); out.print("Our store has the following products:<br/>"); Map<String,Book> map=Db.getAll(); for(Map.Entry<String, Book> entry:map.entrySet()) { Book book=entry.getValue(); String url=response.encodeURL("/Session/BuyServlet?id ="+book.getId()); out.print(book.getName()+"<a href='"+url+"' target='_blank' >Buy</a><br/>"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); }}//Db as database class Db{ private static Map <String,Book> map=new LinkedHashMap(); static { map.put("1", new Book("1","Java WEB Development","WY","Good Book")); map.put("2", new Book("2","WEB Development","zt","General" )); map.put("3", new Book("3","Programming","df","Better Book")); map.put("4", new Book("4","Computer components","as","Generally good books")); map.put("5", new Book("5","Compilation principles","ty","Very good book")); map.put("6", new Book("6","Network Maintenance","hj","very good book")); } public static Map getAll() { return map; }} //book class Book{ private String id; private String name; private String author; private String description; public Book() { super(); // TODO Auto-generated constructor stub } 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; }}package cn.com.shopping;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 SessionCountDemo extends HttpServlet { private static final long serialVersionUID = 1L; protected 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.write("You have not Buy any product! "); return; } out.write("You purchased the following products:"); List<Book> list=(List) session.getAttribute("list"); for(Book book:list) { out.write (book.getName()); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); }}I hope this article will be helpful to everyone in Java web programming.