jsp9個內置對象

一、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.mldn.cn/articleview/2007-5-8/article_view_2061.htm

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