jsp 9大內置對象

內置對象:
Request,Response,Out,Session,Application,Cookie,Config,Page,Exception。

1、Request對象

    該對象封裝了用戶提交的信息,通過調用該對象相應的方法可以獲取封裝的信息,即使用該對象可以
獲取用戶提交的信息。
    當Request對象獲取客戶提交的漢字字符時,會出現亂碼問題,必須進行特殊處理。首先,將獲取的
字符串用ISO-8859-1進行編碼,並將編碼存發島一個字節數組中,然後再將這個數組轉化爲字符串對象
即可。如下:

String textContent=request.getParameter("boy");
byte b[]=textContent.getBytes("ISO-8859-1");
textContent=new String(b);

  • Request常用的方法:
    1.01 getParameter(String strTextName) 獲取表單提交的信息。

String strName=request.getParameter("name");

    1.02 getProtocol() 獲取客戶使用的協議。

String strProtocol=request.getProtocol();

    1.03 getServletPath() 獲取客戶提交信息的頁面。

String strServlet=request.getServletPath();

    1.04 getMethod() 獲取客戶提交信息的方式,get|post。

String strMethod = request.getMethod();

    1.05 getHeade() 獲取HTTP頭文件中的accept、accept-encoding和Host的值。

String strHeader = request.getHeader("accept");

    1.06 getRermoteAddr() 獲取客戶的IP地址。

String strIP = request.getRemoteAddr();

    1.07 getRemoteHost() 獲取客戶機的名稱。

String clientName = request.getRemoteHost();

    1.08 getServerName() 獲取服務器名稱。

String serverName = request.getServerName();

    1.09 getServerPort() 獲取服務器的端口號。

int serverPort = request.getServerPort();

    1.10 getParameterNames() 獲取客戶端提交的所有參數的名字。

Enumeration enum = request.getParameterNames();
while(enum.hasMoreElements()){
    String s=(String)enum.nextElement();
    out.println(s);
}

2、Response對象

    對客戶的請求做出動態的響應,向客戶端發送數據。
    2.1 動態響應contentType屬性
    當一個用戶訪問一個JSP頁面時,如果該頁面用page指令設置頁面的contentType屬性時text/html,那麼JSP引擎將按照這個屬性值做出反應。如果要動態改變這換個屬性值來響應客戶,就需要使用Response對象的setContentType(String s)方法來改變contentType的屬性值。
    格式:response.setContentType(String s);
    參數s可取text/html,application/x-msexcel,application/msword等。
    2.2 Response重定向
    在某些情況下,當響應客戶時,需要將客戶重新引導至另一個頁面,可以使用Response的sendRedirect(URL)方法實現客戶的重定向。例如:

response.sendRedirect("index.jsp");

3、Session對象


    (1)什麼是Session對象
    Session對象是一個JSP內置對象,它在第一個JSP頁面被裝載時自動創建,完成會話期管理。從一個客戶打開瀏覽器並連接到服務器開始,到客戶關閉瀏覽器離開這個服務器結束,被稱爲一個會話。當一個客戶訪問一個服務器時,可能會在這個服務器的幾個頁面之間切換,服務器應當通過某種辦法知道這是一個客戶,就需要Session對象。
    (2)Session對象的ID
    當一個客戶首次訪問服務器上的一個JSP頁面時,JSP引擎產生一個Session對象,同時分配一個String類型的ID號,JSP引擎同時將這換個ID號發送到客戶端,存放在Cookie中,這樣Session對象,直到客戶關閉瀏覽器後,服務器端該客戶的Session對象才取消,並且和客戶的會話對應關係消失。當客戶重新打開瀏覽器再連接到該服務器時,服務器爲該客戶再創建一個新的Session對象。
    (3)Session對象的常用方法
    ● public String getId():獲取Session對象編號。
    ● public void setAttribute(String key,Object obj):將參數Object指定的對象obj添加到Session對象中,併爲添加的對象指定一個索引關鍵字。
    ● public Object getAttribute(String key):獲取Session對象中含有關鍵字的對象。
    ● public Boolean isNew():判斷是否是一個新的客戶。


4、Application對象


    (1)什麼時Application對象
    服務器啓動後就產生了這個Application對象,當客戶再所訪問的網站的各個頁面之間瀏覽時,這個Application對象都時同一個,直到服務器關閉。但是與Session對象不同的時,所有客戶的Application對象都時同一個,即所有客戶共享這個內置的Application對象。
    (2)Application對象的常用方法
    ● setAttribute(String key,Object obj):將參數Object指定的對象obj添加到Application對象中,併爲添加的對象指定一個索引關鍵字。
    ● getAttribute(String key):獲取Application對象中含有關鍵字的對象。


5、Out對象


   Out對象時一個輸出流,用來向客戶端輸出數據。Out對象用於各種數據的輸出。其常用方法如下。
    ● out.print():輸出各種類型數據。
    ● out.newLine():輸出一個換行符。
    ● out.close():關閉流。


6、Cookie對象


    (1)什麼是Cookie
    Cookie是Web服務器保存在用戶硬盤上的一段文本。Cookie允許一個Web站點在用戶電腦上保存信息並且隨後再取回它。
    舉例來說,一個Web站點可能會爲每一個訪問者產生一個唯一的ID,然後以Cookie文件的形式保存在每個用戶的機器上。
    如果用戶使用IE瀏覽器訪問Web,用戶就會看到所有保存在自己硬盤上的Cookie。它們最常存放的地方是:C:/Windows/Cookies。Cookie是以“關鍵字key=值value”的格式來保存記錄的。
    (2)創建一個Cookie對象
    調用Cookie對象的構造函數就可以創建Cookie對象。Cookie對象的構造函數有兩個字符串參數:Cookie名字和Cookie值。
    例如:Cookie c = new Cookie("username","john");
    (3)將Cookie對象傳送到客戶端
    在JSP中,如果要將封裝好的Cookie對象傳送到客戶端,可使用Response對象的addCookie()方法。
    例如:response.addCookie(c)。
    (4)讀取保存到客戶端的Cookie
    使用Request對象的getCookie()方法,執行時將所有客戶端傳來的Cookie對象以數組的形式排列,如果要取出符合需要的Cookie對象,就需要循環比較數組內每個對象的關鍵字。
    例如:

Cookie[] c = request.getCookies();
if(c != null)
 for(int i = 0;i < c.length;i++){
       if("username".equals(c.getName()))
           out.println(c.getValue());
    }

    (5)設置Cookie對象的有效時間
    調用Cookie對象的setMaxAge()方法便可以設置Cookie對象的有效時間,
    例如:Cookie c = new Cookie("username","john");
          c.setMaxAge(3600);
    (6)Cookie應用
    Cookie對象的典型應用時用來統計網站的訪問人數。由於代理服務器、緩存等的使用,唯一能幫助網站精確統計來訪人數的方法就是爲每個訪問者建立一個唯一ID。使用Cookie,網站可以完成一下工作。
    ● 測定多少人訪問過。
    ● 測定訪問者有多少是新用戶(即第一次來訪),多少是老用戶。
    ● 測定一個用戶多久訪問一次網站
    當一個用戶第一次訪問時,網站在數據庫中建立一個新的ID,並把ID通過Cookie傳送給用戶。用戶再次來訪時,網站把該用戶ID對應的計數器加1,得到用戶的來訪次數。

7、Config對象


    配置對象


8、Page對象

    頁面對象。

  PageContext對象

    頁面上下文對象
    Jsp引入了一個名位PageContext的類,通過它可以訪問頁面的許多屬性。
    PageContext類擁有getRequest,getResponse,getOut,getSession等方法。
    pageContext變量存儲與當前頁面相關聯的PageContext對象的值。
    補:
    如果方法需要訪問多個與頁面相關的對象,
傳遞pageContext要比傳遞request,response,out等的獨立引用更容易。(雖然兩種方式都能達到同樣的目的)

9、Exception對象

     在處理異常的網頁中可以直接訪問exception隱式對象。

-----------------------------------補充部分--------------------------------

 

① out - javax.servlet.jsp.jspWriter
   out對象用於把結果輸出到網頁上。

方法:
1. void clear() ;
   清除輸出緩衝區的內容,但是不輸出到客戶端。

2. void clearBuffer() ;
   清除輸出緩衝區的內容,並輸出到客戶端。

