天天看點

JSP的9個内置對象

JSP的内置對象引用名稱            相應的類型 

    request                            HttpServletRequest

    response                        HttpServletResponse

    session                            HttpSession(有開關的:page指令的session屬性的取值)

    application                        ServletContext

    config                            ServletConfig

    page                            this(目前Servlet對象)

    exception                        java.lang.Throwable(有開關的:page指令的isErrorPage屬性改為true)

    out                                JspWriter

    pageContext                        javax.servlet.jsp.PageContext很重要

            有三大作用:

                    1、本身是一個域對象。同一時候還能操作其它三個域對象(PageContext ServletRequest HttpSession  ServletContext)

                            本身表示的域範圍是本頁面。

                                    void setAttribute(String name,Object value)

                                    void removeAttribute(String name)

                                    Object getAttribute(String name)

                            操作其它的三個域對象

                                    void setAttribute(String name,Object value,int scope)

                                    void removeAttribute(String name,int scope)

                                    Object getAttribute(String name,int scope)

                                    參數int scope是由PageContext類提供的靜态變量規定的。

                                                PageContext.PAGE_SCOPE:頁面範圍(是PageContext本身中的那個Map,代号page)

                                                PageContext.REQUEST_SCOPE:請求範圍(是ServletRequest中的那個Map。代号request)

                                                PageContext.SESSION_SCOPE:請求範圍(是HttpSession中的那個Map,代号session)

                                                PageContext.APPLICATION_SCOPE:請求範圍(是ServletContext中的那個Map。代号application)

                            (很實用)Object findAttribute(String name):依次依照page request session application範圍搜尋指定名稱的對象,找到為止。

                    2、擷取其它8個隐式對象

                    3、提供了轉發和包括的友善方法

                        RequestDispatcher rd = request.getRequestDispatcher("/url");

                        rd.forward(request,response);

                        pageContext.forward("url");

                        pageContext.include("url");

四大域對象(兩個資源之間互傳資料)

    JSP中隐式對象的名稱                範圍名稱            詳細的類型

    pageContext                        page                javax.servlet.jsp.PageContext

    request                            request                javax.servlet.ServletRequest  (顯示完資料就沒實用了)

    session                            session                javax.servlet.http.HttpSession (顯示完資料了,過一會自己還要用)

    application                        application            javax.servlet.ServletContext (顯示完資料了。大家都要用。不建議使用。假設使用,必須同步處理)