step1.jsp
<%-- Created by IntelliJ IDEA. User: dell Date: 2019/7/10 Time: 16:59 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h4>Step1:选择要购买的书籍</h4> <form action="<%=request.getContextPath()%>/Step1Servlet" method="post"> <table border="1"> <tr> <td>书名</td> <td>购买</td> </tr> <tr> <td>Java</td> <td><input type="checkbox" name="book" value="Java"></td> </tr> <tr> <td>Oracle</td> <td><input type="checkbox" name="book" value="Oracle"></td> </tr> <tr> <td>Strust</td> <td><input type="checkbox" name="book" value="Strust"></td> </tr> <tr> <td colspan="2"> <input type="submit" value="Submit"> </td> </tr> </table> </form> </body> </html>
step1.jsp效果图:
Step1Servlet
import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class Step1Servlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //1.获取选中的图书的信息 String [] books = req.getParameterValues("book"); //2.把图书信息放入到HttpSession中 req.getSession().setAttribute("books",books); //3.重定向页面到shopcart/step2.jsp resp.sendRedirect(req.getContextPath() + "/shopcart/step2.jsp"); } }
Step2.jsp
<%-- Created by IntelliJ IDEA. User: dell Date: 2019/7/10 Time: 17:03 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h4>Step2:请输入寄送地址与信用卡信息</h4> <form action="<%=request.getContextPath()%>/Step2Servlet" method="post"> <table cellpadding="10" cellspacing="0" border="1"> <tr> <td colspan="2">寄送信息</td> </tr> <tr> <td>姓名:</td> <td><input type="text" name="name"></td> </tr> <tr> <td>寄送地址:</td> <td><input type="text" name="address"></td> </tr> <tr> <td colspan="2">信用卡信息</td> </tr> <tr> <td>种类:</td> <td> <input type="radio" name="cardType" value="Visa">Visa <input type="radio" name="cardType" value="Master">Master </td> </tr> <tr> <td>卡号:</td> <td> <input type="text" name="card"> </td> </tr> <tr> <td colspan="2"><input type="submit" value="Submit"></td> </tr> </table> </form> </body> </html>
step2.jsp效果图
Step2Servlet
import com.demo.javaweb.Customer; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; public class Step2Servlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //1.获取请求参数 String address = req.getParameter("address"); byte [] bytes1 = address.getBytes("ISO8859-1"); address = new String(bytes1,"UTF-8"); String name = req.getParameter("name"); byte [] bytes2 = name.getBytes("ISO8859-1"); name = new String(bytes2,"UTF-8"); Customer customer = new Customer(); customer.setName(name); customer.setAddress(address); customer.setCardType(req.getParameter("cardType")); customer.setCard(req.getParameter("card")); HttpSession session = req.getSession(); System.out.println(customer); session.setAttribute("customer",customer); resp.sendRedirect(req.getContextPath()+"/shopcart/submit.jsp"); } }
submit.jsp
<%-- Created by IntelliJ IDEA. User: dell Date: 2019/7/10 Time: 17:12 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" import="com.demo.javaweb.Customer" %> <html> <head> <title>Title</title> </head> <body> <% Customer customer = (Customer) session.getAttribute("customer"); String[] books = (String[]) session.getAttribute("books"); %> <table cellspacing="0" cellpadding="10" border="1"> <tr> <td>顾客姓名:</td> <td><%= customer.getName()%></td> </tr> <tr> <td>顾客地址:</td> <td><%= customer.getAddress()%></td> </tr> <tr> <td>种类:</td> <td><%= customer.getCardType()%></td> </tr> <tr> <td>卡号:</td> <td><%= customer.getCard()%></td> </tr> <tr> <td>BOOKS:</td> <td> <% for (String book:books ) { out.print(book); out.print("<br>"); } %> </td> </tr> </table> </body> </html>
submit.jsp效果图
层级关系: