1. 程式人生 > >jsp我的學習筆記,

jsp我的學習筆記,

這篇目標Jsp隱式物件

Jsp隱式物件
  Jsp物件是通過Jsp機制自動建立的Java類例項,充許與底層Servlet環境互動.Jsp物件分為四類:(1)輸入.輸出物件,(2)Servlet相關物件,(3)作用域物件,(4)exception物件。

輸入輸出物件
Request 物件:它表示一個HTTP的請求,如來源、cookies、GET/POST 請求的引數值,此物件實現javax.servlet.http.HttpServletRequest介面,方法:

void setAttribute(String name, Object value)   設定Request引數的值
String getAttribute(String name)           取出Request引數的值
String getParameter(String name)           取出Request引數值
Enumeration getParameterNames()           取出所有Request引數名
String getHeader(String name)             取出請求頭值
Enumeration getHeaderNames()             取出所有的請求頭名稱
Cookie [] getCookies()                 取出請求關聯的Cookie
String getMethod()                     取出HTTP的方法如(GET、POST)
String getRequestURI()                 取出請求的URL
String getQueryString( )                 取出請求引數字串
String getContextPath()                 取出站點名稱
String getRemoteUser()                 取出請求的使用者的名稱
String getRemoteAddr()                 取出請求的IP地址
String getRemoteHost()                 取出請求的主機名


Response 物件:將JSP處理結果返回給客戶端,此物件實現javax.servlet.http.HttpServletResponse 介面,方法:

void addCookie(Cookie cookie)             新增Cookie
void addHeader(String name, Object value)     新增值到name標頭
void setStatus(code)                   設定狀態碼
void sendError(int sc, String msg)         設定狀態碼和錯誤資訊
void sendRedirect(url)                 響應客戶端,請求另一個URL


out 物件:將內容寫入Jsp頁面輸出流中,控制管理輸出緩衝區和輸出流,方法:

void close()                         關閉輸出流
void clear()                         清除輸出緩衝區
void newLine                         寫入一個換行符到緩衝區
int getRemaining()                     取出未使用的緩衝區大小
int getBufferSize()                   取出輸出緩衝區的大小
boolean isAutoFlush()                   取出輸出緩衝區是否為自動重新整理


Servlet相關物件
page物件:代表JSP本身,更好的說它表示Servlet的例項,它可以呼叫Servlet類所定義的任何方法。

config 物件:存放Servlet配置資訊,是javax.servlet.ServletConfig介面的例項,方法:
Enumeration getInitParameterNames()         返回所有初始化引數的名稱
public String getInitParameter(name)         返回指定初始化引數的值


作用域物件
session 物件:處理會話狀況,此物件實現javax.servlet.http.HttpSession介面,方法:

String getId()                       取出sessionID
boolean isNew()                       判斷session是否是新的
void invalidate()                     清除session物件,釋放資源
long getCreationTime()                 取出session生成的時間
long getLastAccessedTime()               取出最後一次session送出請求的時間
void setMaxInactiveInterval()             設定session失效的最長時間
long getMaxInactiveInterval()             取出session失效的最長時間


application物件:儲存執行文件的環境資訊,每個頁面都有一個application的物件此物件實現javax.servlet.ServletContext 介面,方法:

int getMajorVersion()                   取出Servlet容器支援的Servlet API主要版本
int getMinorVersion()                   取出Servlet次要的Servlet API次要版本
String getServerInfo()                   取出Servlet的名稱和版本
String getMimeType(String file)             取出指定檔案的MIME 型別
ServletContext getContext(String uripath)     取出本地URL
URL getResource(path)                   取出指定資源的URL
String getRealPath(String path)             取出本地URL的絕對路徑
void log(String message)                 將資訊寫入日誌檔案中
void log(String message, exception)         將資訊寫入日誌檔案中,並寫入指定的異常資訊


pageContext物件:描述某個Jsp文件的執行環境,也可控制從當前頁面傳輸至其它頁面的方法,方法:

pageContext作用域變數
PAGE_SCOPE                           儲存在pageContext物件中的屬性的作用域
REQUEST_SCOPE                         儲存在request物件中的屬性的作用域
SESSION_SCOPE                         儲存在session物件中的屬性的作用域
APPLICATION_SCOPE                     儲存在application物件中的屬性的作用域

pageContext隱式物件的方法
Object getPage()                       返回當前的Servlet例項
JspWriter getOut()                     返回此頁的輸出流(out物件)
Exception getException()                 返回此頁的異常(exception物件)
HttpSession getSession()                 返回此頁的請求關聯的會話(session物件)
ServletRequest getRequest()               返回觸發頁的請求(request物件)
ServletResponse getResponse()             返回觸發頁的響應(response物件)
ServletConfig getServletConfig( )           返回此頁的Servlet配置物件(config物件)
ServletContext getServletContext( )         返回此頁的執行環境(application物件)

pageContext請求排程方法
void forward(path)                     將處理轉發到本地的另一個URL
void include(path)                     包括另一個本地URL的輸出結果
void removeAttribute(String name)           清除與特定作用域中的屬性名關聯的值
Object findAttribute(String name)           搜尋在所有作用域中屬性名稱
void setAttribute(key,value,scope)           將值與指定的作用域中的屬性名關聯
int getAttributesScope(String name)         返回儲存指定屬性的作用域
Object getAttribute(String name, int scope)     返回與特定作用域中的屬性名關聯的值
Enumeration getAttributeNamesInScope(int scope) 返回指定的作用域中所有屬性名稱


exception物件
exception 物件:它是處理Jsp頁面錯誤資訊,此物件只能在Jsp錯誤頁裡使用,並在page指令中定義<%@ page isErrorPage="true" %>才能用,它是java.lang.Throwable類的例項,方法:
String getMessage()                     返回此異常的錯誤資訊
String toString()                     返回一個包含異常名及錯誤訊息的字串
void printStackTrace(out)                 向指定的輸出流輸出堆疊跟蹤