1. 程式人生 > >jsp內建物件和方法

jsp內建物件和方法

SP內建物件(9個常用的內建物件)

   1.request物件

客戶端的請求資訊被封裝在request物件中,通過它才能瞭解到客戶的需求,

然後做出響應。它是HttpServletRequest類的例項。

序號方法說明

  1. objectgetAttribute(Stringname) 返回指定屬性的屬性值
  2. EnumerationgetAttributeNames() 返回所有可用屬性名的列舉
  3. StringgetCharacterEncoding() 返回字元編碼方式
  4. intgetContentLength() 返回請求體的長度(以位元組數)
  5. StringgetContentType() 得到請求體的MIME型別
  6. ServletInputStreamgetInputStream() 得到請求體中一行的二進位制流
  7. StringgetParameter(Stringname) 返回name指定引數的引數值
  8. EnumerationgetParameterNames() 返回可用引數名的列舉
  9. String[]getParameterValues(Stringname) 返回包含引數name的所有值的陣列
  10. StringgetProtocol() 返回請求用的協議型別及版本號
  11. StringgetScheme() 返回請求用的計劃名,如:http.https及ftp等
  12. StringgetServerName() 返回接受請求的伺服器主機名
  13. intgetServerPort() 返回伺服器接受此請求所用的埠號
  14. BufferedReadergetReader() 返回解碼過了的請求體
  15. StringgetRemoteAddr() 返回傳送此請求的客戶端IP地址
  16. StringgetRemoteHost() 返回傳送此請求的客戶端主機名
  17. voidsetAttribute(Stringkey,Objectobj) 設定屬性的屬性值
  18. StringgetRealPath(Stringpath) 返回一虛擬路徑的真實路徑
  19.    2.response物件
  20. response物件包含了響應客戶請求的有關資訊,但在JSP中很少直接用到它。
  21. 它是HttpServletResponse類的例項。
  22. 序號方法說明
  23. 1StringgetCharacterEncoding() 返回響應用的是何種字元編碼
  24. 2ServletOutputStreamgetOutputStream() 返回響應的一個二進位制輸出流
  25. 3PrintWritergetWriter() 返回可以向客戶端輸出字元的一個物件
  26. 4voidsetContentLength(intlen) 設定響應頭長度
  27. 5voidsetContentType(Stringtype) 設定響應的MIME型別
  28. 6sendRedirect(java.lang.Stringlocation) 重新定向客戶端的請求

   3.session物件

session物件指的是客戶端與伺服器的一次會話,從客戶連到伺服器的一個

WebApplication開始,直到客戶端與伺服器斷開連線為止。它是HttpSession

類的例項.

序號方法說明

  1. longgetCreationTime() 返回SESSION建立時間
  2. publicStringgetId() 返回SESSION建立時JSP引擎為它設的惟一ID號
  3. longgetLastAccessedTime() 返回此SESSION裡客戶端最近一次請求時間
  4. intgetMaxInactiveInterval() 返回兩次請求間隔多長時間此SESSION被取
  5. 消(ms)
  6. String[]getValueNames() 返回一個包含此SESSION中所有可用屬性的數
  7. voidinvalidate() 取消SESSION,使SESSION不可用
  8. booleanisNew() 返回伺服器建立的一個SESSION,客戶端是否已經加入
  9. voidremoveValue(Stringname) 刪除SESSION中指定的屬性
  10. voidsetMaxInactiveInterval() 設定兩次請求間隔多長時間此SESSION被取

消(ms)

   4.out物件

  1. out物件是JspWriter類的例項,是向客戶端輸出內容常用的物件
  2. 序號方法說明
  3. voidclear() 清除緩衝區的內容
  4. voidclearBuffer() 清除緩衝區的當前內容
  5. voidflush() 清空流
  6. intgetBufferSize() 返回緩衝區以位元組數的大小,如不設緩衝區則為0
  7. intgetRemaining() 返回緩衝區還剩餘多少可用
  8. booleanisAutoFlush() 返回緩衝區滿時,是自動清空還是丟擲異常
  9. voidclose() 關閉輸出流

   5.page物件

  1. page物件就是指向當前JSP頁面本身,有點象類中的this指標,它是
  2. java.lang.Object類的例項
  3. 序號方法說明
  4. classgetClass 返回此Object的類
  5. inthashCode() 返回此Object的hash碼
  6. booleanequals(Objectobj) 判斷此Object是否與指定的Object物件相等
  7. voidcopy(Objectobj) 把此Object拷貝到指定的Object物件中
  8. Objectclone() 克隆此Object物件
  9. StringtoString() 把此Object物件轉換成String類的物件
  10. voidnotify() 喚醒一個等待的執行緒
  11. voidnotifyAll() 喚醒所有等待的執行緒
  12. voidwait(inttimeout) 使一個執行緒處於等待直到timeout結束或被喚醒
  13. voidwait() 使一個執行緒處於等待直到被喚醒
  14. voidenterMonitor() 對Object加鎖
  15. voidexitMonitor() 對Object開鎖

   6.application物件

application物件實現了使用者間資料的共享,可存放全域性變數。它開始於伺服器

