【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. [SCOI2007]压缩(区间dp)

    神仙题,看了半天题解才看明白... 因为题目里说如果没有m,会自动默认m在最前面. 我们设计状态为dp[l][r][0/1]为在区间l到r中有没有m的最小长度. 转移:枚举我们要压缩的起点,dp[l] ...

  2. Loj 103、10043 (KMP统计子串个数)

    KMP算法学习链接:https://blog.csdn.net/starstar1992/article/details/54913261/ KMP算法:可以实现复杂度为O(m+n) 为何简化了时间复 ...

  3. 关于servlet连接数据库会出现空指针异常情况

    一.servlet在连接数据库时,如果没有事先配置,当用Tomcat运行时会出现NullPointer的情况,是因为Tomcat在运行你的应用程序时没有连接mysql的jar包, 正确做法是将你的my ...

  4. A1046. Shortest Distance

    The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed t ...

  5. 腾讯云centos7安装MySQL

    centos就centos呗,为什么要加个腾讯云呢?有这种疑问的兄dei,一定是没被不同云的系统坑过啊,阿里云的Ubuntu和腾讯云的Ubuntu不一样,centos好像也有差别,各个云平台,同样的系 ...

  6. Python线程状态和全局解释器锁

    在刚接触Python的时候时常听到GIL这个词,并且发现这个词经常和Python无法高效的实现多线程划上等号.本着不光要知其然,还要知其所以然的研究态度,博主搜集了各方面的资料,花了一周内几个小时的闲 ...

  7. 自定义数据类型 typedef

    其实就是为数据类型起一个别名. typedef unsigned char AGE; //字符类型AGE x; //等价于 unsigned char x; typedef int * IPointe ...

  8. text-overflow文本溢出隐藏“...”显示

    一.文本溢出省略号显示 1.文本溢出是否“...”显示属性:text-overflow:clip(不显示省略标记)/ellipsis(文本溢出时“...”显示) 定义此属性有四个必要条件:1)须有容器 ...

  9. 8.Django

    ##update 操作更新数据

  10. python自动化开发-[第十七天]-django的ORM与其他

    今日概要: 1.name别名 2.模版的深度查询 3.模版语言之filter 4.自定义过滤器,filter和simpletag的区别 5.orm进阶 扫盲:url的组成 URL:协议+域名+端口+路 ...