jsp的八個隱含對象

來源:互聯網
上載者:User

一、page 對象
    page對象代表JSP本身,更準確地說它代表JSP被轉譯後的Servlet,它可以調用Servlet類所定義的方法。
   
二、config 對象
    config 對象裡存放著一些Servlet 初始的資料結構。
    config 對象實現於javax.servlet.ServletConfig 介面,它共有下列四種方法:
        public String getInitParameter(name)
        public java.util.Enumeration getInitParameterNames( )
        public ServletContext getServletContext( )
        public Sring getServletName( )

三、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.取得請求參數的方法
        String getParameter(String name)                取得name 的參數值
        Enumeration getParameterNames( )                取得所有的參數名稱
        String [] getParameterValues(String name)         取得所有name 的參數值
        Map getParameterMap( )                             取得一個要求參數的Map
    3.能夠取得請求HTTP 標題的方法
        String getHeader(String name)                     取得name 的標題
        Enumeration getHeaderNames()                     取得所有的標題名稱
        Enumeration getHeaders(String name)             取得所有name 的標題
        int getIntHeader(String name)                     取得整數類型name 的標題
        long getDateHeader(String name)                 取得日期類型name 的標題
        Cookie [] getCookies( )                         取得與請求有關的cookies
    4.其他的方法
        String getContextPath( )                         取得Context 路徑(即月台名稱)
        String getMethod( )                             取得HTTP 的方法(GET、POST)
        String getProtocol( )                             取得使用的協議 (HTTP/1.1、HTTP/1.0 )
        String getQueryString( )                        取得請求的參數字串,不過,HTTP的方法必須為GET
        String getRequestedSessionId( )                 取得使用者端的Session ID
        String getRequestURI( )                            取得請求的URL,但是不包括請求的參數字串
        String getRemoteAddr( )                            取得使用者的IP 位址
        String getRemoteHost( )                            取得使用者的主機名稱
        int getRemotePort( )                            取得使用者的主機連接埠
        String getRemoteUser( )                         取得使用者的名稱
        void etCharacterEncoding(String    encoding)        設定編碼格式,用來解決表單傳遞中文的問題

四、response 對象
    response 對象主要將JSP 處理資料後的結果傳回到用戶端。
    response 對象是實現javax.servlet.http.HttpServletResponse 介面。response對象所提供的方法。
    1.設定表頭的方法
        void addCookie(Cookie cookie)                     新增cookie
        void addDateHeader(String name, long date)        新增long類型的值到name標題
        void addHeader(String name, String value)        新增String類型的值到name標題
        void addIntHeader(String name, int value)         新增int類型的值到name標題
        void setDateHeader(String name, long date)        指定long類型的值到name標題
        void setHeader(String name, String value)        指定String類型的值到name標題
        void setIntHeader(String name, int value)         指定int類型的值到name標題
    2.設定響應狀態代碼的方法
        void sendError(int sc)                             傳送狀態代碼(status code)
        void sendError(int sc, String msg)                傳送狀態代碼和錯誤資訊
        void setStatus(int sc)                             設定狀態代碼
    3.用來URL 重寫(rewriting)的方法   
        String encodeRedirectURL(String    url)            對使用sendRedirect( )方法的URL予以編碼

五、out 對象
    out 對象能把結果輸出到網頁上。
    out主要是用來控制管理輸出的緩衝區(buffer)和輸出資料流(output stream)。
        void clear( )                                     清除輸出緩衝區的內容
        void clearBuffer( )                             清除輸出緩衝區的內容
        void close( )                                     關閉輸出資料流,清除所有的內容
        int getBufferSize( )                             取得目前緩衝區的大小(KB)
        int getRemaining( )                             取得目前使用後還剩下的緩衝區大小(KB)
        boolean isAutoFlush( )                            回傳true表示緩衝區滿時會自動清除;false表示不會自動清除並且產生異常處理
       
六、session 對象
    session對象表示目前個別使用者的會話(session)狀況。
    session對象實現javax.servlet.http.HttpSession介面,HttpSession介面所提供的方法
        long getCreationTime()                            取得session產生的時間,單位是毫秒
        String getId()                                     取得session 的ID
        long getLastAccessedTime()                        取得使用者最後通過這個session送出請求的時間
        long getMaxInactiveInterval()                    取得最大session不活動的時間,若超過這時間,session 將會失效
        void invalidate()                                取消session 對象,並將對象存放的內容完全拋棄
        boolean isNew()                                    判斷session 是否為"新"的
        void setMaxInactiveInterval(int    interval)        設定最大session不活動的時間,若超過這時間,session 將會失效

七、application對象
    application對象最常被使用在存取環境的資訊。
    因為環境的資訊通常都儲存在ServletContext中,所以常利用application對象來存取ServletContext中的資訊。
    application 對象實現javax.servlet.ServletContext 介面,ServletContext介面容器所提供的方法
        int getMajorVersion( )                             取得Container主要的Servlet API版本
        int getMinorVersion( )                             取得Container次要的Servlet API 版本
        String getServerInfo( )                         取得Container的名稱和版本
        String getMimeType(String file)                 取得指定檔案的MIME 類型
        ServletContext getContext(String uripath)        取得指定Local URL的Application context
        String getRealPath(String path)                 取得本地端path的絕對路徑
        void log(String message)                         將資訊寫入log檔案中
        void log(String message, Throwable throwable)    將stack trace 所產生的異常資訊寫入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對象取得其他隱含對象的方法
        Exception getException( )                        回傳目前網頁的異常,不過此網頁要為error page,
        JspWriter getOut( )                             回傳目前網頁的輸出資料流,例如:out
        Object getPage( )                                回傳目前網頁的Servlet 實體(instance),例如:page
        ServletRequest getRequest( )                    回傳目前網頁的請求,例如:request
        ServletResponse getResponse( )                    回傳目前網頁的響應,例如:response
        ServletConfig getServletConfig( )                回傳目前此網頁的ServletConfig 對象,例如:config
        ServletContext getServletContext( )                回傳目前此網頁的執行環境(context),例如:application
        HttpSession getSession( )                        回傳和目前網頁有聯絡的會話(session),例如:session
    3.PageContext對象提供取得屬性的方法
        Object getAttribute(String name, int scope)        回傳name 屬性,範圍為scope的屬性對象,回傳類型為Object
        Enumeration getAttributeNamesInScope(int scope)    回傳所有屬性範圍為scope 的屬性名稱,回傳類型為Enumeration
        int getAttributesScope(String name)             回傳屬性名稱為name 的屬性範圍
        void removeAttribute(String name)                 移除屬性名稱為name 的屬性對象
        void removeAttribute(String name, int scope)    移除屬性名稱為name,範圍為scope 的屬性對象
        void setAttribute(String name, Object value, int scope)        指定屬性對象的名稱為name、值為value、範圍為scope
        Object findAttribute(String name)                尋找在所有範圍中屬性名稱為name 的屬性對象

九、exception對象
    若要使用exception 對象時,必須在page 指令中設定。<%@ page isErrorPage="true" %>才能使用。
    exception提供的三個方法:
        getMessage( )
        getLocalizedMessage( )、
        printStackTrace(new java.io.PrintWriter(out)) 

本篇文章來源於 :劉志猛部落格 原文連結:http://www.liuzm.com/article/java/09050030.htm

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.