javaWeb(七)Session简介、Session模拟简单购物车应用

源代码:source code


1、Session简介:

Cookie的安全问题:

由于Cookie会保存在客户端上,所以有安全隐患问题。 还有一个问题, Cookie的大小与个数有限制。 为了解决这个问题 —> Session .

Session的引出:

会话 , Session是基于Cookie的一种会话机制。 Cookie是服务器返回一小份数据给客户端,并且存放在客户端上。 Session是,数据存放在服务器端。

2、简单购物车应用:
描述:
javaWeb(七)Session简介、Session模拟简单购物车应用_第1张图片

登陆界面:login.jsp

 <body>
    <a href="carservlet?id=0">iphoneXa><br>
    <a href="carservlet?id=1">HUAWEIa><br>
    <a href="carservlet?id=2">MIa><br>
    <a href="carservlet?id=3">vivoa><br>
    <a href="carservlet?id=4">oppoa><br>
 body>

买东西carservlet:

package cn.nupt;

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

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 Car
 */
@WebServlet("/Car")
public class Car extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=UTF-8");
		int id = Integer.parseInt(request.getParameter("id"));// 拿到我们点击的手机的id,只有一个

		String[] phone = { "iphoneX", "HUAWEI", "MI", "vivo", "oppo" };// 我们通过数据库自己创建的数组

		String name = phone[id];

		Map<String, Integer> map = (Map<String, Integer>) request.getSession().getAttribute("car");// 我们创造一个session,这里的key是名字,这里的value是map集合,放手机-数量的自合
		if (map == null) { // 如果map不存在,就创建一个map
			map = new LinkedHashMap<String, Integer>();
			request.getSession().setAttribute("car", map);
		}

		if (map.containsKey(name)) {
			map.put(name, map.get(name) + 1);
		} else {
			map.put(name, 1);
		}

		response.getWriter().write("请继续购物
"
); response.getWriter().write("去购物车结算
"
); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }

购物车页面:giveMoney.jsp

<body>

	<%
		Map<String, Integer> map = (Map<String, Integer>) request.getSession().getAttribute("car");

		if (map != null) {

			for (String name : map.keySet()) {

				int num = map.get(name);
	%>

<h3>你买了:<%=num %> 部 <%=name %>h3>

	<%
		}
	}
	%>
	
	<a href="DeleteCarServlet">清空购物车a>




body>

清空购物车DeleteCarServlet:

package cn.xinhua;

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 DeleteCarServlet
 */
@WebServlet("/DeleteCarServlet")
public class DeleteCarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
	request.getSession().removeAttribute("car");
	
	response.sendRedirect("giveMoney.jsp");
	
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

你可能感兴趣的:(JavaWeb,javaWeb)