3. void close() ;
   關閉輸出流,清除所有內容。

4. void flush() ;
   輸出緩衝區裏面的數據。

5. int getBufferSize() ;
   獲取以kb爲單位的目前緩衝區大小。

6. int getRemaining() ;
   獲取以kb爲單位的緩衝區中未被佔用的空間大小。

7. boolean isAutoFlush() ;
   是否自動刷新緩衝區。

8. void newLine() ;
   輸出一個換行字符。

9. void print( boolean b ) ;
   void print( char c ) ;
   void print( char[] s ) ;
   void print( double d ) ;
   void print( float f ) ;
   void print( int i ) ;
   void print( long l ) ;
   void print( Object obj ) ;
   void print( String s ) ;
   將指定類型的數據輸出到Http流,不換行。

10. void println( boolean b ) ;
    void println( char c ) ;
    void println( char[] s ) ;
    void println( double d ) ;
    void println( float f ) ;
    void println( int i ) ;
    void println( long l ) ;
    void println( Object obj ) ;
    void println( String s ) ;
    將指定類型的數據輸出到Http流,並輸出一個換行符。
   
11. Appendable append( char c ) ;
    Appendable append( CharSequence cxq, int start, int end ) ;
    Appendable append( CharSequence cxq ) ;
    將一個字符或者實現了CharSequence接口的對象添加到輸出流的後面。

成員:
int DEFAULT_BUFFER = 0    - 缺省緩衝區大小
int NO_BUFFER = -1        - writer是否處於緩衝輸出狀態
int UNBOUNDED_BUFFER = -2 - 是否限制緩衝區大小


② request - javax.servlet.http.HttpServletRequest
   request對象包含所有請求的信息,如請求的來源、標頭、cookies和請求相關的參數值等。

方法:
1. Object getAttribute( String name ) ;
   返回由name指定的屬性值,該屬性不存在時返回null。

2. Enumeration getAttributeNames() ;
   返回request對象的所有屬性名稱的集合。

3. String getAuthType() ;
   返回用來保護servlet的認證方法的名稱,未受保護時返回null。

4. String getCharacterEncoding() ;
   返回請求中的字符編碼方法,可以在response對象中設置。

5. int getContentLength() ;
   返回請求的BODY的長度,不能確定長度時返回-1。可以在response中設置。

6. String getContentType() ;
   返回在response中定義的內容類型。

7. String getContentPath() ;
   返回請求的路徑。

8. Cookie[] getCookies() ;
   返回客戶端所有的Cookie的數組。

9. Enumeration getHeaderNames() ;
   返回所有HTTP頭的名稱的集合。

10. Enumeration getHeaders( String name ) ;
    返回指定HTTP頭的所有值的集合。

11. String getHeader( String name ) ;
    返回指定名稱的HTTP頭的信息。

12. long getDateHeader( String name ) ;
    返回指定名稱的Data類型的HTTP頭的信息。

13. int getIntHeader( String name ) ;
    返回指定名稱的Int類型的HTTP頭的信息。

14. ServletInputStream getInputStream() ;
    返回請求的輸入流。

15. Locale getLocale() ;
    返回當前頁的Locale對象,可以在response中設定。

16. Enumeration getLocales() ;
    返回請求中所有的Locale對象的集合。

17. String getLocalName() ;
    獲取響應請求的服務器端主機名。

18. String getLocalAddr() ;
    獲取響應請求的服務器端地址。

19. int getLocalPort() ;
    獲取響應請求的服務器端端口

20. String getMethod() ;
    獲取客戶端向服務器端發送請求的方法(GET、POST)。

21. String getParameter( String name ) ;
    獲取客戶端發送給服務器端的參數值。

22. Map getParameterMap() ;
    該方法返回包含請求中所有參數的一個Map對象。

23. Enumeration getParameterNames() ;
    返回請求中所有參數的集合。

24. String[] getParameterValues( String name ) ;
    獲得請求中指定參數的所有值。

25. String getQueryString() ;
    返回get方法傳遞的參數字符串,該方法不分解出單獨的參數。

26. String getPathInfo() ;
    取出請求中處於ServletPath和QueryString之間的額外信息。

27. String getPathTranslated() ;
    返回用getPathInfo()方法取得的路徑信息的實際路徑。

