1. 程式人生 > >JSP學習筆記七之Cookie

JSP學習筆記七之Cookie

首先提一下http協議的無狀態性指的是伺服器不會記住已經給它發過請求的客戶端。每次收到請求都會認為是一個新的客戶端發過來的。(即:伺服器不會記住給他發過請求的客戶端)

所以這個時候我們就需要使用Cookie來儲存使用者的狀態。

 

Cookie指web伺服器儲存在客戶端的一系列文字資訊。比如:判定註冊使用者是否已經登陸網站、網購購物車的處理等。所以消耗的是客戶端的儲存空間。

Session是通過伺服器來保持狀態的,是伺服器端為客戶端所開闢的儲存空間。所以消耗的是伺服器端的儲存空間。

1、儲存使用者的狀態的兩大機制:cookie和session。

   a 、

cookie作用:
1.對特定物件的追蹤 
2.儲存使用者網頁瀏覽記錄與習慣
3.簡化登入
不足的是安全風險:容易洩露使用者資訊

   b、session的作用

在建立了Session的同時,伺服器會為該Session生成唯一的Session id,而這個Session id在隨後的請求中會被用來重新獲得已經建立的Session;在Session被建立之後,就可以呼叫Session相關的方法往Session中增加內容了,而這些內容只會儲存在伺服器中,發到客戶端的只有Session id;當客戶端再次傳送請求的時候,會將這個Session id帶上,

伺服器接受到請求之後就會依據Session id找到相應的Session,從而再次使用之。正這樣一個過程,使用者的狀態也就得以保持了。

 

2、Cookie的常用的方法

建立Cookie物件: Cookie newCookie = new Cookie(String key,Object value);
寫入cookie物件: response.addCookie(newCookie);
讀取Cookie物件: Cookie[] cookies = request.getCookies();


設定Cookie物件的有效期(秒): setMaxAge()
建立Cookie後進行賦值 setValue(String value)
獲取Cookie的名稱 getName()
獲取Cookie的 getValue()
獲取Cookie的有效期(秒): getMaxAge()

 

3、下面給出一個Cookie的JSP例項。


login.jsp中使用一個checkbox來進行記錄是否記住登陸狀態。然後在dologin.jsp進行建立cookie,並且設定cookie的值和向伺服器新增cookie例項、設定cookie物件的存活時間等。

dologin.jsp中會有超連結的存在,用於連線users.jsp。在users.jsp介面會顯示剛剛在登陸介面輸入的使用者名稱和密碼。這裡通過checkbox是否被勾選來判斷是否需要建立Cookie。程式的邏輯是checkbox的被選上就建立Cookie

 首先是登陸介面login.jsp


    <%
    	//獲取Cookie例項物件中的元素值
    	Cookie[] cookie=request.getCookies();
    	String username="";
    	String password="";
    	if(cookie!=null && cookie.length>0){
    		for(Cookie c:cookie){
    			if(c.getName().equals("username")){
    				username=c.getValue();
    			}
    			if(c.getName().equals("password")){
    				password=c.getValue();
    			}
    		}
    	}
    %>
  <body>
    <h1>使用者登入</h1>
    <hr>
        <form name="loginForm" action="dologin.jsp" method="post">
       <table>
         <tr>
           <td>使用者名稱:</td>
           <td><input type="text" name="username" value="<%=username %>"/></td>
         </tr>
         <tr>
           <td>密碼:</td>
           <td><input type="password" name="password" value="<%=password %>" /></td>
         </tr>
         <tr>
           <td colspan="2"><input type="checkbox" name="isUseCookie" checked="checked"/>十天內記住我的登入狀態</td>
         </tr>
         <tr>
           <td colspan="2" align="center"><input type="submit" value="登入"/><input type="reset" value="取消"/></td>
         </tr>
       </table>
    </form>
  </body>


<span style="font-family: Arial, Helvetica, sans-serif;">dologin.jsp程式碼如下:</span>
<span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="html" style="color: rgb(20, 25, 30);"><span style="font-family: Arial, Helvetica, sans-serif;"> <body></span>
 
 
    <h1>登入成功</h1>
    <hr>
    <br>
    <br>
    <br>
	<%
	//首先判斷使用者是否記住了登陸狀態
	String[] isUseCookie = request.getParameterValues("isUseCookie");
	if(isUseCookie!=null && isUseCookie.length>0){
		//將使用者名稱和密碼儲存到Cookie中
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		
		//定義Cookie物件
		Cookie usernameCookie=new Cookie("username",username);
		Cookie passwordCookie=new Cookie("password",password);
		//設定Cookie物件的有效時間
		usernameCookie.setMaxAge(864000);//10天
		passwordCookie.setMaxAge(864000);//10天
		
		//向伺服器中新增Cookie
		response.addCookie(usernameCookie);
		response.addCookie(passwordCookie);	
	}
	else{
		//檢查之前是否有cookie存在
		Cookie[] cookie=request.getCookies();
		if(cookie!=null && cookie.length>0){
			//遍歷Cookie
			for(Cookie c:cookie){
				if(c.getName().equals("username")||   //如果出現username和password的Cookie
						c.getName().equals("password")){
					c.setMaxAge(0);//將該cookie的時間設為0
					response.addCookie(c);//重新將Cookie新增到伺服器中
				}
			}
		}
	}
	%>
    <a href="users.jsp" target="_blank">檢視使用者資訊</a>
    
  </body>


users.jsp

結果顯示:




不選擇checkbox時的結果:




1、JSP常用的有page、include、taglib指令這三種指令

page:位於頁面頂端,一個頁面可以包含多個page指令
include:將一個外部檔案嵌入jsp中,同時解析這個頁面中的jsp語句。
taglib:使用標籤庫,自定義新的標籤,在jsp中啟動定製行為。

 

a、include指令 
語法 <% include file="地址"%>。
案例:顯示當前時間的頁面。步驟如下:

(1)寫一個只輸出時間的方法的date.jsp。

(2)用於顯示的頁面,需要包含<% include file="date.jsp"%>這句。

 

例項程式碼:

date.jsp

<%
//建立一個日期的例項
    Date d=new Date();
	SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
	String s=sdf.format(d);
	out.println(s);
%>

inculde_command.jsp

<body>
    <h1>include指令的測試</h1><br>
    <%@ include file="date.jsp"%>
  </body>

結果顯示如下:


5、include動作(動作標籤)
<jsp:include page="URL" flush="true/false" />
page :要包含的頁面
flush :被包含的頁面是否從緩衝區讀取

 

程式碼例項:

include_action.jsp

 <body>
    <h1>include動作的測試</h1><br>
    <jsp:include page="date.jsp" 					flush="false"></jsp:include>
  </body>


6、include指令和動作的比較:

 

 

include指令

jsp:include動作

語法格式

<%@ include file=””%>

<jsp:include page=””>

發生作用的時間

頁面轉換期間

請求期間

包含的內容

檔案的實際內容

頁面的輸出

轉換成Servlet

主頁面和包含頁面轉換成一個Servlet

主頁面和包含轉換為獨立的Servlet

編譯時間

較慢-資源必須被解析

較快

執行時間

稍快

較慢-每次資源必須被解析