Session机制:
1)、session机制采用的是在服务器端保持 HTTP 状态信息的方案 。
2)、当程序需要为某个客户端的请求创建一个session时,服务器首先检查这个客户端的请求里是否包含了一个session标识(即sessionId),如果已经包含一个sessionId则说明以前已经为此客户创建过session,服务器就按照session id把这个session检索出来使用(如果检索不到,可能会新建一个,这种情况可能出现在服务端已经删除了该用户对应的session对象,但用户人为地在请求的URL后面附加上一个JSESSION的参数)。如果客户请求不包含sessionId,则为此客户创建一个session并且生成一个与此session相关联的sessionId,这个session id将在本次响应中返回给客户端保存。
3)、方法:
--获取Session 对象:
request.getSession(), request.getSession(boolean Create);
--属性相关的:
getAttribute()、setAttribute()、removeAttribute()
--使HttpSession 失效:
invalidate()
--设置其最大时效:
setMaxInactiveInterval()
4)、url 重写:它允许不支持Cookie的浏览器也可以与WEB服务器保持连续的会话。将会话标识号以参数形式附加在超链接的URL地址后面的技术称为URL重写。
<%= response.encodeURL("login.jsp")%>
5)、应用:一个简易的购物车。
先看一下效果:
代码:
step1.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <h4>Step1:选择要购买的书籍</h4> 11 <form action="<%= request.getContextPath() %>/processStep1" method="post"> 12 <table border="1" cellpadding="10" cellspacing="0"> 13 <tr> 14 <td>书名</td> 15 <td>购买</td> 16 </tr> 17 <tr> 18 <td>Java</td> 19 <td><input type="checkbox" name="book" value="Java"/></td> 20 </tr> 21 <tr> 22 <td>Mysql</td> 23 <td><input type="checkbox" name="book" value="MySql"/></td> 24 </tr> 25 <tr> 26 <td>Oracle</td> 27 <td><input type="checkbox" name="book" value="Oracle"/></td> 28 </tr> 29 <tr> 30 <td colspan="2"><input type="submit" value="Submit"/></td> 31 </tr> 32 </table> 33 </form> 34 </body> 35 </html>
ProcessStep1Servlet .java:
package com.hnust.javaweb; 2 3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 9 public class ProcessStep1Servlet extends HttpServlet { 10 private static final long serialVersionUID = 1L; 11 12 protected void doPost(HttpServletRequest request, HttpServletResponse response) 13 throws ServletException, IOException { 14 //获取book 参数信息 15 String[] book = request.getParameterValues("book"); 16 //放入 17 request.getSession().setAttribute("books", book); 18 //重定向到step2.jsp 页面,要用绝对路径 19 response.sendRedirect(request.getContextPath() + "/shoppingcart/step2.jsp"); 20 } 21 22 }
step2.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <h4>Step2:请输入寄送地址和信用卡地址</h4> 11 <form action="<%= request.getContextPath() %>/processStep2" method="post"> 12 <table border="1" cellpadding="10" cellspacing="0"> 13 <tr> 14 <td colspan="2">基本信息</td> 15 </tr> 16 <tr> 17 <td>姓名</td> 18 <td><input type="text" name="name"/></td> 19 </tr> 20 <tr> 21 <td>寄送地址:</td> 22 <td><input type="text" name="address"/></td> 23 </tr> 24 <tr> 25 <td colspan="2">信用卡信息</td> 26 </tr> 27 <tr> 28 <td>种类</td> 29 <td> 30 <input type="radio" name="cardtype" value="Visa"/>Visa 31 <input type="radio" name="cardtype" value="Master"/>Master 32 </td> 33 </tr> 34 <tr> 35 <td>卡号</td> 36 <td><input type="text" name="card"/></td> 37 </tr> 38 <tr> 39 <td colspan="2"><input type="submit" value="Submit"/></td> 40 </tr> 41 </table> 42 </form> 43 </body> 44 </html>
ProcessStep2Servlet .java:
package com.hnust.javaweb; 2 3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 9 public class ProcessStep2Servlet extends HttpServlet { 10 private static final long serialVersionUID = 1L; 11 12 protected void doPost(HttpServletRequest request, HttpServletResponse response) 13 throws ServletException, IOException { 14 //获取请求参数 15 String name = request.getParameter("name"); 16 String address = request.getParameter("address"); 17 String cardtype = request.getParameter("cardtype"); 18 String card = request.getParameter("card"); 19 //封装 20 Customer customer = new Customer(name, address, cardtype, card); 21 //放入 22 request.getSession().setAttribute("customers", customer); 23 //重定向到step3.jsp 页面,使用绝对路径 24 response.sendRedirect(request.getContextPath() + "/shoppingcart/step3.jsp"); 25 } 26 27 }
注意要建一个 Customer 实体类,来封装 step2 里面的信息。代码就不贴了。
step3.jsp:
<%@page import="com.hnust.javaweb.Customer"%> 2 <%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <h4>Step3:订单确认</h4> 12 <% 13 String[] books = (String[]) session.getAttribute("books"); 14 Customer cus = (Customer) session.getAttribute("customers"); 15 %> 16 <table border="1" cellpadding="10" cellspacing="0"> 17 <tr> 18 <td>顾客姓名</td> 19 <td><input type="text" value="<%= cus.getName() %>"/></td> 20 </tr> 21 <tr> 22 <td>地址</td> 23 <td><input type="text" value="<%= cus.getAddress() %>"/></td> 24 </tr> 25 <tr> 26 <td colspan="2">付款信息</td> 27 </tr> 28 <tr> 29 <td>信用卡类型</td> 30 <td><input type="text" value="<%= cus.getCardtype() %>"/></td> 31 </tr> 32 <tr> 33 <td>卡号</td> 34 <td><input type="text" value="<%= cus.getCard()%>"/></td> 35 </tr> 36 <tr> 37 <td colspan="2">订货项目</td> 38 </tr> 39 <tr> 40 <td></td> 41 <td> 42 <% 43 for(String book : books){ 44 out.print(book); 45 out.print("<br>"); 46 } 47 %> 48 </td> 49 </tr> 50 </table> 51 </body> 52 </html>