28. String getProtocol() ;
    返回請求使用的協議。可以是HTTP1.1或者HTTP1.0。

29. BufferedReader getReader() ;
    返回請求的輸入流對應的Reader對象,該方法和getInputStream()方法在一個頁面中只能調用一個。

30. String getRemoteAddr() ;
    獲取發出請求的客戶端IP地址。

31. String getRemoteHost() ;
    獲取發出請求的客戶端主機名

32. String getRemoteUser() ;
    返回經過客戶端驗證的用戶名,未經驗證返回null。

33. int getRemotePort() ;
    返回發出請求的客戶端主機端口。

34. String getRealPath( String path ) ;
    返回給定虛擬路徑的物理路徑。

35. RequestDispatcher getRequestDispatcher( String path ) ;
    按給定的路徑生成資源轉向處理適配器對象。

36. String getRequestedSessionId() ;
    返回請求的session的標識。

37. String RequestURI() ;
    返回發出請求的客戶端地址,但是不包括請求的參數字符串。

38. StringBuffer getRequestURI() ;
    返回響應請求的服務器端地址

39. String getScheme() ;
    獲取協議名稱,缺省值爲HTTP協議。

40. String getServerName() ;
    返回響應請求的服務器名稱。

41. String getServletPath() ;
    獲取客戶端所請求的腳本文件的文件路徑。

42. int getServerPort() ;
    獲取響應請求的服務器端主機端口號。

43. void removeAttribute( String name ) ;
    在屬性列表中刪除指定名稱的屬性。

44. void setAttribute( String name, Object value ) ;
    在屬性列表中添加/刪除指定的屬性。

45. void setCharacterEncoding( String name ) ;
    設置請求的字符編碼格式。

46. HttpSession getSession() ;
    HttpSession getSession( boolean create ) ;
    獲取session,如果create爲true,在無session的情況下創建一個。
   
47. boolean isRequestedSessionIdFromCookie() ;
    檢查請求的會話ID是否爲通過Cookie傳入。

48. boolean isRequestedSessionIdFromURL() ;
    檢查請求的會話ID是否爲通過URL傳入。

49. boolean isRequestedSessionIdValid() ;
    檢查請求的會話ID是否仍然有效。

50. boolean isSecure() ;
    檢查請求是否使用安全鏈接,如果HTTPS等。

51. boolean isUserInRole( String role ) ;
    檢查已經通過驗證的用戶是否在是role所指定的角色。

52. Principal getUserPrincipal() ;
    返回包含用戶登陸名的一個java.security.Principal對象。

成員:
String BASIC_AUTH = "BASIC"             -
String CLIENT_CERT_AUTH = "CLIENT_CERT" -
String DIGEST_AUTH = "DIGEST"           -
String FORM_AUTH = "FORM"               -


③ response - javax.servlet.http.HttpServletResponse
   response對象主要將JSP容器處理後的結果傳回到客戶端。

方法:
1. void addCookie( Cookie cookie ) ;
   添加一個Cookie對象,保存客戶端信息。

2. void addDateHeader( String name, long value ) ;
   添加一個日期類型的HTTP頭信息,覆蓋同名的HTTP頭信息。

3. void addHeader( String name, String value ) ;
   添加一個HTTP頭,覆蓋同名的舊HTTP頭。

4. void addIntHeader( String name, int value ) ;
   添加一個整型的HTTP頭,覆蓋同名的舊HTTP頭。

5. boolean containsHeader( String name ) ;
   判斷指定的HTTP頭是否存在。

6. String encodeRedirectURL( String url ) ;
   對sendRedirect()方法使用的URL進行編碼。

7. String encodeURL( String url ) ;
   將URL予以編碼,回傳包含session ID的URL。
  
8. void flushBuffer() ;
   強制把當前緩衝區的內容發送到客戶端。

9. int getBufferSize() ;
   取得以kb爲單位的緩衝區大小。

10. String getCharacterEncoding() ;
    獲取響應的字符編碼格式。

11. String getContentType() ;
    獲取響應的類型。

12. Locale getLocale() ;
    獲取響應的Locale對象。

13. ServletOutputStream getOutputStream() ;
    返回客戶端的輸出流對象。

