![](https://box.kancloud.cn/6096d8998f0d1cc8eba27971abf60c8d_894x508.png)
![](https://box.kancloud.cn/586c8af679768819feaf8fd0c42d389a_960x720.png)
step1.jsp
~~~
<%@ 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>
<h1>Step1:选择要购买的图书</h1>
<form action="<%= request.getContextPath() %>/processStep1" method="post">
<table border="2" cellspacing="0" cellpadding="10">
<tr>
<td>书名</td>
<td>购买</td>
</tr>
<tr>
<td>Java</td>
<td><input type="checkbox" name="book" value="Java"></td>
</tr>
<tr>
<td>JavaScript</td>
<td><input type="checkbox" name="book" value="JavaScript"></td>
</tr>
<tr>
<td>Oracle</td>
<td><input type="checkbox" name="book" value="Oracle"></td>
</tr>
<tr>
<td>JavaWEB</td>
<td><input type="checkbox" name="book" value="JavaWEB"></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Submit">
</td>
</tr>
</table>
</form>
</body>
</html>
~~~
Process1Servlet
~~~
package com.neusoft.shopping;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Process1Servlet
*/
@WebServlet("/processStep1")
public class Process1Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1.获取选中的图书的信息
String books[] = request.getParameterValues("book");
// 2.把图书信息放入到HttpSession中
request.getSession().setAttribute("books", books);
// 3.重定向到step2.jsp
response.sendRedirect(request.getContextPath()+"/20180120/step2.jsp");
}
}
~~~
step2.jsp
~~~
<%@ 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>
<h1>Step2:输入寄送地址和信用卡信息</h1>
<form action="<%= request.getContextPath() %>/processStep2" method="post">
<table border="2" cellspacing="0" cellpadding="10">
<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>
~~~
Process2Servlet
~~~
package com.neusoft.shopping;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Process2Servlet
*/
@WebServlet("/processStep2")
public class Process2Servlet 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中
request.getSession().setAttribute("customer",customer);
// 3.重定向页面到confirm.jsp
response.sendRedirect(request.getContextPath()+"/20180120/confirm.jsp");
}
}
~~~
confirm.jsp
~~~
<%@page import="com.neusoft.shopping.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");
%>
<h1>Step3:订单确认信息</h1>
<table>
<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>
~~~
- 第一章 配置和安装Tomcat
- 第二章 Servlet(一)
- 第三章 Servlet(二)
- 练习 一 . Servlet配置级获取初始化参数
- 第四章 JSP(一)
- 第五章 JSP(二)
- 第六章 MVC设计模式
- 第七章 Cookie
- 第八章 Session
- 练习 二 . 简易版购物车
- 第九章 EL表达式
- 第十章 JSTL
- 第十一章 过滤器
- 第十二章 监听器
- 第十三章 文件的上传与下载
- 复习总结
- 如何手动启动Tomcat
- 如何修改Tomcat端口号
- 如何在web.xml中配置Servlet
- Servlet生命周期
- load-on-startup参数
- Servlet映射路径
- POST和GET的区别
- JSP中9个隐式对象及功能
- 请求转发及请求重定向的区别
- JSP指令有哪些
- 简述对MVC设计模式的理解
- 简述Cookie机制
- 简述Session机制
- HttpSession的生命周期
- Cookie和Session有什么区别
- 简述创建过滤器步骤
- 过滤器经典案例--统一编码字符集
- getParameter与getAttribute的区别
- JSP页面中可以包含哪些元素
- web应用中,是如何跟踪用户的
- InteliJ创建web项目