【web.xml】

<session-config>
<session-timeout>30</session-timeout>
<cookie-config>
<!--<name>JSESSIONID</name>-->
<!--<domain>net.mypla</domain>-->
<!--<path>/shop</path>-->
<!--<comment><![CDATA[Keeps you logged in.See our privacy policy for more information]]></comment>-->
<http-only>true</http-only><!--Flash RIA不让访问-->
<!--<secure>false</secure>&lt;!&ndash;如果使用了HTTPS设为true&ndash;&gt;-->
<!--<max-age>1800</max-age>-->
</cookie-config>
<tracking-mode>COOKIE</tracking-mode><!--顺序很重要 首选cookie-->
<!--<tracking-mode>URL</tracking-mode>-->
<!--<tracking-mode>SSL</tracking-mode>-->
</session-config> 【StoreServlet】
@WebServlet(name="storeServlet",urlPatterns = {"/shop"})
public class StoreServlet extends HttpServlet{
private final Map<Integer,String> products = new Hashtable<>(); public StoreServlet(){
this.products.put(1,"Sandpaper");
this.products.put(2,"Nails");
this.products.put(3,"Glue");
this.products.put(4,"Paint");
this.products.put(5,"Tape");
} @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String action = req.getParameter("action");
if (action==null){
action = "browse";
}
switch (action){
case "addToCart":
this.addToCart(req,resp);
break;
case "viewCart":
this.viewCart(req,resp);
break;
case "browse":
default:
this.browse(req,resp);
break;
}
} private void browse(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setAttribute("products",this.products);
req.getRequestDispatcher("/WEB-INF/jsp/view/browse.jsp").forward(req,resp);
} private void viewCart(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setAttribute("products",this.products);
req.getRequestDispatcher("/WEB-INF/jsp/view/viewCart.jsp").forward(req,resp); } private void addToCart(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
int productId;
try{
productId = Integer.parseInt(req.getParameter("productId"));
}catch (Exception e){
resp.sendRedirect("shop");
return;
} //cart的结构 <productId,qty>
//准备把购物车商品保存在session当中
HttpSession session = req.getSession();
//如果先前已经有
if(session.getAttribute("cart") == null) {
session.setAttribute("cart", new Hashtable<Integer, Integer>());
} @SuppressWarnings("unchecked")
Map<Integer,Integer> cart = (Map<Integer, Integer>) session.getAttribute("cart");
if (!cart.containsKey(productId)){
cart.put(productId,0);
}
cart.put(productId,cart.get(productId)+1); resp.sendRedirect("shop?action=viewCart");
}
} 【browse.jsp】
<h2>商品列表</h2>
<a href="<c:url value="/shop?action=viewCart"/>">View Cart</a><br /><br />
<%
@SuppressWarnings("unchecked")
Map<Integer,String> products = (Map<Integer, String>) request.getAttribute("products");
for (int id:products.keySet()){
%><a href="<c:url value="/shop"><c:param name="action" value="addToCart"/>
<c:param name="productId" value="<%=Integer.toString(id)%>"/>
</c:url>"><%=products.get(id)%><br /></a><%
}
%>
【viewCart.jsp】
<h2>查看购物车</h2>
<a href="<c:url value="/shop"/>">商品列表</a><br/><br/>
<%
@SuppressWarnings("unchecked")
Map<Integer,String> products = (Map<Integer, String>) request.getAttribute("products");
@SuppressWarnings("unchecked")
Map<Integer,Integer> cart = (Map<Integer, Integer>) session.getAttribute("cart"); if (cart == null || cart.size()==0){
out.println("购物车是空空的");
}else{
for (int id:cart.keySet()){
out.println(products.get(id)+"(数量: "+cart.get(id)+")<br/>");
}
}
%>

Session 快速开始 通过session的attribute通信的更多相关文章

  1. 会话跟踪技术 - Cookie 和 Session 快速上手 + 登陆注册案例

    目录 1. 会话跟踪技术概述 2. Cookie 2.1 Cookie的概念和工作流程 2.2 Cookie的基本使用 2.3 Cookie的原理分析 2.4 Cookie的使用细节 2.4.1 Co ...

  2. 会话Cookie及session的关系(Cookie & Session)

    会话Cookie及session的关系(Cookie & Session) 在通常的使用中,我们只知道session信息是存放在服务器端,而cookie是存放在客户端.但服务器如何使用sess ...

  3. 客户端session与服务端session

    会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通过在服务器端 ...

  4. 转:客户端session与服务端session

    会话(Session)跟踪是Web程序中常用的技术,用来 跟踪用户的整个会话 .常用的会话跟踪技术是Cookie与Session. Cookie通过在客户端记录信息确定用户身份 , Session通过 ...

  5. 通过Spring Session实现新一代的Session管理

    长期以来,session管理就是企业级Java中的一部分,以致于我们潜意识就认为它是已经解决的问题,在最近的记忆中,我们没有看到这个领域有很大的革新. 但是,现代的趋势是微服务以及可水平扩展的原生云应 ...

  6. 转:通过Spring Session实现新一代的Session管理

    长期以来,session管理就是企业级Java中的一部分,以致于我们潜意识就认为它是已经解决的问题,在最近的记忆中,我们没有看到这个领域有很大的革新. 但是,现代的趋势是微服务以及可水平扩展的原生云应 ...

  7. 通过 Spring Session 实现新一代的 Session 管理

    长期以来,session 管理就是企业级 Java 中的一部分,以致于我们潜意识就认为它是已经解决的问题,在最近的记忆中,我们没有看到这个领域有很大的革新. 但是,现代的趋势是微服务以及可水平扩展的原 ...

  8. 懒加载session 无法打开 no session or session was closed 解决办法(完美解决)

           首先说明一下,hibernate的延迟加载特性(lazy).所谓的延迟加载就是当真正需要查询数据时才执行数据加载操作.因为hibernate当中支持实体对象,外键会与实体对象关联起来.如 ...

  9. 【荐】PHP Session和Cookie,Session阻塞,Session垃圾回收,Redis共享Session,不推荐Memcached保存Session

    什么是 Session 在 web 应用开发中,Session 被称为会话.主要被用于保存某个访问者的数据. 由于 HTTP 无状态的特点,服务端是不会记住客户端的,对服务端来说,每一个请求都是全新的 ...

随机推荐

  1. LOJ#2245 魔法森林

    这道题以前zbtrs大佬给我讲过.但是我只知道思想,不知道要lct维护... 这个套路很常见. 题意:给你一个无向图,每条边有a,b两个权值.求1到n号点的一条路径,路径的权值是每条边的最大a与最大b ...

  2. Remote debugger is in a background tab which may cause apps to perform slowly. Fix this by foregrounding the tab (or opening it in a separate window).

    先上代码: /** * Sample React Native App * https://github.com/facebook/react-native * * @format * @flow * ...

  3. c++ stl sort

    两者相等时,必须为false. 满足拟序. 群里大佬666.

  4. vue-cli+webpack在生成的项目中使用bootstrap的方法

    在一个html页面中加入bootstrap是很方便,就是一般的将css和js文件通过Link和Script标签就行.那么在一个用vue-vli生成的前端项目中如何加入?因为框架不一样了,略微要适应一下 ...

  5. HR算法具体过程

    首先研究HR算法在概率分布估计中的实现,我们再考虑如何将其应用于频繁项挖掘中. 一.确定输入数据类型 def generate_uniform_distribution(k): raw_distrib ...

  6. ASCLL、Unicode和UTF-8编码的理解

    我们已经讲过了,字符串也是一种数据类型,但是,字符串比较特殊的是还有一个编码问题. 因为计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.最早的计算机在设计时采用8个比特(bit) ...

  7. qml: 以鼠标为中心进行放缩;

    import QtQuick 2.9 import QtQuick.Window 2.2 import QtQuick.Controls 2.2 Window { visible: true widt ...

  8. 剑指Offer_编程题_4

    题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7, ...

  9. Python中函数的嵌套及闭包

    函数的嵌套 调用:在函数中调用函数 定义:在函数中定义函数 地址:函数名有内存地址,内存地址可赋值 示例 a = 1 def outer(): a = 1 def inner(): a = 2 def ...

  10. Spring+Mybatis+SpringMVC+Atomikos多数据源共存+不同数据库事物一致性处理

    网上找了一大堆的例子,没一个跑通的,都是copy转发,哎,整理得好辛苦..做个笔记,方便正遇到此问题的猿们能够得到帮助....废话不多说,贴代码..... 项目结构说明: 1.dao层的admin.w ...