关于“javax.servlet.include.request_uri”属性值 include 请求 RequestDispatcher.include

在springMVC的DispatcherServlet类的doService方法中有如下代码:     

1
2
3
4
5
6
7
8
9
10
if (WebUtils.isIncludeRequest(request)) {
             attributesSnapshot = new HashMap<String, Object>();
             Enumeration<?> attrNames = request.getAttributeNames();
             while (attrNames.hasMoreElements()) {
                 String attrName = (String) attrNames.nextElement();
                 if ( this .cleanupAfterInclude || attrName.startsWith( "org.springframework.web.servlet" )) {
                     attributesSnapshot.put(attrName, request.getAttribute(attrName));
                 }
             }
         }

其中WebUtils.isIncludeRequest(request)这段代码实现如下:

1
2
3
public static boolean isIncludeRequest(ServletRequest request) {
         return (request.getAttribute(INCLUDE_REQUEST_URI_ATTRIBUTE) != null );
     }
INCLUDE_REQUEST_URI_ATTRIBUTE常量值就是标题上的“javax.servlet.include.request_uri” ,isIncludeRequest(request)方法究竟是什么作用呢,要想明白这个问题,我们可以借助一条JSP的指令来理解:<jsp:incluede page="xxx.jsp"/> ,这条指令是指在一个页面中嵌套了另一个页面,那么我们知道JSP在运行期间是会被编译成相应的Servlet类来运行的,所以在Servlet中也会有类似的功能和调用语法,这就是RequestDispatch.include()方法。
1
那么在一个被别的servlet使用RequestDispatcher的include方法调用过的servlet中,如果它想知道那个调用它的servlet的上下文信息该怎么办呢,那就可以通过request中的attribute中的如下属性获取:
  • javax.servlet.include.request_uri
  • javax.servlet.include.context_path
  • javax.servlet.include.servlet_path
  • javax.servlet.include.path_info
  • javax.servlet.include.query_string
注RequestDispatch是通过ServletRequest.getRequestDispatcher("xxx.do")或ServletContext.getRequestDispatcher("/xxx.do")产生的,其中ServletRequest.getRequestDispatcher产生的是相对于根目录的,ServletRequest产生的是相对于当前请求路径的。RequestDispatcher接口的include或forward方法可以被调用。

在springMVC的DispatcherServlet类的doService方法中有如下代码:     

1
2
3
4
5
6
7
8
9
10
if (WebUtils.isIncludeRequest(request)) {
             attributesSnapshot = new HashMap<String, Object>();
             Enumeration<?> attrNames = request.getAttributeNames();
             while (attrNames.hasMoreElements()) {
                 String attrName = (String) attrNames.nextElement();
                 if ( this .cleanupAfterInclude || attrName.startsWith( "org.springframework.web.servlet" )) {
                     attributesSnapshot.put(attrName, request.getAttribute(attrName));
                 }
             }
         }

其中WebUtils.isIncludeRequest(request)这段代码实现如下:

1
2
3
public static boolean isIncludeRequest(ServletRequest request) {
         return (request.getAttribute(INCLUDE_REQUEST_URI_ATTRIBUTE) != null );
     }
INCLUDE_REQUEST_URI_ATTRIBUTE常量值就是标题上的“javax.servlet.include.request_uri” ,isIncludeRequest(request)方法究竟是什么作用呢,要想明白这个问题,我们可以借助一条JSP的指令来理解:<jsp:incluede page="xxx.jsp"/> ,这条指令是指在一个页面中嵌套了另一个页面,那么我们知道JSP在运行期间是会被编译成相应的Servlet类来运行的,所以在Servlet中也会有类似的功能和调用语法,这就是RequestDispatch.include()方法。
1
那么在一个被别的servlet使用RequestDispatcher的include方法调用过的servlet中,如果它想知道那个调用它的servlet的上下文信息该怎么办呢,那就可以通过request中的attribute中的如下属性获取:
  • javax.servlet.include.request_uri
  • javax.servlet.include.context_path
  • javax.servlet.include.servlet_path
  • javax.servlet.include.path_info
  • javax.servlet.include.query_string
注RequestDispatch是通过ServletRequest.getRequestDispatcher("xxx.do")或ServletContext.getRequestDispatcher("/xxx.do")产生的,其中ServletRequest.getRequestDispatcher产生的是相对于根目录的,ServletRequest产生的是相对于当前请求路径的。RequestDispatcher接口的include或forward方法可以被调用。

猜你喜欢

转载自www.cnblogs.com/hfultrastrong/p/11864396.html
今日推荐