Session机制二(简易购物车案例)
一:案例一(简易购物车)
1.目录结构

2.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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2>Step1:选择要购买的图书</h2>
<form action="<%=request.getContextPath() %>/processStep1" method="post">
<table border="1" cellpadding="10" cellspacing="0">
<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>Struct2</td>
<td><input type="checkbox" name="book" value="Struct2"></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Submit"/>
</td>
</tr>
</table>
</form>
</body>
</html>
3.ProcessStep1Servlet.java
package javaweb; 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 ProcessStep1Servlet
*/
@WebServlet("/processStep1")
public class ProcessStep1Servlet 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 {
//获取选中的图书的信息
String[] books=request.getParameterValues("book"); //将图书放进HttpSession中
request.getSession().setAttribute("books", books); //重定向页面到shopcart/step2.jsp
//这里使用的绝对路径,同理,在jsp里也需要使用相对路径
response.sendRedirect(request.getContextPath()+"/shopCart/step2.jsp");
} }
4.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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2>Step2:请输入信用卡的信息</h2>
<form action="<%=request.getContextPath()%>/processStep2" 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 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="cardNum" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Submit" /></td>
</tr>
</table>
</form>
</body>
</html><%@ 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2>Step2:请输入信用卡的信息</h2>
<form action="<%=request.getContextPath()%>/processStep2" 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 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="cardNum" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Submit" /></td>
</tr>
</table>
</form>
</body>
</html>
5.ProcessStep2Servlet.java
package javaweb; 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 ProcessStep2Servlet
*/
@WebServlet("/processStep2")
public class ProcessStep2Servlet 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 {
//获取请求参数name address cardType cardNum
String name=request.getParameter("name");
String address=request.getParameter("address");
String cardType=request.getParameter("cardType");
String cardNum=request.getParameter("cardNum"); //将参入放进一个对象中
Customer customer=new Customer(name, address, cardType, cardNum); //将请求信息存入HtppSession中
request.getSession().setAttribute("customer", customer); //重定向到confirm.jsp
response.sendRedirect(request.getContextPath()+"/shopCart/confirm.jsp");
} }
6.conform.jsp
<%@page import="javaweb.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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
Customer customer=(Customer)session.getAttribute("customer");
String[] books=(String[])session.getAttribute("books");
%>
<h2>Step3:请确认</h2>
<table>
<tr>
<td>顾客姓名</td>
<td><%=customer.getName() %></td>
</tr>
<tr>
<td>顾客地址</td>
<td><%=customer.getAddress() %></td>
</tr>
<tr>
<td>顾客卡号</td>
<td><%=customer.getCardNum() %></td>
</tr>
<tr>
<td>顾客类型</td>
<td><%=customer.getCardType() %></td>
</tr>
<tr>
<td>Books</td>
<td>
<%
for(String book:books){
out.print(book);
out.print("<br>");
}
%>
</td>
</tr>
</table>
</body>
</html>
7.补充简单bean,Customer.java
package javaweb;
public class Customer {
private String name;
private String address;
private String cardType;
private String cardNum;
public Customer() {
}
public Customer(String name, String address, String cardType, String cardNum) {
super();
this.name = name;
this.address = address;
this.cardType = cardType;
this.cardNum = cardNum;
}
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 getCardType() {
return cardType;
}
public void setCardType(String cardType) {
this.cardType = cardType;
}
public String getCardNum() {
return cardNum;
}
public void setCardNum(String cardNum) {
this.cardNum = cardNum;
}
}
8.展示结果

