Session案例:简易的购物车
三个jsp和两个Servlet组成:在WebContent下边建立一个shoppingcart文件夹,将三个jsp文件放在里面:
1.建立一个step1.jsp文件,出现一个表格,是一个复选框,可以选择要购买的书籍,完毕后,点击Submit,跳转到Servlet类 ProcessStep1Servlet里面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>step1: 选择要购买的书籍!</h2>
<form action="<%= request.getContextPath() %>/processStep1Servlet" method="post">
<table border="1" cellpadding="10" cellspacing="0">
<tr>
<th>书名</th>
<th>点击购买</th>
</tr>
<tr>
<td>Oracle</td>
<td><input type="checkbox" name="book" value="Oracle"/></td>
</tr>
<tr>
<td>java</td>
<td><input type="checkbox" name="book" value="java"/></td>
</tr>
<tr>
<td>C++</td>
<td><input type="checkbox" name="book" value="C++"/></td>
</tr>
<tr>
<td>mysql</td>
<td><input type="checkbox" name="book" value="mysql"/></td>
</tr>
<tr>
<td>WEB</td>
<td><input type="checkbox" name="book" value="WEB"/></td>
</tr>
<tr>
<td colspan="2"> <input type="submit" value="Submit"/></td>
</tr>
</table>
</form>
</body>
</html>
2.建立一个Servlet类 ProcessStep1Servlet,里面有doPost方法,可实现session方法(好处就是获取用户选择书名的id,保存到后边再使用),获取用户选择的书名,使绝对路径的方法重定向到step1.jsp页面; response.sendRedirect(request.getContextPath()+"/shoppingcart/step2.jsp");
public class ProcessStep1Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1. 获取选中的图书的信息
String [] books = request.getParameterValues("book");
//2. 把图书信息放入到 HttpSession 中
request.getSession().setAttribute("books", books);
//2. 重定向页面到 shoppingcart/step-2.jsp
System.out.println(request.getContextPath() + "/shoppingcart/step-2.jsp");
response.sendRedirect(request.getContextPath() + "/shoppingcart/step-2.jsp");
}
}
3.在step2.jsp页面,是用户输入个人信息页面,付款,发送货物需要的信息,然后跳转到Servlet类 processStep2Servlet 对用户的信息封装到customer类里面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h2>step2: 请输入寄送地址和信用卡</h2> <form action="<%= request.getContextPath() %>/processStep2Servlet" method="post"> <table border="1" cellpadding="10" cellspacing="0"> <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>联系电话:</td> <td><input type="text" name="phone"/></td> </tr> <tr > <td colspan="2">信用卡信息:</td> </tr> <tr> <td>信用卡种类</td> <td> <input type="radio" name="cardType" value="nong"/>nong <input type="radio" name="cardType" value="gong"/>gong </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>
4.建立一个Servlet类,里面有doPost方法,对用户的信息封装到customer类里面,session实例,保存用户的id信息,便于后边输出,然后重定向到confirm.jsp页面 ,跳转一面使用了绝对路径:response.sendRedirect(request.getContextPath()+"/shoppingcart/confirm.jsp");
public class ProcessStep2Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1. 获取请求参数: name, address, cardType, card
String name = request.getParameter("name");
String address = request.getParameter("address");
String cardType = request.getParameter("cardType");
String card = request.getParameter("card");
Customer customer = new Customer(name, address, cardType, card);
//2. 把请求信息存入到 HttpSession 中
HttpSession session = request.getSession();
session.setAttribute("customer", customer);
//3. 重定向页面到 confirm.jsp
response.sendRedirect(request.getContextPath() + "/shoppingcart/confirm.jsp");
}
}
5.confirm.jsp页面,使用了session实例,将前边的用户选择的书籍和用户输入的个人信息,输出到这个页面
<%@page import="com.lanqiao.javatest.Customer"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% Customer customer=(Customer)session.getAttribute("customer"); String [] books=(String [])session.getAttribute("books"); %> <h2>信息表单</h2> <table> <tr> <td>顾客姓名:</td> <td><%=customer.getName() %></td> </tr> <tr> <td>顾客地址:</td> <td><%=customer.getAddress() %></td> </tr> <tr> <td>顾客联系电话:</td> <td><%=customer.getPhone() %></td> </tr> <tr> <td>顾客卡类型:</td> <td><%=customer.getCardType() %></td> </tr> <tr> <td>顾客的卡号:</td> <td><%=customer.getCard() %></td> </tr> <tr> <td>您购买的书:</td> <td> <% for(String book:books){ out.print(book); out.print("\t\t"); } %> </td> </tr> </table> </body> </html>
6.lib下边的web.xml文件
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <servlet> <description></description> <display-name>ProcessStep1Servlet</display-name> <servlet-name>ProcessStep1Servlet</servlet-name> <servlet-class>com.lanqiao.javatest.ProcessStep1Servlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ProcessStep1Servlet</servlet-name> <url-pattern>/processStep1Servlet</url-pattern> </servlet-mapping> <servlet> <description></description> <display-name>ProcessStep2Servlet</display-name> <servlet-name>ProcessStep2Servlet</servlet-name> <servlet-class>com.lanqiao.javatest.ProcessStep2Servlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ProcessStep2Servlet</servlet-name> <url-pattern>/processStep2Servlet</url-pattern> </servlet-mapping> </web-app>
7.Customer封装类:
package com.lanqiao.javatest; public class Customer { private String name; private String address; private String phone; private String cardType; private String card; public Customer() { super(); } public Customer(String name, String address, String phone, String cardType, String card) { super(); this.name = name; this.address = address; this.phone = phone; this.cardType = cardType; this.card = card; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getCardType() { return cardType; } public void setCardType(String cardType) { this.cardType = cardType; } public String getCard() { return card; } public void setCard(String card) { this.card = card; } @Override public String toString() { return "Customer [name=" + name + ", address=" + address + ", phone=" + phone + ", cardType=" + cardType + ", card=" + card + "]"; } }