的啟動,直到伺服器的關閉,在此期間,此物件將一直存在;這樣在使用者的前

後連線或不同使用者之間的連線中,可以對此物件的同一屬性進行操作;在任何

地方對此物件屬性的操作,都將影響到其他使用者對此的訪問。伺服器的啟動和

關閉決定了application物件的生命。它是ServletContext類的例項。

序號方法說明

  1. ObjectgetAttribute(Stringname) 返回給定名的屬性值
  2. EnumerationgetAttributeNames() 返回所有可用屬性名的列舉
  3. voidsetAttribute(Stringname,Objectobj) 設定屬性的屬性值
  4. voidremoveAttribute(Stringname) 刪除一屬性及其屬性值
  5. StringgetServerInfo() 返回JSP(SERVLET)引擎名及版本號
  6. StringgetRealPath(Stringpath) 返回一虛擬路徑的真實路徑
  7. ServletContextgetContext(Stringuripath) 返回指定WebApplication的application物件
  8. intgetMajorVersion() 返回伺服器支援的ServletAPI的最大版本號
  9. intgetMinorVersion() 返回伺服器支援的ServletAPI的最大版本號
  10. StringgetMimeType(Stringfile) 返回指定檔案的MIME型別
  11. URLgetResource(Stringpath) 返回指定資源(檔案及目錄)的URL路徑
  12. InputStreamgetResourceAsStream(Stringpath) 返回指定資源的輸入流
  13. RequestDispatchergetRequestDispatcher(Stringuripath) 返回指定資源的RequestDispatcher物件
  14. ServletgetServlet(Stringname) 返回指定名的Servlet
  15. EnumerationgetServlets() 返回所有Servlet的列舉
  16. EnumerationgetServletNames() 返回所有Servlet名的列舉
  17. voidlog(Stringmsg) 把指定訊息寫入Servlet的日誌檔案
  18. voidlog(Exceptionexception,Stringmsg) 把指定異常的棧軌跡及錯誤訊息寫入Servlet的日誌檔案
  19. voidlog(Stringmsg,Throwablethrowable) 把棧軌跡及給出的Throwable異常的說明資訊寫入Servlet的日誌檔案

   7.exception物件

exception物件是一個例外物件,當一個頁面在執行過程中發生了例外,就產

生這個物件。如果一個JSP頁面要應用此物件,就必須把isErrorPage設為true,

否則無法編譯。他實際上是Java.lang.Throwable的物件

序號方法說明

  1. StringgetMessage() 返回描述異常的訊息
  2. StringtoString() 返回關於異常的簡短描述訊息
  3. voidprintStackTrace() 顯示異常及其棧軌跡
  4. ThrowableFillInStackTrace() 重寫異常的執行棧軌跡

   8.pageContext物件

pageContext物件提供了對JSP頁面內所有的物件及名字空間的訪問,也就是

說他可以訪問到本頁所在的SESSION,也可以取本頁面所在的application的

某一屬性值,他相當於頁面中所有功能的集大成者,它的本類名也叫

pageContext。

序號方法說明

  1. JspWritergetOut() 返回當前客戶端響應被使用的JspWriter流(out)
  2. HttpSessiongetSession() 返回當前頁中的HttpSession物件(session)
  3. ObjectgetPage() 返回當前頁的Object物件(page)
  4. ServletRequestgetRequest() 返回當前頁的ServletRequest物件(request)
  5. ServletResponsegetResponse() 返回當前頁的ServletResponse物件(response)
  6. ExceptiongetException() 返回當前頁的Exception物件(exception)
  7. ServletConfiggetServletConfig() 返回當前頁的ServletConfig物件(config)
  8. ServletContextgetServletContext() 返回當前頁的ServletContext物件(application)
  9. voidsetAttribute(Stringname,Objectattribute) 設定屬性及屬性值
  10. voidsetAttribute(Stringname,Objectobj,intscope) 在指定範圍內設定屬性及屬性值
  11. publicObjectgetAttribute(Stringname) 取屬性的值
  12. ObjectgetAttribute(Stringname,intscope) 在指定範圍內取屬性的值
  13. publicObjectfindAttribute(Stringname) 尋找一屬性,返回起屬性值或NULL
  14. voidremoveAttribute(Stringname) 刪除某屬性
  15. voidremoveAttribute(Stringname,intscope) 在指定範圍刪除某屬性
  16. intgetAttributeScope(Stringname) 返回某屬性的作用範圍
  17. EnumerationgetAttributeNamesInScope(intscope) 返回指定範圍內可用的屬性名列舉
  18. voidrelease() 釋放pageContext所佔用的資源
  19. voidforward(StringrelativeUrlPath) 使當前頁面重導到另一頁面
  20. voidinclude(StringrelativeUrlPath) 在當前位置包含另一檔案

   9.config物件

config物件是在一個Servlet初始化時,JSP引擎向它傳遞資訊用的,此資訊包括Servlet初始化時所要用到的引數(通過屬性名和屬性值構成)以及伺服器的有關資訊(通過傳遞一個ServletContext物件)

轉載:http://blog.csdn.net/yenange/article/details/5949518