前言
之前我們在這篇文章java之jsp教程九大內置對象詳解中,已經講解完了六個個對象,接下來我們講解最后的三個對象
jsp pagecontext對象
pagecontext 是 javax.servlet.jsp.pagecontext 的實例對象。
pagecontext 對象表示整個 jsp 頁面,可以獲取或刪除以下對象的任意屬性:
- page
- request
- session
- application
pagecontext 常用的方法如下:
- object findattribute (string attributename):按 page、request、session、application 的順序查找指定的屬性,并返回對應的屬性值。如果沒有相應的屬性,則返回 null
- object getattribute (string attributename, int scope):在指定范圍內獲取屬性值。與 findattribute 不同的是,getattribute 需要指定查找范圍
- void removeattribute(string attributename, int scope):在指定范圍內刪除某屬性
- void setattribute(string attributename, object attributevalue, int scope):在指定范圍內設置屬性和屬性值
- exception getexception():返回當前頁的 exception 對象
- servletrequest getrequest():返回當前頁的 request 對象
- servletresponse getresponse():返回當前頁的 response 對象
- servletconfig getservletconfig():返回當前頁的 servletconfig 對象
- httpsession getsession():返回當前頁的 session 對象
- object getpage():返回當前頁的 page 對象
- servletcontext getservletcontext():返回當前頁的 application 對象
示例
使用 pagecontext 對象取得不同范圍的屬性值。index.jsp 代碼如下:
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <!doctype html> <html> <head> </head> <body> <% request.setattribute("info", "request范圍的值"); session.setattribute("info", "session范圍的值"); application.setattribute("info", "application范圍的值"); %> 利用 pagecontext 取出以下范圍內各值(方法一): <br> request 設定的值:<%=pagecontext.getrequest().getattribute("info")%> <br> session 設定的值:<%=pagecontext.getsession().getattribute("info")%> <br> application 設的值:<%=pagecontext.getservletcontext().getattribute("info")%> <hr> 利用pagecontext取出以下范圍內各值(方法二): <br> 范圍1(page)內的值:<%=pagecontext.getattribute("info", 1)%> <br> 范圍2(request)內的值:<%=pagecontext.getattribute("info", 2)%> <br> 范圍3(session)內的值:<%=pagecontext.getattribute("info", 3)%> <br> 范圍4(application)內的值:<%=pagecontext.getattribute("info", 4)%> <hr> 利用 pagecontext 修改或刪除某個范圍內的值: <% pagecontext.setattribute("info", "修改request范圍的值", 2); %> <br> 修改 request 設定的值: <br> <%=pagecontext.getrequest().getattribute("info")%> <br> <% pagecontext.removeattribute("info"); %> 刪除 session 設定的值:<%=session.getattribute("info")%> </body> </html>
運行結果如下:
index.jsp運行結果
jsp page對象
jsp page 的實質是 java.lang.object 對象,相當于 java 中的 this 關鍵字。
page 對象是指當前的 jsp 頁面本身,在實際開發(fā)中并不常用。
page 對象的常用方法如下:
class getclass():返回當前頁面所在類
int hashcode():返回當前頁面的 hash 代碼
string tostring():將當前頁面所在類轉換成字符串
boolean equals(object obj):比較對象和指定的對象是否相等
void copy (object obj):把對象復制到指定的對象中
object clone():復制對象
示例
下面通過一個簡單的例子來演示 page 中的方法。
index.jsp 代碼如下:
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <!doctype html> <html> <head> </head> <body> <% object obj; obj = null; %> 返回當前頁面所在類:<%=page.getclass()%> <br> 返回當前頁面的 hash 代碼:<%=page.hashcode()%> <br> 轉換成 string 類的對象:<%=page.tostring()%> <br> page和obj比較:<%=page.equals(obj)%> <br> page和this比較:<%=page.equals(this)%> </body> </html>
運行結果如下:
jsp cookie的使用
cookie 不是 jsp 內置對象,而是由 netscape 公司發(fā)明,用來跟蹤用戶會話(session)的方式。
cookie 由服務器生成并發(fā)送給瀏覽器(客戶端),瀏覽器會將其以文本文件的形式存儲在某個目錄下。
例如,ie 瀏覽器把 cookie 信息保存在類似于 c://windows//cookies 的目錄下,當用戶再次訪問某個網站時,服務器就會要求瀏覽器查找并返回之前發(fā)送的 cookie 信息,來識別此用戶。
識別用戶通常有以下步驟:
cookie 的作用表現(xiàn)在以下方面:
- 對特定對象的追蹤,如用戶的訪問次數(shù)、最后訪問時間等。
- 統(tǒng)計網頁的瀏覽次數(shù)。
- 在 cookie 有效期內,記錄用戶的登錄信息,簡化下一次的登錄過程。
- 實現(xiàn)各種個性化服務,如”最近瀏覽過的商品“。
注意:由于 cookie 會將用戶的個人信息保存在客戶端,如用戶名、計算機名、以及瀏覽和登錄的網站等。這些信息可能會包含一些比較敏感的內容,所以從安全角度來說,使用 cookie 存在著一定的風險,因此不建議在 cookie 中保存比較重要或隱私的內容。
cookie方法
cookie 常用方法如下:
- public void setdomain(string pattern):設置 cookie 的域名,如 biancheng.net
- public string getdomain():獲取 cookie 的域名
- public void setmaxage(int expiry):設置 cookie 有效期,單位:秒 默認僅在當前會話中存在
- public int getmaxage():獲取 cookie 有效期,單位:秒 默認為 -1,表示 cookie 保存到瀏覽器關閉為止
- public string getname():返回 cookie 的名稱,名稱創(chuàng)建后將不能被修改
- public void setvalue(string newvalue):設置 cookie 的值
- public string getvalue():獲取 cookie 的值
- public void setpath(string uri):設置 cookie 的路徑 默認為當前頁面目錄以及子目錄下的所有 url
- public string getpath():獲取 cookie 的路徑
- public void setsecure(boolean flag):設置 cookie 是否要加密傳輸
- public void setcomment(string purpose):設置 cookie 注釋
- public string getcomment():返回 cookie 注釋,如果 cookie 沒有注釋,則返回 nulljsp使用cookie
jsp 使用 cookie 主要分為以下幾個步驟。
創(chuàng)建 cookie 對象
創(chuàng)建 cookie 對象,name 代表 cookie 的名稱,value 表示該名稱對應的值,語法如下:
cookie cookie = new cookie(string name,string value);
注意:name 和 value 中不能包含空格和以下字符:
[ ] ( ) = , " / ? @ : ;
寫入 cookie
創(chuàng)建 cookie 對象后,調用 response.addcookie() 方法寫入 cookie,代碼如下:
response.addcookie(cookie);
設置 cookie 有效期
調用 setmaxage() 方法設置 cookie 的有效期(單位:秒),如將 cookie 有效期設置為 24 小時,代碼如下:
cookie.setmaxage(60*60*24);
讀取cookie
調用 request.getcookies() 方法讀取 cookie,該方法返回 http 請求中的 cookie 對象數(shù)組,需要通過遍歷進行訪問。
示例
通過 html 表單將客戶端數(shù)據(jù)提交到 index.jsp 中,并設置 cookie。
login.jsp 代碼如下:
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <html> <head> </head> <body> <form action="index.jsp" method="get"> 站點名: <input type="text" name="name"> <br /> 網址: <input type="text" name="url" /> <input type="submit" value="提交" /> </form> </body> </html>
index.jsp 代碼如下:
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <%@ page import="java.net.*"%> <% // 解決中文亂碼 string str = urlencoder.encode(request.getparameter("name"), "utf-8"); // 創(chuàng)建cookie對象 cookie name = new cookie("name", str); cookie url = new cookie("url", request.getparameter("url")); // 設置cookie有效期為24小時。 name.setmaxage(60 * 60 * 24); url.setmaxage(60 * 60 * 24); // 在響應頭部添加cookie response.addcookie(name); response.addcookie(url); %> <html> <head> <title>編程幫(www.biancheng.net)</title> </head> <body> <b>網站名:</b> <%=request.getparameter("name")%><br> <b>網址:</b> <%=request.getparameter("url")%> </body> </html>
運行結果如下:
login.jsp頁面運行結果
index.jsp頁面運行結果
讀取cookie
調用 request.getcookies() 方法,在 cookie.jsp 頁面中讀取 cookie
cookie.jsp 代碼如下:
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <%@ page import="java.net.*"%> <!doctype html> <html> <head> <title>編程幫(www.biancheng.net)</title> </head> <body> <% cookie cookie = null; //創(chuàng)建cookie對象 cookie[] cookies = null; // 獲取 cookie 的數(shù)據(jù) cookies = request.getcookies(); if (cookies != null) { out.println("<h2> 獲取cookie名稱與對應值</h2>"); for (int i = 0; i < cookies.length; i++) { cookie = cookies[i]; out.print("參數(shù)名 : " + cookie.getname()); out.print("<br>"); out.print("參數(shù)值: " + urldecoder.decode(cookie.getvalue(), "utf-8") + " <br>"); out.print("------------------------------------<br>"); } } else { out.println("<h2>cookie為空</h2>"); } %> </body> </html>
運行結果如下:
刪除cookie
刪除 cookie 步驟如下:
- 獲取 cookie
- 將要刪除的 cookie 有效期設置為 0
- 調用 response.addcookie() 方法重新寫入 cookie
刪除參數(shù)名為“name”的 cookie
cookie.jsp 代碼如下:
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <%@ page import="java.net.*"%> <!doctype html> <html> <head> <title>編程幫(www.biancheng.net)</title> </head> <body> <% cookie cookie = null; //創(chuàng)建cookie對象 cookie[] cookies = null; // 獲取 cookie 的數(shù)據(jù) cookies = request.getcookies(); if (cookies != null) { out.println("<h2> 獲取cookie名稱與對應值</h2>"); for (int i = 0; i < cookies.length; i++) { cookie = cookies[i]; //刪除參數(shù)名為name的cookie if ((cookie.getname()).compareto("name") == 0) { cookie.setmaxage(0); response.addcookie(cookie); out.print("刪除 cookie: " + cookie.getname() + "<br/>"); } out.print("參數(shù)名 : " + cookie.getname()); out.print("<br>"); out.print("參數(shù)值: " + urldecoder.decode(cookie.getvalue(), "utf-8") + " <br>"); out.print("------------------------------------<br>"); } } else { out.println("<h2>cookie為空</h2>"); } %> </body> </html>
刷新 cookie.jsp 頁面,運行結果如下:
注:也可以手動在瀏覽器中刪除 cookie。
session和cookie的區(qū)別
session 和 cookie 的區(qū)別如下:
session:
- cookie將信息保存在服務器
- 保存的值是 object 類型
- session 存儲的數(shù)據(jù)隨會話的結束而結束
- 安全性高,可以保存重要的信息
cookie:
- 將信息保存在客戶端
- 保存的值是 string 類型
- cookie 可以長期保存在客戶端
- 安全性低,通常用于保存不重要的信息
實際開發(fā)中,需要根據(jù)不同的業(yè)務需求來選擇采用哪種技術,例如,用戶的用戶名和密碼等敏感信息不能使用 cookie 存儲,淘寶購物的”最近瀏覽過的商品“,則可以使用 cookie 存儲在客戶端。
到此這篇關于java之jsp教程九大內置對象詳解的文章就介紹到這了,所有內容也都講解完了,對其他內容還感興趣的請搜索碩編程以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持碩編程!