1,IndexServlet.java 主页Servlet实现主页显示商品
import java.io.IOException; import java.io.PrintWriter; import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; //首页:列出所有书 public class IndexServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); request.getSession(); out.write("本网站有如下书:<br/>"); Set<Map.Entry<String,Book>> set = DB.getAll().entrySet(); for(Map.Entry<String,Book> me : set){ Book book = me.getValue(); String url = "/day07/servlet/BuyServlet?id=" + book.getId(); url = response.encodeURL(url); out.println(book.getName() + " <a href='"+url+"'>购买</a><br/>"); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
2,BuyServlet.java 该Servlet实现购买页面
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; 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(); /*Cookie cookie = new Cookie("JSESSIONID",session.getId()); cookie.setMaxAge(30*60); cookie.setPath("/day07"); response.addCookie(cookie);*/ List list = (List) session.getAttribute("list"); //得到用户用于保存所有书的容器 if(list==null){ list = new ArrayList(); session.setAttribute("list", list); } list.add(book); //request.getRequestDispatcher("/servlet/ListCartServlet").forward(request, response); String url = response.encodeRedirectURL("/day07/servlet/ListCartServlet");//重新URL //System.out.println(url); response.sendRedirect(url); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
3,ListCartServlet.java 实现将购买的商品通过Session保存发给ListCartServlet,显示你购买的商品
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.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); HttpSession session = request.getSession(); List<Book> list = (List) session.getAttribute("list"); if(list==null || list.size()==0){ out.write("对不起,您还没有购买任何商品!!"); return; } //显示用户买过的商品 out.write("您买过如下商品:<br>"); for(Book book : list){ out.write(book.getName() + "<br/>"); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }