【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. 模拟赛 yjqb

    对于这种“不能交叉”的条件,不是很好处理.那么就考虑一下dp dp[i][j]表示,考虑A中用前i个,考虑连接B中用前j个,最大匹配.(类似LCS的DP) 转移:dp[i][j]=max(dp[i][ ...

  2. windows 文件夹下所有文件名称

    dir dir /b 没有其它信息 dir /b >>xxx.txt 保存到txt

  3. Linux:不同文件相同列字符合并文件(awk函数)

    存在file1.txt,其内容如下: H aa 0 0 1 -9 H bb 0 0 2 -9 H cc 0 0 2 -9 存在file2.txt,其内容如下: H aa 0 0 0 -9 asd qw ...

  4. request 对于cookie,session, json的处理

    一.cookie是存放在客户端,session是存放在服务端. 因为http是无状态的,当客户端发送请求给服务端的时候,服务端为了区分下一次发送请求的是不是同一个客户,那么就需要用一种方式记录下这个客 ...

  5. bigdata learning unit two--Spark environment setting

    1.下载 Spark安装之前的准备 文件的解压与改名 tar -zxvf spark-2.2.0-bin-hadoop2.7.tgz rm -rf spark-2.2.0-bin-hadoop2.7. ...

  6. vnc连接虚拟机中的CentOS7系统

    1.CentOS7 core安装gnome桌面 安装Gnome包# yum groupinstall "GNOME Desktop" "Graphical Adminis ...

  7. Luogu P2770 航空路线问题

    题目链接 \(Click\) \(Here\) 本来想调剂心情没想到写了那么久,还被\(dreagonm\)神仙嘲讽不会传纸条,我真是太弱了\(QAQ\)(原因:最开始写最大费用最大流一直想消圈,最后 ...

  8. bzoj1233 单调队列优化dp

    https://www.lydsy.com/JudgeOnline/problem.php?id=1233 数据结构优化dp的代码总是那么抽象 题意:奶牛们讨厌黑暗. 为了调整牛棚顶的电灯的亮度,Be ...

  9. Java基础方法整理

    方法 9.1方法概述 方法就是用来完成解决某件事情或实现某个功能的办法 可以通过在程序代码中引用方法名称和所需的参数,实现在该程序中执行(或称调用)该方法.方法,一般都有一个返回值,用来作为事情的处理 ...

  10. git ssh https 踩坑记 ---- 域账号密码更新

    前几天突然通知要更新公司的域账号密码,然后git pull就一直报 fatal: Authentication failed for 'https://git ... 很奇怪的是,有一个项目git p ...