一、page 對象
page對象代表JSP本身,更準確地說它代表JSP被轉譯後的Servlet,它可以調用Servlet類所定義的方法。
二、config 對象
config 對象裡存放著一些Servlet 初始的資料結構。
config 對象實現於javax.servlet.ServletConfig 介面,它共有下列四種方法:
| public String getInitParameter(name) |
返回String類型的初始化參數 |
| public java.util.Enumeration getInitParameterNames( ) |
返回所有初始化參數的名字 |
| public ServletContext getServletContext( ) |
獲得當前伺服器小程式或JSP頁面的伺服器小程式環境 |
| public Sring getServletName( ) |
獲得當前伺服器小程式或JSP頁面的名稱 |
三、request 對象
request 對象包含所有請求的資訊,如:請求的來源、標題、cookies和請求相關的參數值等等。
request 對象實現javax.servlet.http.HttpServletRequest介面的,所提供的方法可以將它分為四大類:
1.儲存和取得屬性方法;
| void setAttribute(String name, Object value) |
設定name屬性的值為value |
| Enumeration getAttributeNamesInScope(int scope) |
取得所有scope 範圍的屬性 |
| Object getAttribute(String name) |
取得name 屬性的值 |
| void removeAttribute(String name) |
移除name 屬性的值 |
2.取得請求參數的方法
| StringgetHeader(Stringname) |
取得name的標題 |
| EnumerationgetHeaderNames() |
取得所有的標題名稱 |
| EnumerationgetHeaders(Stringname) |
取得所有name的標題 |
| intgetIntHeader(Stringname) |
取得整數類型name的標題 |
| longgetDateHeader(Stringname) |
取得日期類型name的標題 |
| Cookie[]getCookies() |
取得與請求有關的cookies |
3.能夠取得請求HTTP 標題的方法
| StringgetContextPath() |
取得Context路徑(即月台名稱) |
| StringgetMethod() |
取得HTTP的方法(GET、POST) |
| StringgetProtocol() |
取得使用的協議(HTTP/1.1、HTTP/1.0) |
| StringgetQueryString() |
取得請求的參數字串,不過,HTTP的方法必須為GET |
| StringgetRequestedSessionId() |
取得使用者端的SessionID |
| StringgetRequestURI() |
取得請求的URL,但是不包括請求的參數字串 |
| StringgetRemoteAddr() |
取得使用者的IP地址 |
| StringgetRemoteHost() |
取得使用者的主機名稱 |
| intgetRemotePort() |
取得使用者的主機連接埠 |
| StringgetRemoteUser() |
取得使用者的名稱 |
| voidetCharacterEncoding(Stringencoding) |
設定編碼格式,用來解決表單傳遞中文的問題 |
四、response 對象
response 對象主要將JSP 處理資料後的結果傳回到用戶端。
response 對象是實現javax.servlet.http.HttpServletResponse 介面。response對象所提供的方法。
1.設定表頭的方法
| voidaddCookie(Cookiecookie) |
新增cookie |
| voidaddDateHeader(Stringname,longdate) |
新增long類型的值到name標題 |
| voidaddHeader(Stringname,Stringvalue) |
新增String類型的值到name標題 |
| voidaddIntHeader(Stringname,intvalue) |
新增int類型的值到name標題 |
| voidsetDateHeader(Stringname,longdate) |
指定long類型的值到name標題 |
| voidsetHeader(Stringname,Stringvalue) |
指定String類型的值到name標題 |
| voidsetIntHeader(Stringname,intvalue) |
指定int類型的值到name標題 |
2.設定響應狀態代碼的方法
| voidsendError(intsc) |
傳送狀態代碼(statuscode) |
| voidsendError(intsc,Stringmsg) |
傳送狀態代碼和錯誤資訊 |
| voidsetStatus(intsc) |
設定狀態代碼 |
3.用來URL 重寫(rewriting)的方法
| StringencodeRedirectURL(Stringurl) |
對使用sendRedirect()方法的URL予以編碼 |
五、out 對象
out 對象能把結果輸出到網頁上。
out主要是用來控制管理輸出的緩衝區(buffer)和輸出資料流(output stream)。
| voidclear() |
清除輸出緩衝區的內容 |
| voidclearBuffer() |
清除輸出緩衝區的內容 |
| voidclose() |
關閉輸出資料流,清除所有的內容 |
| intgetBufferSize() |
取得目前緩衝區的大小(KB) |
| intgetRemaining() |
取得目前使用後還剩下的緩衝區大小(KB) |
| booleanisAutoFlush() |
回傳true表示緩衝區滿時會自動清除;false表示不會自動清除並且產生異常處理 |
六、session 對象
session對象表示目前個別使用者的會話(session)狀況。
session對象實現javax.servlet.http.HttpSession介面,HttpSession介面所提供的方法
| longgetCreationTime() |
取得session產生的時間,單位是毫秒 |
| StringgetId() |
取得session的ID |
| longgetLastAccessedTime() |
取得使用者最後通過這個session送出請求的時間 |
| longgetMaxInactiveInterval() |
取得最大session不活動的時間,若超過這時間,session將會失效 |
| voidinvalidate() |
取消session對象,並將對象存放的內容完全拋棄 |
| booleanisNew() |
判斷session是否為"新"的 |
| voidsetMaxInactiveInterval(intinterval) |
設定最大session不活動的時間,若超過這時間,session將會失效 |
七、application對象
application對象最常被使用在存取環境的資訊。
因為環境的資訊通常都儲存在ServletContext中,所以常利用application對象來存取ServletContext中的資訊。
application 對象實現javax.servlet.ServletContext 介面,ServletContext介面容器所提供的方法
| intgetMajorVersion() |
取得Container主要的ServletAPI版本 |
| intgetMinorVersion() |
取得Container次要的ServletAPI版本 |
| StringgetServerInfo() |
取得Container的名稱和版本 |
| StringgetMimeType(Stringfile) |
取得指定檔案的MIME類型 |
| ServletContextgetContext(Stringuripath) |
取得指定LocalURL的Applicationcontext |
| StringgetRealPath(Stringpath) |
取得本地端path的絕對路徑 |
| voidlog(Stringmessage) |
將資訊寫入log檔案中 |
| voidlog(Stringmessage,Throwablethrowable) |
將stacktrace所產生的異常資訊寫入log檔案中 |
八、pageContext對象
pageContext對象能夠存取其他隱含對象。
1.pageContext對象存取其他隱含對象屬性的方法,此時需要指定範圍的參數。
Object getAttribute(String name, int scope)
Enumeration getAttributeNamesInScope(int scope)
void removeAttribute(String name, int scope)
void setAttribute(String name, Object value, int scope)
範圍參數有四個,分別代表四種範圍:PAGE_SCOPE、REQUEST_SCOPE、SESSION_SCOPE、APPLICATION_SCOPE
2.PageContext對象取得其他隱含對象的方法
| ExceptiongetException() |
回傳目前網頁的異常,不過此網頁要為errorpage, |
| JspWritergetOut() |
回傳目前網頁的輸出資料流,例如:out |
| ObjectgetPage() |
回傳目前網頁的Servlet實體(instance),例如:page |
| ServletRequestgetRequest() |
回傳目前網頁的請求,例如:request |
| ServletResponsegetResponse() |
回傳目前網頁的響應,例如:response |
| ServletConfiggetServletConfig() |
回傳目前此網頁的ServletConfig對象,例如:config |
| ServletContextgetServletContext() |
回傳目前此網頁的執行環境(context),例如:application |
| HttpSessiongetSession() |
回傳和目前網頁有聯絡的會話(session),例如:session |
3.PageContext對象提供取得屬性的方法
| ObjectgetAttribute(Stringname,intscope) |
回傳name屬性,範圍為scope的屬性對象,回傳類型為Object |
| EnumerationgetAttributeNamesInScope(intscope) |
回傳所有屬性範圍為scope的屬性名稱,回傳類型為Enumeration |
| intgetAttributesScope(Stringname) |
回傳屬性名稱為name的屬性範圍 |
| voidremoveAttribute(Stringname) |
移除屬性名稱為name的屬性對象 |
| voidremoveAttribute(Stringname,intscope) |
移除屬性名稱為name,範圍為scope的屬性對象 |
| voidsetAttribute(Stringname,Objectvalue,intscope) |
指定屬性對象的名稱為name、值為value、範圍為scope |
| ObjectfindAttribute(Stringname) |
尋找在所有範圍中屬性名稱為name的屬性對象 |
九、exception對象
若要使用exception 對象時,必須在page 指令中設定。<%@ page isErrorPage="true" %>才能使用。
exception提供的三個方法:
| getMessage() |
|
| getLocalizedMessage()、 |
|
| printStackTrace(newjava.io.PrintWriter(out) |
轉自點擊開啟連結