Session 快速开始 通过session的attribute通信
【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><!–如果使用了HTTPS设为true–>-->
<!--<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通信的更多相关文章
- 会话跟踪技术 - Cookie 和 Session 快速上手 + 登陆注册案例
目录 1. 会话跟踪技术概述 2. Cookie 2.1 Cookie的概念和工作流程 2.2 Cookie的基本使用 2.3 Cookie的原理分析 2.4 Cookie的使用细节 2.4.1 Co ...
- 会话Cookie及session的关系(Cookie & Session)
会话Cookie及session的关系(Cookie & Session) 在通常的使用中,我们只知道session信息是存放在服务器端,而cookie是存放在客户端.但服务器如何使用sess ...
- 客户端session与服务端session
会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通过在服务器端 ...
- 转:客户端session与服务端session
会话(Session)跟踪是Web程序中常用的技术,用来 跟踪用户的整个会话 .常用的会话跟踪技术是Cookie与Session. Cookie通过在客户端记录信息确定用户身份 , Session通过 ...
- 通过Spring Session实现新一代的Session管理
长期以来,session管理就是企业级Java中的一部分,以致于我们潜意识就认为它是已经解决的问题,在最近的记忆中,我们没有看到这个领域有很大的革新. 但是,现代的趋势是微服务以及可水平扩展的原生云应 ...
- 转:通过Spring Session实现新一代的Session管理
长期以来,session管理就是企业级Java中的一部分,以致于我们潜意识就认为它是已经解决的问题,在最近的记忆中,我们没有看到这个领域有很大的革新. 但是,现代的趋势是微服务以及可水平扩展的原生云应 ...
- 通过 Spring Session 实现新一代的 Session 管理
长期以来,session 管理就是企业级 Java 中的一部分,以致于我们潜意识就认为它是已经解决的问题,在最近的记忆中,我们没有看到这个领域有很大的革新. 但是,现代的趋势是微服务以及可水平扩展的原 ...
- 懒加载session 无法打开 no session or session was closed 解决办法(完美解决)
首先说明一下,hibernate的延迟加载特性(lazy).所谓的延迟加载就是当真正需要查询数据时才执行数据加载操作.因为hibernate当中支持实体对象,外键会与实体对象关联起来.如 ...
- 【荐】PHP Session和Cookie,Session阻塞,Session垃圾回收,Redis共享Session,不推荐Memcached保存Session
什么是 Session 在 web 应用开发中,Session 被称为会话.主要被用于保存某个访问者的数据. 由于 HTTP 无状态的特点,服务端是不会记住客户端的,对服务端来说,每一个请求都是全新的 ...
随机推荐
- webpack入门(一)webpack的动机 ---前端专业英语
记得某次考试,出国N年老师出的卷子全是英语,坑的英语不好的我们不要不要的.幸亏上了专业英语课.最重要的是专业英语对于我们很重要,比如webpack,一堆博客都是几小时入门,如何会用webpack,当你 ...
- Java线程池中submit()和execute之间的区别?
一: submit()方法,可以提供Future < T > 类型的返回值. executor()方法,无返回值. execute无返回值 public void execute(Runn ...
- GNOME禁用GDM中night-light功能
Night-light feature is enabled also in GDM screen, see here : https://bugzilla.gnome.org/show_bug.cg ...
- bzoj1398 Necklace
关于最小表示法的模板题. 最小表示法:把一个字符串表示为它的的所有循环同构字符串中的字典序最小者. 直接参见代码中的函数getmin()获取精髓 #include <cstdio> #in ...
- 洛谷P3674 小清新人渣的本愿
题意:多次询问,区间内是否存在两个数,使得它们的和为x,差为x,积为x. n,m,V <= 100000 解: 毒瘤bitset...... 假如我们有询问区间的一个桶,那么我们就可以做到O(n ...
- c#中序列化和反序列化的理解
using System.IO;using System.Runtime.Serialization.Formatters.Binary; 序列化:对象转化为文件的过程(字节流) 反序列化:文件(字节 ...
- SSH框架下ajax调用action并生成JSON再传递到客户端【以get和post方式提交】
需要完成的任务: 主要是把JSP页面上图片ID传给服务器端,服务器读取cookie看是否有username,如果有则根据ID读取MongoDB数据库,读出图片URL,再存放到mysql中的collec ...
- python: 多态与虚函数;
通过python的abc模块能够实现虚函数: 首先在开头from abc import ABCMeta, abstractmethod 例子 : #!/usr/bin/python #coding ...
- vim: 基本知识;
1. 函数: function! funcName(para.) content; endfunction 如果添加!,将覆盖已存在的重名函数: 注: 该博文为扩展型: 2.调用外部命令: exe ...
- 反汇编Dis解析
目录 反汇编dis解析 COMM段BSS段 注释段 Bl指令 title: 反汇编Dis解析 tags: ARM date: 2018-10-21 18:02:58 --- 反汇编dis解析 关于段, ...