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 (显示完数据了。大家都要用。不建议使用。假设使用,必须同步处理)

posted on 2017-05-28 15:18  wgwyanfs  阅读(71)  评论(0编辑  收藏  举报

导航