版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Java Web 开发,天津师范大学 刘洋,第六章 Servlet会话(Session)编程,会话是什么,请求 1,请求 2,请求 n,.,服务器将唯一的会话 ID 分配给客户端以跟踪用户,会话的典型应用,网店中的购物车功能 用户权限控制 跨Servlet/JSP传递参数,已授权用户,未授权用户,请求,请求,Servlet 会话编程,HttpSession对象获取 request.getSession(boolean create) HttpSession接口的主要方法 void setAttribute(String, Object) Object getAttribute(String)
2、Enumeration getAttributeNames() void invalidate() int getMaxInactiveInterval() void removeAttribute(String name) void setMaxInactiveInterval(int interval),Servlet 会话编程,会话结束 浏览器关闭 主动关闭 会话超时,会话的底层实现方式,Cookies URL 重写,会话的底层实现方式,URL 重写 当浏览器不接受Cookie时,仍然可以使用Session 在链接后面增加jsessionid参数,例如:/servlet/getUser;
3、jsessionid=431D980051204FC8DC3 编程实现 out.println(User);,演示会话的使用,HttpSession session = request.getSession(); Integer counter = (Integer) session.getAttribute(counter); if (counter = null) counter = new Integer(1); else counter = new Integer(Value() + 1); session.setAttribute(counter, counte
4、r); session.setMaxInactiveInterval(3600); out.println(使用session的演示); out.println(你已经访问了 + counter + 次.);,演示会话的使用,if (session.isNew() out.println(这是一个新建的Session.); else out.println(这不是一个新建的Session.); out.println(jsessionid: + session.getId() + ); out.println(格式转换以后的LastAccessedTime: + new Date(sessio
5、n.getLastAccessedTime() + ); out.println(格式转换以后的CreationTime: + new Date(session.getCreationTime() + ); out.println(MaxInactiveInterval: + session.getMaxInactiveInterval() + 秒);,演示会话的使用,out.println(使用Session对象的getAttributeNames); Enumeration enumer = session.getAttributeNames(); String name = null;
6、while (enumer.hasMoreElements() name = (String) enumer.nextElement(); out.println(Session name: + name + ); out.println(Session value: + session.getAttribute(name) + ); / session.removeAttribute(counter); / session.invalidate();,使用会话实现网络购物车,实例流程图,displayItems.htm,AddShoppingCartServlet,GetShoppingCa
7、rtServlet,显示错误信息,提交添加商品请求,添加商品成功,添加商品失败,CartItem,public class CartItem private String name; private int quantity; private double price; private String id; public CartItem(String id, String name, int quantity, double price) this.id = id; = name; this.quantity = quantity; this.price = price;
8、 , public String getId() return id; public void setId(String id) this.id = id; public String getName() return name; public void setName(String name) = name; ,public double getPrice() return price; public void setPrice(double price) this.price = price; public int getQuantity() return quanti
9、ty; public void setQuantity(int quantity) this.quantity = quantity; public double getSum() return this.quantity * this.price; ,ShoppingCart,import java.util.ArrayList; import java.util.Iterator; public class ShoppingCart /* * 保存所有CartItem对象的容器对象。 */ private ArrayList cart; public ShoppingCart() cart
10、 = new ArrayList(); /* * 返回包括所有已经购物的商品信息的容器对象。 * return 当前的items容器对象 */ public ArrayList getCart() return cart; ,/* * 添加一种商品到购物车中,如果这种商品在购物车中已经存在, * 那就修改已有的商品的数量, * 反之,构造一个新的CartItem对象添加到items对象中。 * param item 新增的代表这种商品的对象 */ public void addCartItem(CartItem item) CartItem oldItem = null; if (item !
11、= null) for (int i = 0; i cart.size(); i+) oldItem = cart.get(i); if (oldItem.getId().equals(item.getId() oldItem.setQuantity(oldItem.getQuantity() + item.getQuantity(); return; cart.add(item); ,/* * 从购物车中,删除商品。 * param id 所删除商品的商品编号 * return 删除成功,返回true,反之返回false */ public boolean removeCartItem(St
12、ring id) CartItem item = null; for (int i = 0; i cart.size(); i+) item = cart.get(i); if (item.getId().equals(id) cart.remove(i); return true; return false; ,/* * 计算所购所有商品的总价。 * return 商品的总价 */ public double getTotal() Iterator it = cart.iterator(); double sum = 0.0; CartItem item = null; while (it.
13、hasNext() item = it.next(); sum = sum + item.getSum(); return sum; ,AddShoppingCartServlet,HttpSession session = request.getSession(); ShoppingCart cart = (ShoppingCart) session.getAttribute(shoppingcart); if (cart = null) cart = new ShoppingCart(); session.setAttribute(shoppingcart, cart); String i
14、d = request.getParameter(id); String name = request.getParameter(name); String quantity = request.getParameter(q); String price = request.getParameter(price);, cart.addCartItem(new CartItem(id, name, Integer.parseInt(quantity), Double.parseDouble(price); ,GetShoppingCartServlet, HttpSession session = request.getSession(); ShoppingCart cart = (ShoppingCart)session.getAttribute(shoppingcart); printCartItem(out, cart); ,private void printCartItem(PrintWriter out, ShoppingCart cart) ArrayList items = cart.get
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年南阳市南石医院医护人员招聘笔试备考题库及答案详解
- 2026年齐齐哈尔市中医医院医护人员招聘考试参考试题及答案详解
- 2026年中国人民解放军第三二二医院医护人员招聘笔试备考题库及答案详解
- (2026年)家长学校教师培训制度
- 2026年菏泽市中医医院医护人员招聘考试备考试题及答案详解
- (2026年)食堂原材料采购管理制度
- 2026年华东医院闵行门诊部医护人员招聘考试参考试题及答案详解
- 2026年保定市妇幼保健院医护人员招聘考试备考题库及答案详解
- 2026年天津市中医药研究院附属医院医护人员招聘笔试参考试题及答案详解
- 2026年黑龙江中医药大学附属第四医院医护人员招聘笔试备考试题及答案详解
- 2025年液化气站考试题库及答案
- 高速公路养护施工安全
- 第十章南国侨乡岭南文化旅游区74课件
- 华工综评测试题目及答案
- 2024年遵义市新蒲新区选调在编教师真题
- 商业计划书版
- 铁路基础知识86课件
- 非煤矿山电气安全培训
- 特检院面试试题及答案
- 电商平台客服部门绩效考核方案
- 北京市《配电室安全管理规范》(DB11T 527-2021)地方标准
评论
0/150
提交评论