標籤:blog http java 使用 os io 檔案 資料
轉自:http://www.blogjava.net/RoyPayne/archive/2012/01/05/367930.html
————————————————————————————————————————————————————
JSP內建對象:我們在使用JSP進行頁面編程時可以直接使用而不需自己建立的一些Web容器已為使用者建立好的JSP內建對象。如request,session,response,out等。
下面就JSP2.0給出的9個內建對象:
| 內建對象 |
類型 |
範圍 |
| request |
javax.servlet.http.HttpServletRequest |
request |
| response |
javax.servlet.http.HttpServletResponse |
response |
| pageContext |
javax.servlet.jsp.PageContext |
page |
| session |
javax.servlet.http.HtpSession |
session |
| application |
javax.servlet.jsp.ServletContext |
application |
| out |
javax.servlet.jsp.JspWriter |
page |
| config |
javax.servlet.ServletConfig |
page |
| page |
java.lang.Object |
page |
| exception |
java.lang.Throwable |
page |
我們同樣可以根據它們的作用進行分類:
第一類:與Servlet有關:page和config
第二類:與Input/Output有關:out,request和response
第三類:與Context有關:application,session和pageContext
第四類:與Error有關:exception
一、屬性儲存範圍
在JSP中可以通過setAttribute()和getAttribute()這兩個方法來設定和取得屬性。從而實現資料的共用。JSP提供了四種屬性儲存範圍:response,request,session和application。
1、page:就是設定的屬性只能在當前頁面有效。通過pageContext的setAttribute()和getAttribute()。
2、request:指屬性在一次請求範圍內有效。如果頁面從給一個頁面跳轉到另一個頁面,那麼該屬性就失效了。這裡所指的跳轉是指用戶端跳轉,比如客戶單擊超連結跳轉到其他頁面或者通過瀏覽器地址欄瀏覽其他頁面。如果使用伺服器端跳轉<jsp:forward>,則該屬性仍然生效。同理使用request對象的setAttribute()和getAttribute()。
3、session:指客戶瀏覽器與伺服器一次會話範圍內,如果伺服器中斷連線,那麼屬性就失效了。同理通過session對象的setAttribute()和getAttribute()。session範圍的例外比如重新開啟一個瀏覽器。
4、application:指在整個伺服器範圍,知道伺服器停止以後才會失效。同理通過application對象的setAttribute()和getAttribute()。application範圍就是儲存的屬性只要伺服器不重啟,就能在任意頁面中擷取,就算重新開啟瀏覽器也是可以擷取屬性的。
二、移除屬性:在屬性的範圍還未到就想清除相應的屬性時,必須編程清除。即調用相應對象的removeAttribute()方法就可以移除指定屬性。
<%@ page language="java" contentType="text/html;charset=gb2312"%> <html> <head> <title>移除屬性</title> </head> <body> <%-- 在page範圍設定屬性 --%> <% pageContext.setAttribute("name","James"); //設定屬性name,其值為James pageContext.setAttribute("age","12"); //設定屬性age,其值為12 pageContext.setAttribute("sex","男"); //設定屬性sex,其值為男 %> <%-- 移除屬性--%> <% pageContext.removeAttribute("sex"); %> <%-- 取得page範圍屬性--%> <% String strName = (String)pageContext.getAttribute("name"); //取值屬性name的值 String strAge = (String)pageContext.getAttribute("age"); //取值屬性age的值 String strSex = (String)pageContext.getAttribute("sex"); //取值屬性sex的值 out.println("page範圍:name屬性值為" + strName + "<br>"); out.println("page範圍:age屬性值為" + strAge+ "<br>"); out.println("page範圍:sex屬性值為" + strSex); %> </body> </html>
結果頁面輸出:page範圍:name屬性值為James
page範圍:age屬性值為 12
page範圍:sex屬性值為 null
三、request對象:request對象不但可以用來設定和取得request範圍變數,還可以用來擷取用戶端請求參數、請求的來源、表頭、Cookies等。
1、request擷取用戶端請求參數方法
| 方法 |
傳回值類型 |
方法說明 |
| getParameter(String name) |
String |
擷取參數名為name的參數值 |
| getParameterNames() |
Enumeration |
擷取所有參數的名稱,可與上一個方法合用擷取所有參數的值 |
| getParameterValues(String name) |
String[] |
擷取參數名為name的所有參數,比如參數是多個checkbox |
| getParameterMap() |
Map |
擷取所有參數封裝的Map執行個體,通過Map執行個體的String[] get("id")方法返回對應參數名為id的值數組 |
2、request的其他方法
| 方法 |
傳回值類型 |
方法說明 |
| getHeader(String name) |
String |
擷取指定標題名稱為name的標題 |
| getHeaderName() |
Enumeration |
擷取所有的標題名稱 |
| getIntHeader(String name) |
int |
擷取標題名稱為name的標題,內容以整數類型返回 |
| getDateHeader(String name) |
long |
擷取標題名稱為name的標題,內容以日期類型返回 |
| getCookies() |
Cookie |
擷取相關的Cookie |
| getContextPath() |
String |
擷取Context的路徑 |
| getMethod() |
String |
擷取用戶端的提交方式 |
| getProtocol() |
String |
擷取使用的HTTP協議 |
| getQueryString() |
String |
擷取請求的字串 |
| getRequestSessionId() |
String |
擷取用戶端的Session ID |
| getRequestURI() |
String |
擷取請求的URI |
| getRemoteAddr() |
String |
擷取用戶端IP地址 |
四、response對象:用來向用戶端傳送輸出資訊。
| 方法 |
傳回值 |
方法說明 |
| addCookie(Cookie cookie) |
void |
|
| addDateHeader(String name,long date) |
void |
|
| addHeader(String name,String value) |
void |
|
| addIntHeader(String name,int value) |
void |
|
| setDateHeader(String name,long date) |
void |
|
| setHeader(String name,String value) |
void |
|
| setIntHeader(String name,int value) |
void |
|
| sendError(int sc) |
void |
傳送狀態代碼 |
| sendError(int sc,String msg) |
void |
傳送狀態代碼和錯誤資訊 |
| setStatus(int sc) |
void |
設定狀態代碼 |
| sendRedirect(String URL) |
void |
頁面重新導向,用來實現頁面跳轉 |
註:這裡的response對象的sendRedirect(String URL)方法設定頁面重新導向將改變瀏覽器地址欄資訊,所以也稱用戶端跳轉。
執行個體1:response對象實現頁面的自動重新整理:只需要早JSP頁面中加上
<%--使用response對象的setIntHeader設定屬性Refresh的值(單位為秒)來實現頁面自動重新整理--%> <% response.addIntHeader("Refresh",10); %>
執行個體2:實現頁面自動跳轉:可以通過response對象的setHeader()方法添加一個標題為Refresh的標題,並制定頁面跳轉時間及跳轉頁面,從而實現頁面自動跳轉。 <% response.setHeader("Refresh","10;URL=http://www.baidu.com"); %>
這裡使用setHeader方法添加一個標題為"Refresh",值為"10,URL=http://www.baidu.com"的標題。
五、out對象:用來向頁面輸出資訊。
| 方法 |
傳回值類型 |
方法說明 |
| clear() |
void |
清除網頁上輸出內容 |
| clearBuffer() |
void |
清除緩衝區內容 |
| close() |
void |
關閉緩衝區,清除所有內容 |
| getBufferSize() |
int |
取得緩衝區大小 |
| getRemaining() |
int |
取得緩衝區剩餘大小 |
| isAutoFulsh() |
boolean |
獲得緩衝區是否進行自動清除的資訊 |
| print(String str) |
void |
進行頁面輸出 |
| println(String str) |
void |
進行頁面輸出並換行 |
六、session對象:用來表示使用者的會話狀況,一般用於儲存使用者的各種資訊,直到生命週期逾時或者被認為釋放掉為止。
| 方法 |
傳回值類型 |
方法說明 |
| getId() |
String |
擷取session的ID |
| getCreationTime() |
long |
擷取session的產生時間 |
| getLashAccessedTime() |
long |
擷取使用者最後通過session發送請求時間 |
| getMaxInactiveInterval() |
long |
擷取session生命週期,如果超過這個時間則失效 |
| invalidate() |
void |
清空session內容 |
| isNew() |
boolean |
判斷session是否為“新”的 |
| setMaxInactiveInterval() |
void |
設定session生命週期,如果超過這個時間則失效 |
七、application對象:用來取得和設定Servlet的相關資訊。
| 方法 |
傳回值類型 |
方法說明 |
| getMajorVersion() |
int |
擷取主要的Servlet API版本 |
| getMinorVersion() |
int |
擷取次要的Servlet API版本 |
| getServerInfo() |
String |
擷取伺服器版本 |
| getMimeType() |
String |
擷取指定檔案的MIME類型 |
| getContext() |
ServerContext |
擷取指定Local的Application context |
| getRealPath |
String |
擷取指定path的絕對路徑 |
八、pageContext對象:可以用來設定page範圍的屬性,還可以設定其他範圍屬性,不過需要指定範圍參數,同時還可以擷取其他內建對象。
| 方法 |
傳回值 |
方法說明 |
| getException() |
Exception |
擷取當前的exception內建對象 |
| getOut() |
JspWriter |
|
| getPage() |
Object |
|
| getRequest() |
ServletRequset |
|
| getResponse() |
ServletResponse |
|
| getServletConfig() |
ServletConfig |
|
| getServeltContext() |
ServletContext |
|
| getSession() |
HttpSession |
|
| getAttribute(String name,int scope) |
Object |
擷取指定範圍的name屬性值 |
| getAttributeNamesInScope(int scope) |
Enumeration |
擷取指定範圍所有屬性名稱 |
| getAttributesScope(String name) |
int |
擷取屬性名稱為name的屬性範圍 |
| removeAttribute(String name) |
void |
移除屬性名稱為name的 屬性 |
| removeAttribute(String name,int scope) |
void |
移除指定範圍的屬性名稱為name的 屬性 |
| setAttribute(String name,Object value,int scope) |
void |
設定指定範圍的name屬性 |
| findAttribute(String name) |
Object |
尋找所有範圍的屬性名稱為name的屬性 |
九、page對象:page對象代表JSP轉譯後的Servlet,通過page對象可以非常方便地調用Servlet類中定義的方法。
執行個體1:通過page對象調用Servlet類中定義的方法
<%@ page language="java" contentType="text/html;charset=gb2312"%> <%@ page info="this is the page Object Practice" %> <html> <head> <title>page對象</title> </head> <body> <%--通過page對象調用Servlet中的getServletInfo()方法 --%> <% String info = ((javax.servlet.jsp.HttpJspPage)page).getServletInfo(); %> <%=info %> </body> </html>
輸出:this is the page Object Practice.
十、config對象:可以用來擷取Servlet的配置資訊。
| 方法 |
傳回值類型 |
方法說明 |
| getInitParameter(name) |
String |
擷取Servlet初始化參數 |
| getInitParameterNames() |
Enumeration |
擷取Servlet所有初始化參數名稱 |
| getServletContext() |
ServletContext |
擷取當前Application context |
| getServletName() |
String |
擷取Servlet名稱 |
十一、exception對象:用來處理錯誤異常,如果要用exception對象,必須指定page中的isErrorPage屬性值為true。
執行個體:exception用來處理錯誤異常
1、包含錯誤的頁面:error.jsp
<%@ page language="java" contentType="text/html;charset=gb2312" errorPage="ExceptionDemo01.jsp"%> <html> <head> <title>錯誤頁面</title> </head> <body> <% int[] arr = {1,2,3}; out.println(arr[3]); %> </body> </html>
註:error.jsp中arr[3]數組下表越界.需在page指令中指定errorPage的屬性值為相應的例外處理常式。
2、異常處理頁面:exceptionDo.jsp
<%@ page language="java" contentType="text/html;charset=gb2312" isErrorPage="true"%> <%@page import="java.io.PrintStream"%> <html> <head> <title>處理錯誤異常</title> </head> <body> <%=exception%><br> <%=exception.getMessage()%><br> <%=exception.getLocalizedMessage()%><br> <% exception.printStackTrace(new java.io.PrintWriter(out)); %> </body> </html>
註:必須在page指令中指定屬性isErrorPage屬性值為true。也就是說這個頁面可以用於異常處理。
將會在exceptionDo.jsp中列印出error.jsp中的數組下標越界異常 資訊