Java 购物车 Session

choice.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>选择商品</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 bgcolor=cyan>
		<form action="./servlet/AddProductServlet" method="post" name='e'>
			<select name="item">
				<option value="电视机">电视机</option>
				<option value="苹果">苹果</option>
				<option value="可乐">可乐</option>
				<option value="牛奶">牛奶</option>
				<option value="茶叶">茶叶</option>
			</select>
			<p>输入购买的数量:<input type="text" name="mount"/></p>
			<p>选择计量单位:
				<input type="radio" name="unit" value="个"/>个
				<input type="radio" name="unit" value="公斤"/>公斤
				<input type="radio" name="unit" value="台"/>台
				<input type="radio" name="unit" value="瓶"/>瓶
				<input type="submit"  value="提交添加"/>
			</p>
		</form>  
  </body>
</html>

2. removegood.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>选择删除的商品</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 bgcolor=cyan>
		<form action="./servlet/RemoveProductServlet" method="post" name='e'>
			<select name="item">
				<option value="电视机">电视机</option>
				<option value="苹果">苹果</option>
				<option value="可乐">可乐</option>
				<option value="牛奶">牛奶</option>
				<option value="茶叶">茶叶</option>
			</select>
			<input type="submit"  value="删除"/>
			</form>
  </body>
</html>

3. Good.java

package day10Shopping;

public class Good {
	private String item;
	private String mount;
	private String unit;
	public String getItem() {
		return item;
	}
	public void setItem(String item) {
		this.item = item;
	}
	public String getMount() {
		return mount;
	}
	public void setMount(String mount) {
		this.mount = mount;
	}
	public String getUnit() {
		return unit;
	}
	public void setUnit(String unit) {
		this.unit = unit;
	}
	
}

4. AddProductServlet.java

package day10Shopping;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

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 AddProductServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public AddProductServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @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 doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request,response);
	}

	/**
	 * 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
	 */
	@SuppressWarnings("unchecked")
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8"); 
		response.setContentType("text/html;charset=utf-8");
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
		HttpSession session=request.getSession(true);
		//从表单中获取
		String item=request.getParameter("item");
		String acount=request.getParameter("mount");
		int num=0;
		if(acount!=null&&!"".equals(acount.trim())){
			num=Integer.parseInt(acount.trim());
		}
		String unit=request.getParameter("unit");
		
		
		//从session中获取
		Good g=(Good)session.getAttribute(item);
		if(g!=null){
			g.setMount(Integer.toString((Integer.parseInt(g.getMount())+num)));
		}else{
			//封装商品到商品对象中
			Good good=new Good();
			good.setItem(item);
			good.setMount(acount);
			good.setUnit(unit); 
			//放置商品到session中
			session.setAttribute(item, good);
		}
		
		out.println("当前购买的商品是"+item+" 数量 "+acount+" 单位 "+ unit+"<br>");
		
		out.println("您购买的商品如下<br>");
		//获取购物车中的商品
		Enumeration<String> em=session.getAttributeNames();
		while(em.hasMoreElements()){
			String name=em.nextElement();
			Good g1=(Good)session.getAttribute(name);
			if(g1!=null){
				out.println(g1.getItem()+" 数量: "+g1.getMount()+" 单位: "+g1.getUnit()+"<br>");
			}
		}
		
		out.println("<a href='../choice.jsp'>继续购物</a>");
		out.println("<a href='/Web/removegood.jsp'>删除商品</a>");
		
		
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}

5. RemoveProductGood.java

package day10Shopping;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

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 RemoveProductServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public RemoveProductServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @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 doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request,response);
	}

	/**
	 * 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 {
		request.setCharacterEncoding("utf-8"); 
		response.setContentType("text/html;charset=utf-8");
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
		HttpSession session=request.getSession(true);
		String item=request.getParameter("item");
		session.removeAttribute(item);
		out.println("当前您删除的商品是"+item+"<br>");
		out.println("您购买的商品如下<br>");
		//获取购物车中的商品
		Enumeration<String> em=session.getAttributeNames();
		while(em.hasMoreElements()){
			String name=em.nextElement();
			Good g=(Good)session.getAttribute(name);
			if(g!=null){
				out.println(g.getItem()+" 数量: "+g.getMount()+" 单位: "+g.getUnit()+"<br>");
			}
		}
		out.println("<a href='/Web/choice.jsp'>继续购物</a>");
		out.println("<a href='/Web/removegood.jsp'>删除商品</a>");
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}

6. web.xml

<servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>AddProductServlet</servlet-name>
    <servlet-class>day10Shopping.AddProductServlet</servlet-class>
  </servlet>
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>RemoveProductServlet</servlet-name>
    <servlet-class>day10Shopping.RemoveProductServlet</servlet-class>
  </servlet>

<servlet-mapping>
    <servlet-name>AddProductServlet</servlet-name>
    <url-pattern>/servlet/AddProductServlet</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>RemoveProductServlet</servlet-name>
    <url-pattern>/servlet/RemoveProductServlet</url-pattern>
  </servlet-mapping>






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值