14. PrintWriter getWriter() ;
    獲取輸出流對應的writer對象。

15. boolean isCommitted() ;
    判斷服務器端是否已經將數據輸出到客戶端。

16. void reset() ;
    清空buffer中的所有內容。

17. void resetBuffer() ;
    情況buffer中所有的內容,但是保留HTTP頭和狀態信息。

18. void sendError( int xc, String msg ) ;
    void sendError( int xc ) ;
    發送錯誤,包括狀態碼和錯誤信息。

19. void sendRedirect( String locationg ) ;
    把響應發送到另外一個位置進行處理。

20. void setBufferSize( int size ) ;
    設置以kb爲單位的緩衝區大小。

21. void setCharacterEncoding( String charset ) ;
    設置響應使用的字符編碼格式。

22. void setContentLength( int length ) ;
    設置響應的BODY長度。

23. void setContentType( String type ) ;
    設置響應的類型。

24. void setDateHeader( String name, long value ) ;
    設置指定名稱的Data類型的HTTP頭的值。

25. void setHeader( String name, String value ) ;
    設置指定名稱的HTTP頭的值。

26. void setIntHeader( String name, int value ) ;
    設置指定名稱的int類型的HTTP頭的值。

27. void setStatus( int xc ) ;
    設置響應狀態碼,新值會覆蓋當前值。

成員(HTTP狀態碼):
int SC_CONTINUE = 100                      int SC_SWITCHING_PROTOCOLS = 101
int SC_OK = 200                            int SC_NON_AUTHORITATIVE_INFORMATION = 203
int SC_ACCEPTED = 202                      int SC_CREATED = 201
int SC_NO_CONTENT = 204                    int SC_RESET_CONTENT = 205
int SC_PARTIAL_CONTENT = 206               int SC_MULTIPLE_CHOICES = 300
int SC_MOVED_PERMANENTLY = 301             int SC_MOVED_TEMPORARILY = 302
int SC_FOUND = 302                         int SC_SEE_OTHER = 303
int SC_NOT_MODIFIED = 304                  int SC_USE_PROXY = 305
int SC_TEMPORARY_REDIRECT = 307            int SC_BAD_REQUEST = 400
int SC_UNAUTHORIZED = 401                  int SC_PAYMENT_REQUIRED = 402
int SC_FORBIDDEN = 403                     int SC_NOT_FOUND = 404
int SC_METHOD_NOT_ALLOWED = 405            int SC_NOT_ACCEPTABLE = 406
int SC_PROXY_AUTHENTICATION_REQUIRED = 407 int SC_REQUEST_TIMEOUT = 408
int SC_CONFLICT = 409                      int SC_GONE = 410
int SC_LENGTH_REQUIRED = 411               int SC_PRECONDITION_FAILED = 412
int SC_REQUEST_ENTITY_TOO_LARGE = 413      int SC_REQUEST_URI_TOO_LONG = 414
int SC_UNSUPPORTED_MEDIA_TYPE = 415        int SC_REQUESTED_RANGE_NOT_SATISFIABLE = 416
int SC_EXPECTATION_FAILED = 417            int SC_INTERNAL_SERVER_ERROR = 500
int SC_NOT_IMPLEMENTED = 501               int SC_BAD_GATEWAY = 502
int SC_SERVICE_UNAVAILABLE = 503           int SC_GATEWAY_TIMEOUT = 504
int SC_HTTP_VERSION_NOT_SUPPORTED = 505


④ session - javax.servlet.http.HttpSession
   session對象表示目前個別用戶的會話狀態,用來識別每個用戶。

方法:
1. Object getAttribute( String name ) ;
   獲取與指定名字相關聯的session屬性值。

2. Enumeration getAttributeNames() ;
   取得session內所有屬性的集合。

3. long getCreationTime() ;
   返回session的創建時間,最小單位千分之一秒。

4. String getId() ;
   取得session標識。

5. long getLastAccessedTime() ;
   返回與當前session相關的客戶端最後一次訪問的時間,由1970-01-01算起,單位毫秒。

6. int getMaxInactiveInterval( int interval ) ;
   返回總時間,以秒爲單位,表示session的有效時間(session不活動時間)。-1爲永不過期。

