1. 程式人生 > >JavaWeb:HttpSession(一)

JavaWeb:HttpSession(一)

art except Language 不包含 ansi web服務器 ctype 信用 com

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>

JavaWeb:HttpSession(一)