JavaWeb同步学习笔记之五十、JavaWeb_HttpSession之简易购物车

JavaWeb_HttpSession之简易购物车

HttpSession之简易购物车

shopping
step-1.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

	<h4>Step1:选择要购买的图书:</h4>

	<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>Struts</td>
				<td><input type="checkbox" name="book" value="Struts" /></td>
			</tr>
			<tr>
				<td colspan="2">
					<input type="submit" value="Submit" />
				</td>
			</tr>
		</table>
	</form>
	
</body>
</html>

ProcessStep1Servlet .java

package com.xs.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;

import org.apache.catalina.ha.backend.Sender;

@WebServlet("/processStep1")
public class ProcessStep1Servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String [] books = request.getParameterValues("book");
		
		request.getSession().setAttribute("books", books);
		
		response.sendRedirect(request.getContextPath() + "/shoppingcart/step-2.jsp");
		
	}
}

step-2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

	<h4>Step2:请输入寄送地址和信用卡信息</h4>

	<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>
				<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>

ProcessStep2Servlet.java

package com.xs.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;
import javax.servlet.http.HttpSession;

@WebServlet("/processStep2")
public class ProcessStep2Servlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
       
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		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);
		
		HttpSession session = request.getSession();
		session.setAttribute("customer", customer);
		
		response.sendRedirect(request.getContextPath() + "/shoppingcart/confirm.jsp");
		
	}
}

confirm.jsp

<%@page import="com.xs.javaweb.Customer"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	
	<% 
		Customer customer = (Customer)session.getAttribute("customer");
		String [] books = (String[])session.getAttribute("books");
	%>

	<table border="1" cellpadding="10" cellspacing="0">
		<tr>
			<td>顾客姓名:</td>
			<td><%= customer.getName() %></td>
		</tr>
		<tr>
			<td>地址:</td>
			<td><%= customer.getAddress() %></td>
		</tr>
		<tr>
			<td>卡号:</td>
			<td><%= customer.getCard() %></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>

Customer.java

/**  
 * All rights Reserved,Designed By XS
 * @Title: Customer.java
 * @Package com.xs.javaweb
 * @Description: TODO
 * @author: XS
 * @date: 2019年3月11日 上午10:08:19
 * @version V1.0
 */
package com.xs.javaweb;

/**   
 * @ClassName: Customer
 * @Description: TODO
 * @author: XS
 * @date: 2019年3月11日 上午10:08:19
 * @version V1.0
 */
public class Customer {
	
	private String name;
	private String address;
	private String cardType;
	private String card;
	/**  
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**  
	 * @param name: the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**  
	 * @return the address
	 */
	public String getAddress() {
		return address;
	}
	/**  
	 * @param address: the address to set
	 */
	public void setAddress(String address) {
		this.address = address;
	}
	/**  
	 * @return the cardType
	 */
	public String getCardType() {
		return cardType;
	}
	/**  
	 * @param cardType: the cardType to set
	 */
	public void setCardType(String cardType) {
		this.cardType = cardType;
	}
	/**  
	 * @return the card
	 */
	public String getCard() {
		return card;
	}
	/**  
	 * @param card: the card to set
	 */
	public void setCard(String card) {
		this.card = card;
	}
	/**   
	 * @Title: Customer
	 * @Description: TODO
	 * @param name
	 * @param address
	 * @param cardType
	 * @param card
	 */
	public Customer(String name, String address, String cardType, String card) {
		super();
		this.name = name;
		this.address = address;
		this.cardType = cardType;
		this.card = card;
	}
	/**   
	 * @Title: Customer
	 * @Description: TODO
	 */
	public Customer() {
		super();
	}
	
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值