二:
Session机制二(简易购物车案例)的更多相关文章
- [转]用 Jsp 的 Session 机制编写的购物车程序
一.构建的商品类 //写一个Goods类,并定义商品的各个属性,返回商品属性的方法,以及商品对象进行比较的方法//Goods.java package com.viita.Shop; public c ...
- DOM操作相关案例 模态对话框,简易留言板,js模拟选择器hover,tab选项卡,购物车案例
1.模态框案例 需求: 打开网页时有一个普通的按钮,点击当前按钮显示一个背景图,中心并弹出一个弹出框,点击X的时候会关闭当前的模态框 代码如下: <!DOCTYPE html> <h ...
- C#基础知识之理解Cookie和Session机制
会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通过在服务器端 ...
- Cookie/Session机制详解
会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通过在服务器端 ...
- 理解Cookie和Session机制(转)
目录[-] Cookie机制 什么是Cookie 记录用户访问次数 Cookie的不可跨域名性 Unicode编码:保存中文 BASE64编码:保存二进制图片 设置Cookie的所有属性 Cookie ...
- [转]Cookie/Session机制详解
原文地址:http://blog.csdn.net/fangaoxin/article/details/6952954 会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用 ...
- 关于cookie的文章(cookie与session机制)
会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通过在服务器端 ...
- 理解Cookie和Session机制
转载: 理解Cookie和Session机制 会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录 ...
- Cookie/Session机制具体解释
会话(Session)跟踪是Web程序中经常使用的技术,用来跟踪用户的整个会话.经常使用的会话跟踪技术是Cookie与Session.Cookie通过在client记录信息确定用户身份,Session ...
随机推荐
- CF&&CC百套计划1 Codeforces Round #449 A. Nephren gives a riddle
http://codeforces.com/contest/896/problem/A 第i个字符串嵌套第i-1个字符串 求第n个字符串的第k个字母 dfs #include<map> # ...
- Docker容器加载宿主机的hosts解析
前言 公司一直在用传统的tomcat下丢war包的架构体系,随着项目的增多.服务器数量的增多.需要为此花费很多时间在不同服务器的系统环境问题上.为了技术的与时俱进和工作的运维效率等方面,笔者引入doc ...
- The Ph.D. Grind
The Ph.D. Grind A Ph.D. Student Memoir Summary The Ph.D. Grind, a 122-page e-book, is the first know ...
- 如何定制Gtk版Emacs的Widget外观
当我们使用 xlib 版的Emacs时,可以通过 XResource 定义 Emacs 的菜单 栏.工具条.滚动条的外观. 现在,在Linux上我们大多使用 gtk版的Emacs,是否还有办法定义 E ...
- linux查询进程 kill进程
查询进程 #ps aux #查看全部进程 #ps aux|grep firewall #查询与firewall相关的进程 kill进程一 kill进程pid为711进程: #pkill -9 711 ...
- SQL查找数据库中所有没有主键的数据表脚本
--SQL查找数据库中所有没有主键的数据表脚本 --运行脚本后在消息中可能会显示下面现象中的一种:--(1)数据库中所有数据表都有主键(则证明所有数据表都有主键)--(2)当前数据表[数据表名]没有主 ...
- 截取汉字 mb_sbstr()
一.中文截取:mb_substr() mb_substr( $str, $start, $length, $encoding ) $str,需要截断的字符串 $start,截断开始处,起始处为0 $l ...
- 定价(Price)
传送门 [题目描述] 在市场上有很多商品的定价类似于 999 元.4999 元.8999 元这样.它们和 1000 元.5000 元和 9000 元并没有什么本质区别,但是在心理学上会让人感觉便宜很多 ...
- 《区块链100问》第78集:EOS是什么?
EOS是一个区块链开发平台,具有可扩展性强.支持大规模商业应用等特点. 首先,EOS采取DPoS共识算法及其他技术手段预期实现每秒百万级别交易请求,将能够支持数千个商业级的DAPPs. 以太坊是一条公 ...
- 凸包入门(Graham扫描法)(A - Wall POJ - 1113)
题目链接:https://cn.vjudge.net/contest/276359#problem/A 题目大意:有一个国王,要在自己的城堡周围建立围墙,要求围墙能把城堡全部围起来,并且围墙距离城堡的 ...