1、HttpSession簡介css
session也是域對象之一,它的範圍是在一個會話範圍內有效。session既然是域對象,那麼固然有getAttribute()和setAttribute()方法了。html
在一個會話內共享一個session對象,因此session中能夠保存一個會話內的數據,例如當前用戶的信息。java
session的範圍大於request,能夠在一個會話中多個請求之間共享數據。但session的範圍小於application,session不能在多個用戶之間共享數據。瀏覽器
2、獲取session對象cookie
使用request.getSession()方法就能夠獲取session對象了。session
有了session,就不用使用cookie來跟蹤會話了!但session不能像Cookie那樣長命,一旦用戶關閉瀏覽器窗口,那麼session就死掉了。app
3、簡單購物車的實現jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>購物車</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <h3>手電筒<a href="/day11/cart?id=1">加入購物車</a></h3> <h3>冰箱<a href="/day11/cart?id=2">加入購物車</a></h3> <h3>電視<a href="/day11/cart?id=3">加入購物車</a></h3> <h3>洗衣機<a href="/day11/cart?id=4">加入購物車</a></h3> <h3>電腦<a href="/day11/cart?id=5">加入購物車</a></h3> </body> </html>
package cn.session; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * 簡單購物車的實現 */ @SuppressWarnings("serial") public class CartServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /** * 1.購物車 Map<String,Integer> cart * 2.先獲取購物車 判斷是不是第一次訪問 * 第一次訪問,建立購物車,把商品的名稱和數量加入到購物車,存入Session * 不是第一次訪問 * 判斷是否包含該商品,經過名稱判斷 * 若是包含,數量+1 存入Session * 若是不包含,存入購物車 存入Session * 3.要麼繼續購物 要麼結算 */ //獲取參數 String id = request.getParameter("id"); //購物車存入的是商品的名稱和數量 String[] names = {"手電筒","冰箱","電視","洗衣機","電腦"}; String name = names[Integer.parseInt(id)-1]; //從session中獲取購物車 HttpSession session = request.getSession(); Map<String, Integer> cart = (Map<String, Integer>) session.getAttribute("cart"); //若是第一次訪問 if(cart == null){ cart = new HashMap<String, Integer>(); cart.put(name, 1); session.setAttribute("cart",cart); }else{ if(cart.containsKey(name)){ cart.put(name, (Integer)(cart.get(name))+1); session.setAttribute("cart",cart ); }else{ cart.put(name, 1); session.setAttribute("cart", cart); } } //要麼繼續購物,要麼結算 response.setContentType("text/html;charset=utf-8"); response.getWriter().print("<h3><a href='/day11/session/cartList.jsp'>繼續購物</a>|<a href='/day11/session/pay.jsp'>去結算</a></h3>");; } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'pay.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <h3>結算頁面</h3> <% Map<String,Integer> cart = (Map<String,Integer>)session.getAttribute("cart"); if(cart != null){ for(Iterator<Map.Entry<String,Integer>> iterator = cart.entrySet().iterator();iterator.hasNext();){ Map.Entry<String,Integer> c = iterator.next(); String name = c.getKey(); Integer value = c.getValue(); %> 你購買的商品是<%=name%>,數量是<%=value%>個<br/> <% } }else{ %> <h3>你尚未購物,請<a href="/day11/session/cartList.jsp">購物</a>吧</h3> <% } %> </body> </html>