7. ServletContext getServletContext() ;
   返回一個該JSP頁面對應的ServletContext對象實例。

8. HttpSessionContext getSessionContext() ;
  

9. Object getValue( String name ) ;
   取得指定名稱的session變量值,不推薦使用。

10. String[] getValueNames() ;
    取得所有session變量的名稱的集合,不推薦使用。

11. void invalidate() ;
    銷燬這個session對象。

12. boolean isNew() ;
    判斷一個session是否由服務器產生,但是客戶端並沒有使用。

13. void pubValue( String name, Object value ) ;
    添加一個session變量,不推薦使用。

14. void removeValue( String name ) ;
    移除一個session變量的值,不推薦使用。

15. void setAttribute( String name, String value ) ;
    設置指定名稱的session屬性值。

16. void setMaxInactiveInterval( int interval ) ;
    設置session的有效期。

17. void removeAttribute( String name ) ;
    移除指定名稱的session屬性。


⑤ pageContext - javax.servlet.jsp.PageContext
   pageContext對象存儲本JSP頁面相關信息,如屬性、內建對象等。

方法:
1. void setAttribute( String name, Object value, int scope ) ;
   void setAttribute( String name, Object value ) ;
   在指定的共享範圍內設置屬性。

2. Object getAttribute( String name, int scope ) ;
   Object getAttribute( String name ) ;
   取得指定共享範圍內以name爲名字的屬性值。

3. Object findAttribute( String name ) ;
   按頁面、請求、會話和應用程序共享範圍搜索已命名的屬性。

4. void removeAttribute( String name, int scope ) ;
   void removeAttribute( String name ) ;
   移除指定名稱和共享範圍的屬性。

5. void forward( String url ) ;
   將頁面導航到指定的URL。

6. Enumeration getAttributeNamesScope( int scope ) ;
   取得指定共享範圍內的所有屬性名稱的集合。

7. int getAttributeScope( String name ) ;
   取得指定屬性的共享範圍。

8. ErrorData getErrorDate() ;
   取得頁面的errorData對象。

9. Exception getException() ;
   取得頁面的exception對象。

10. ExpressionEvaluator getExpressionEvaluator() ;
    取得頁面的expressionEvaluator對象。

11. JspWriter getOut() ;
    取得頁面的out對象。

12. Object getPage() ;
    取得頁面的page對象。

13. ServletRequest getRequest() ;
    取得頁面的request對象。

14. ServletResponse getResponse() ;
    取得頁面的response對象。

15. ServletConfig getConfig() ;
    取得頁面的config對象。

16. ServletContext getServletContext() ;
    取得頁面的servletContext對象。

17. HttpSession getSession() ;
    取得頁面的session對象。

18. VariableResolver getVariableResolver() ;
    取得頁面的variableResolver對象。

19. void include( String url, boolean flush ) ;
    void include( String url ) ;
    包含其他的資源,並指定是否自動刷新。

20. void release() ;
    重置pageContext內部狀態,釋放所有內部引用。

21. void initialize( Servlet servlet, ServletRequest request, ServletResponse response,
                     String errorPageURL, boolean needSession, int bufferSize, boolean autoFlush ) ;
    初始化未經初始化的pageContext對象。

22. BodyContext pushBody() ;
    BodyContext pushBody( Writer writer ) ;
    保存當前的out對象,並更新pageContext中page範圍內的out對象。

23. JspWrite popBody() ;
    取出由pushBody()方法保存的out對象。

24. void handlePageException( Exception e ) ;
    void handlePageException( Thrwoable t ) ;
   

成員:
int PAGE_SCOPE = 1        - 頁面共享範圍
int REQUEST_SCOPE = 2     - 請求共享範圍
int SESSION_SCOPE = 3     - 會話共享範圍
int APPLICATION_SCOPE = 4 - 應用程序共享範圍
String PAGE = "javax.servlet.jsp.jspPage"
String PAGECONTEXT = "javax.servlet.jsp.jspPageContext"
String REQUEST = "javax.servlet.jsp.jspRequest"
String RESPONSE = "javax.servlet.jsp.jspResponse"
String CONFIG = "javax.servlet.jsp.jspConfig"
String SESSION = "javax.servlet.jsp.jspSession"
String OUT = "javax.servlet.jsp.jspOut"
String APPLICATION = "javax.servlet.jsp.jspApplication"
String EXCEPTION = "javax.servlet.jsp.jspException"


