简易Session版购物车

一:实现界面展示:

------》       ----》

 二:框架结构: 一个Customer实体类,两个Servlet类,ProcessStep1Servlet.java处理step-1.jsp数据,ProcessStep2Servlet.java处理step-2.jsp数据,confirm.jsp显示最后结果。

三:代码实现:

Customer.java实体类:

package com.hp.javaweb;

public class Customer {
	private String name;
	private String address;
	private String cardType;
	private String 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 getCardType() {
		return cardType;
	}
	public void setCardType(String cardType) {
		this.cardType = cardType;
	}
	public String getCard() {
		return card;
	}
	public void setCard(String card) {
		this.card = card;
	}
	public Customer(String name, String address, String cardType, String card) {
		super();
		this.name = name;
		this.address = address;
		this.cardType = cardType;
		this.card = card;
	}
	public Customer(){}

}
web.xml中servlet的配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>1007Cookie</display-name>
  <servlet>
    <servlet-name>ProcessStep1Servlet</servlet-name>
    <servlet-class>com.hp.javaweb.ProcessStep1Servlet</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>ProcessStep2Servlet</servlet-name>
    <servlet-class>com.hp.javaweb.ProcessStep2Servlet</servlet-class>
  </servlet>


  <servlet-mapping>
    <servlet-name>ProcessStep1Servlet</servlet-name>
    <url-pattern>/processStep1</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>ProcessStep2Servlet</servlet-name>
    <url-pattern>/processStep2</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>



step-1.jsp:

<pre name="code" class="html"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'step-1.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <h4>选择要购买的图书</h4>
    
    <form action="<%=request.getContextPath() %>/processStep1" method="post">//数据提交到到Servlet层processStep1
    	<table cellpadding="10" cellspacing="0" 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>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数据处理: 

<pre name="code" class="html">package com.hp.javaweb;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ProcessStep1Servlet extends HttpServlet {
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//1、获取选中的图书
		String [] books =request.getParameterValues("book");
		//2、把图书信息放入到Httpsession中
		request.getSession().setAttribute("books", books);
		//3、重定向页面到shoppingCart/step-2.jsp
		System.out.println(request.getContextPath()+"/shoppingCart/step-2.jsp");
		response.sendRedirect(request.getContextPath()+"/shoppingCart/step-2.jsp");
		
	}

}


 重定向页面到shoppingCart/step-2.jsp: 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'step-2.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <h4>Step2:请输入寄送地址和信用卡信息</h4>
     <form action="<%=request.getContextPath() %>/processStep2" 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="Vsia"/>Vsia
    			<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>
ProcessStep2Servlet.java进行数据处理:

package com.hp.javaweb;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class ProcessStep2Servlet extends HttpServlet {

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public 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");
	}

}
重定向到confirm.jsp:

<%@page import="com.hp.javaweb.Customer"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'confirm.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
  
  <%
  	Customer customer=(Customer)session.getAttribute("customer");//获得顾客的信息
  	String []books =(String[] ) session.getAttribute("books");//获得书的信息
   %>
    <table cellpadding="10" cellspacing="0" border="1">
    	<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){    //遍历step-1顾客所买的书
    					out.print(book);
    					out.print("<br>");
    				}
    			
    			 %>
    		
    		</td>
    	</tr>
    </table>
  </body>
</html>

四:体会

   整体来说就是session的简单练习,将信息放入session中,转发到另一JSP中,在另一个JSP中进行操作显示。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值