ServletContext是容器上下文,指當前的一個web應用的上下文
JSP網頁本身,page對象是當前頁面轉換后的Servlet類的實例。從轉換后的Servlet類的代碼中,可以看到這種關系:Object page = this;在JSP頁面中,很少使用page對象。
pageContext javax.servlet.jsp.PageContext 的實例,該對象代表該JSP 頁面上下文,使用該對象可以訪問頁面中的共享數據。常用的方法有getServletContext和getServletConfig等.
- //使用pageContext 設置屬性,該屬性默認在page 范圍內
- pageContext. setAttribute ("page" , "hello") ;
- //使用request 設置屬性,該屬性默認在request 范圍內
- request. setAttribute ("request" , "hello");
- //使用pageContext將屬性設置在request 范圍中
- pageContext.setAttribute("request2″ , "hello" ,pageContext.REQUEST_SCOPE);
- //使用session將屬性設置在session 范圍中
- session.setAttribute("session" , "hello");
- //使用pageContext將屬性設置在session范圍中
- pageContext.setAttribute("session2″ , "hello" ,pageContext.SESSION_SCOPE);
- //使用application將屬性設置在application范圍中
- application. setAttribute ("app" , "hello") ;
- //使用pageContext 將屬性設置在application 范圍中
- pageContext.setAttribute("app2″ , "hello" , pageContext.APPLICATION_SCOPE);
- //使用pageContext 設置屬性,該屬性默認在page 范圍內
- pageContext. setAttribute ("page" , "hello") ;
- //使用request 設置屬性,該屬性默認在request 范圍內
- request. setAttribute ("request" , "hello");
- //使用pageContext將屬性設置在request 范圍中
- pageContext.setAttribute("request2″ , "hello" ,pageContext.REQUEST_SCOPE);
- //使用session將屬性設置在session 范圍中
- session.setAttribute("session" , "hello");
- //使用pageContext將屬性設置在session范圍中
- pageContext.setAttribute("session2″ , "hello" ,pageContext.SESSION_SCOPE);
- //使用application將屬性設置在application范圍中
- application. setAttribute ("app" , "hello") ;
- //使用pageContext 將屬性設置在application 范圍中
- pageContext.setAttribute("app2″ , "hello" , pageContext.APPLICATION_SCOPE);
總的來說,pageContext和page都是jsp中的隱含對象,pageContext代表jsp頁面的上下文關系,能夠調用、存取其他隱含對象;
page代表處理當前請求的時候,這個頁面的實現類的實例。