Session的实现例子

3/8/2017来源:ASP.NET技巧人气:1910

session实现一个购物车

1.首先搭建web项目框架 

目录为:

2.再来看看web.xml的配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>session</display-name>
	<welcome-file-list>
		<welcome-file>/jsp/bookChose.jsp</welcome-file>
	</welcome-file-list>

	<servlet>
		<servlet-name>shoppingcar</servlet-name>
		<servlet-class>session.SessionShoppingCarSevlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>shoppingcar</servlet-name>
		<url-pattern>/SessionShoppingCarSevlet</url-pattern>
	</servlet-mapping>

	<servlet>
		<servlet-name>shoppinglist</servlet-name>
		<servlet-class>session.ShoppingCarListServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>shoppinglist</servlet-name>
		<url-pattern>/ShoppingCarListServlet</url-pattern>
	</servlet-mapping>
</web-app>
3.代码的是实现

SessionShoppingCarSevlet.class

package session;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

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 SessionShoppingCarSevlet extends HttpServlet {
	PRivate static final long serialVersionUID = 1L;
	@SuppressWarnings("unchecked")
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html;charset = UTF-8");
		// 获取会话对象
		HttpSession session = req.getSession();

		// 从会话域中获取shoppingCar属性对象
		// 定义key为书名,value为数量
		Map<String, Integer> shoppingCar = (Map<String, Integer>) session
				.getAttribute("shoppingCar");

		if (null == shoppingCar) {
			shoppingCar = new HashMap<String, Integer>();
		}
		String[] books = req.getParameterValues("book");
		if (books != null && books.length > 0) {
			for (String bookname : books) {
				// 判断书籍是否在购物车中
				if (null == shoppingCar.get(bookname)) {
					shoppingCar.put(bookname, 1);
				} else {
					shoppingCar.put(bookname, shoppingCar.get(bookname) + 1);
				}
			}
		}
		// 将更新后的购物车存储在会话域中
		session.setAttribute("shoppingCar", shoppingCar);
		resp.sendRedirect("http://localhost:8080/SessionExampleServlet/ShoppingCarListServlet");
	}
}


ShoppingCarListServlet类
package session;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import java.util.Map.Entry;

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 ShoppingCarListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@SuppressWarnings("unchecked")
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html;charset = UTF-8");
		PrintWriter out = resp.getWriter();

		HttpSession session = req.getSession();
		Map<String, Integer> shoppingCar = (Map<String, Integer>) session
				.getAttribute("shoppingCar");
		if (shoppingCar != null && shoppingCar.size() > 0) {
			out.print("<p>您购买的书籍有:</p>");
			for (Entry<String, Integer> book : shoppingCar.entrySet()) {
				out.print(book.getKey() + "---" + book.getValue() + "本");
			}
		} else {
			out.print("您还没有购买书籍");
		}
		out.print("<a href = 'http://localhost:8080/SessionExampleServlet/jsp/bookChose.jsp'>继续购买书籍</a>");
	}
}

4.bookChose.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/htnl4/losse.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>书籍选购</title>
</head>
<body>
	<h3>请选择你的书籍</h3>
	<form
		action="http://localhost:8080/SessionExampleServlet/SessionShoppingCarSevlet"
		method="post">
		<p>
			<input type="checkbox" name="book" value="javaSE 应用开发">javaSE
			应用开发
		</p>
		<p>
			<input type="checkbox" name="book" value="javaEE 应用开发">javaEE
			应用开发
		</p>
		<p>
			<input type="checkbox" name="book" value="javaWeb 应用开发">javaWeb
			应用开发
		</p>
		<p>
			<input type="submit" value="提交">
		</p>
	</form>
</body>
</html>