【Java Web StudyNote】JSP implicit objects

There are 9 implicit objects in the JSP.

top 5

1.pageContext.

2.request.

3.response.

4.session.

5.application.

else

6.config.

7.out.

8.page.

9.exception.

 

 

First,let's find the source of the API of Java EE 6.

http://docs.oracle.com/javaee/6/api/

 

public abstract class PageContext

extends JspContext
abstract  void setAttribute(java.lang.String name, java.lang.Object value) 
          Register the name and value specified with page scope semantics.
abstract  void setAttribute(java.lang.String name, java.lang.Object value, int scope) 
          Register the name and value specified with appropriate scope semantics.

we can find these methods,setAttribute() in JspContext class.

 

 

设置属性

setAttribute();

获取属性

getAttribute();

删除属性

removeAttribute();

相如我们常用的就是

当前页:

pageContext.setAttribute(String a,Object o);

pageContext.getAttribute(String a);

当前会话

session.setAttribute(String a,Object o);

session.getAttribute(String a);

当前请求

request.setAttribute(String a,Object o);

request.getAttribute(String a);

当前应用

application.setAttribute(String a,Object o);

application.getAttribute(String a);

 

对于pageContext来说,无论是客户端跳转(就是直接写超链接)还是服务端跳转,都无法访问到属性。

而对于request来说,只要是服务器端跳转,是可以继续访问属性的。

对于session来说,无论是那种跳转,只要是同一个session来说,都是可以访问的。

对于application来说,不同的session也能访问到,只要是设置了的情况下,但是当web容器重启或者关闭的时候,内容将会被清空掉。

 

 

但是有时候,可能出于一些原因,需要用pageContext来设置一个session或者request的属性,这样可以么?

这就是最开始看的那个地方

static int REQUEST_SCOPE 
          Request scope: the named reference remains available from the ServletRequest associated with the Servlet until the current request is completed.

pageContext.setAttribute(String a,Object o,REQUEST_SCOPE);

这样就可以用request.getAttribute(String a)来访问属性了。其他同理。

static int PAGE_SCOPE 
          Page scope: (this is the default) the named reference remains available in this PageContext until the return from the current Servlet.service() invocation.
static int SESSION_SCOPE 
          Session scope (only valid if this page participates in a session): the named reference remains available from the HttpSession (if any) associated with the Servlet until the HttpSession is invalidated.

你可能感兴趣的:(Java Web)