⑥ application - javax.servlet.ServletContext
   application主要功用在於取得或更改Servlet的設定。

方法:
1. Object getAttribute( String name ) ;
   返回由name指定的application屬性。

2. Enumeration getAttributes() ;
   返回所有的application屬性。

3. ServletContext getContext( String uripath ) ;
   取得當前應用的ServletContext對象。

4. String getInitParameter( String name ) ;
   返回由name指定的application屬性的初始值。

5. Enumeration getInitParameters() ;
   返回所有的application屬性的初始值的集合。

6. int getMajorVersion() ;
   返回servlet容器支持的Servlet API的版本號。

7. String getMimeType( String file ) ;
   返回指定文件的類型,未知類型返回null。一般爲"text/html"和"image/gif"。

8. int getMinorVersion() ;
   返回servlet容器支持的Servlet API的副版本號。

9. String getRealPath( String path ) ;
   返回給定虛擬路徑所對應物理路徑。

10. RequestDispatcher getNamedDispatcher( String name ) ;
    爲指定名字的Servlet對象返回一個RequestDispatcher對象的實例。

11. RequestDispatcher getRequestDispatcher( String path ) ;
    返回一個RequestDispatcher對象的實例。

12. URL getResource( String path ) ;
    返回指定的資源路徑對應的一個URL對象實例,參數要以"/"開頭。

13. InputStream getResourceAsStream( String path ) ;
    返回一個由path指定位置的資源的InputStream對象實例。

14. Set getResourcePaths( String path ) ;
    返回存儲在web-app中所有資源路徑的集合。

15. String getServerInfo() ;
    取得應用服務器版本信息。

16. Servlet getServlet( String name ) ;
    在ServletContext中檢索指定名稱的servlet。

17. Enumeration getServlets() ;
    返回ServletContext中所有servlet的集合。

18. String getServletContextName() ;
    返回本web應用的名稱。

19. Enumeration getServletContextNames() ;
    返回ServletContext中所有servlet的名稱集合。

20. void log( Exception ex, String msg ) ;
    void log( String msg, Throwable t ) ;
    void log( String msg ) ;
    把指定的信息寫入servlet log文件。

21. void removeAttribute( String name ) ;
    移除指定名稱的application屬性。

22. void setAttribute( String name, Object value ) ;
    設定指定的application屬性的值。


⑦ config - javax.servlet.ServletConfig
   config對象用來存放Servlet初始的數據結構。

方法:
1. String getInitParameter( String name ) ;
   返回名稱爲name的促使參數的值。

2. Enumeration getInitParameters() ;
   返回這個JSP所有的促使參數的名稱集合。

3. ServletContext getContext() ;
   返回執行者的servlet上下文。

4. String getServletName() ;
   返回servlet的名稱。


⑧ exception - java.lang.Throwable
   錯誤對象,只有在JSP頁面的page指令中指定isErrorPage="true"後,纔可以在本頁面使用exception對象。

方法:
1. Throwable fillInStackTrace() ;
   將當前stack信息記錄到exception對象中。

2. String getLocalizedMessage() ;
   取得本地語系的錯誤提示信息。

3. String getMessage()
   取得錯誤提示信息。

4. StackTrackElement[] getStackTrace() ;
   返回對象中記錄的call stack track信息。

5. Throwable initCause( Throwable cause ) ;
   將另外一個異常對象嵌套進當前異常對象中。
  
6. Throwable getCause() ;
   取出嵌套在當前異常對象中的異常。

7. void printStackTrace() ;
   void printStackTrace( printStream s ) ;
   void printStackTrace( printWriter s ) ;
   打印出Throwable及其call stack trace信息。

8. void setStackTrace( StackTraceElement[] stackTrace )
   設置對象的call stack trace信息。


⑨ page - javax.servlet.jsp.HttpJspPage
   page對象代表JSP對象本身,或者說代表編譯後的servlet對象,
   可以用( (javax.servlet.jsp.HttpJspPage)page )來取用它的方法和屬性。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章