javax.servlet.RequestDispatcher.include()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(9.8k)|赞(0)|评价(0)|浏览(97)

本文整理了Java中javax.servlet.RequestDispatcher.include方法的一些代码示例,展示了RequestDispatcher.include的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。RequestDispatcher.include方法的具体详情如下:
包路径:javax.servlet.RequestDispatcher
类名称:RequestDispatcher
方法名:include

RequestDispatcher.include介绍

[英]Includes the content of a resource (servlet, JSP page, HTML file) in the response. In essence, this method enables programmatic server-side includes.

The ServletResponse object has its path elements and parameters remain unchanged from the caller's. The included servlet cannot change the response status code or set headers; any attempt to make a change is ignored.

The request and response parameters must be either the same objects as were passed to the calling servlet's service method or be subclasses of the ServletRequestWrapper or ServletResponseWrapperclasses that wrap them.
[中]在响应中包含资源的内容(servlet、JSP页面、HTML文件)。本质上,这种方法支持编程的服务器端包含。
ServletResponse对象的路径元素和参数与调用方的保持不变。包含的servlet无法更改响应状态代码或设置标题;任何改变的尝试都会被忽略。
请求和响应参数必须是传递给调用servlet的服务方法的相同对象,或者是封装它们的ServletRequestWrapper或ServletResponseWrapperClass的子类。

代码示例

代码示例来源:origin: spring-projects/spring-framework

@Override
public void include(String path) throws ServletException, IOException {
  this.request.getRequestDispatcher(path).include(this.request, this.response);
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public void include(String path, boolean flush) throws ServletException, IOException {
  this.request.getRequestDispatcher(path).include(this.request, this.response);
  if (flush) {
    this.response.flushBuffer();
  }
}

代码示例来源:origin: oblac/jodd

/**
 * Include page which path relative to the root of the ServletContext.
 */
public static boolean includeAbsolute(final ServletContext context, final ServletRequest request, final HttpServletResponse response, final String page) throws IOException, ServletException {
  RequestDispatcher dispatcher = context.getRequestDispatcher(page);
  if (dispatcher != null) {
    dispatcher.include(request, response);
    return true;
  }
  return false;
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public void include(String path) throws ServletException, IOException {
  this.request.getRequestDispatcher(path).include(this.request, this.response);
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public void include(String path, boolean flush) throws ServletException, IOException {
  this.request.getRequestDispatcher(path).include(this.request, this.response);
  if (flush) {
    this.response.flushBuffer();
  }
}

代码示例来源:origin: stanfordnlp/CoreNLP

public void doGet(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
 if (request.getCharacterEncoding() == null) {
  request.setCharacterEncoding("utf-8");
 }
 response.setContentType("text/html; charset=UTF-8");
 this.getServletContext().getRequestDispatcher("/header.jsp").
  include(request, response);
 addResults(request, response);
 this.getServletContext().getRequestDispatcher("/footer.jsp").
  include(request, response);
}

代码示例来源:origin: oblac/jodd

/**
 * Include page which path is relative to the current HTTP request.
 */
public static boolean include(final ServletRequest request, final ServletResponse response, final String page) throws IOException, ServletException {
  RequestDispatcher dispatcher = request.getRequestDispatcher(page);
  if (dispatcher != null) {
    dispatcher.include(request, response);
    return true;
  }
  return false;
}

代码示例来源:origin: stanfordnlp/CoreNLP

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
 if (request.getCharacterEncoding() == null) {
  request.setCharacterEncoding("utf-8");
 }
 response.setContentType("text/html; charset=UTF-8");
 this.getServletContext().getRequestDispatcher("/header.jsp").
  include(request, response);
 addResults(request, response);
 this.getServletContext().getRequestDispatcher("/footer.jsp").
  include(request, response);
}

代码示例来源:origin: oblac/jodd

/**
 * Include named resource.
 */
public static boolean includeNamed(final ServletContext context, final ServletRequest request, final ServletResponse response, final String page) throws IOException, ServletException {
  RequestDispatcher dispatcher = context.getNamedDispatcher(page);
  if (dispatcher != null) {
    dispatcher.include(request, response);
    return true;
  }
  return false;
}

代码示例来源:origin: spring-projects/spring-framework

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
  ServletContext servletContext = getServletContext();
  Assert.state(servletContext != null, "No ServletContext");
  RequestDispatcher rd = servletContext.getNamedDispatcher(this.servletName);
  if (rd == null) {
    throw new ServletException("No servlet with name '" + this.servletName + "' defined in web.xml");
  }
  // If already included, include again, else forward.
  if (useInclude(request, response)) {
    rd.include(request, response);
    if (logger.isTraceEnabled()) {
      logger.trace("Included servlet [" + this.servletName +
          "] in ServletForwardingController '" + this.beanName + "'");
    }
  }
  else {
    rd.forward(request, response);
    if (logger.isTraceEnabled()) {
      logger.trace("Forwarded to servlet [" + this.servletName +
          "] in ServletForwardingController '" + this.beanName + "'");
    }
  }
  return null;
}

代码示例来源:origin: org.freemarker/freemarker

@Override
public void include(String url) throws ServletException, IOException {
  jspOut.flush();
  request.getRequestDispatcher(url).include(request, response);
}

代码示例来源:origin: stanfordnlp/CoreNLP

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException
{
 if (request.getCharacterEncoding() == null) {
  request.setCharacterEncoding("utf-8");
 }
 response.setContentType("text/html; charset=UTF-8");
 this.getServletContext().getRequestDispatcher("/header.jsp").
  include(request, response);
 request.setAttribute("classifiers", classifiers);
 this.getServletContext().getRequestDispatcher("/ner.jsp").
  include(request, response);
 addResults(request, response);
 this.getServletContext().getRequestDispatcher("/footer.jsp").
  include(request, response);
}

代码示例来源:origin: org.freemarker/freemarker

@Override
public void include(String url, boolean flush) throws ServletException, IOException {
  if (flush) {
    jspOut.flush();
  }
  final PrintWriter pw = new PrintWriter(jspOut);
  request.getRequestDispatcher(url).include(request, new HttpServletResponseWrapper(response) {
    @Override
    public PrintWriter getWriter() {
      return pw;
    }
    
    @Override
    public ServletOutputStream getOutputStream() {
      throw new UnsupportedOperationException("JSP-included resource must use getWriter()");
    }
  });
  pw.flush();
}

代码示例来源:origin: groovy/groovy-core

public void include(String path) throws ServletException, IOException {
  HttpServletRequest request = (HttpServletRequest) super.getVariable("request");
  HttpServletResponse response = (HttpServletResponse) super.getVariable("response");
  RequestDispatcher dispatcher = request.getRequestDispatcher(path);
  dispatcher.include(request, response);
}

代码示例来源:origin: javamelody/javamelody

/** {@inheritDoc} */
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
    throws IOException, ServletException {
  final String resource = request.getParameter("resource");
  if (resource != null && customResources.get(resource) != null) {
    final String customResource = customResources.get(resource);
    final HttpServletResponse httpResponse = (HttpServletResponse) response;
    MonitoringController.addHeadersForResource(httpResponse, customResource);
    if (customResources.get("useForward") == null) {
      request.getRequestDispatcher(customResource).include(request, response);
    } else {
      request.getRequestDispatcher(customResource).forward(request, response);
    }
  } else {
    chain.doFilter(request, response);
  }
}

代码示例来源:origin: oblac/jodd

/**
 * Renders the view by dispatching to the target JSP.
 */
@Override
protected void renderView(final ActionRequest actionRequest, final String target) throws Exception {
  HttpServletRequest request = actionRequest.getHttpServletRequest();
  HttpServletResponse response = actionRequest.getHttpServletResponse();
  RequestDispatcher dispatcher = request.getRequestDispatcher(target);
  if (dispatcher == null) {
    response.sendError(SC_NOT_FOUND, "Result not found: " + target);	// should never happened
    return;
  }
  // If we're included, then include the view, otherwise do forward.
  // This allow the page to, for example, set content type.
  if (DispatcherUtil.isPageIncluded(request, response)) {
    dispatcher.include(request, response);
  } else {
    dispatcher.forward(request, response);
  }
}

代码示例来源:origin: spring-projects/spring-framework

logger.debug("Including [" + getUrl() + "]");
rd.include(request, response);

代码示例来源:origin: jenkinsci/jenkins

d.include(req,rsp);

代码示例来源:origin: spring-projects/spring-session

@Override
  public void doFilter(HttpServletRequest wrappedRequest,
      HttpServletResponse wrappedResponse)
      throws IOException, ServletException {
    String id = wrappedRequest.getSession().getId();
    wrappedRequest.getRequestDispatcher("/").include(wrappedRequest,
        wrappedResponse);
    assertThat(
        SessionRepositoryFilterTests.this.sessionRepository.findById(id))
            .isNotNull();
  }
});

代码示例来源:origin: spring-projects/spring-framework

private void doTestServletForwardingController(ServletForwardingController sfc, boolean include)
    throws Exception {
  HttpServletRequest request = mock(HttpServletRequest.class);
  HttpServletResponse response = mock(HttpServletResponse.class);
  ServletContext context = mock(ServletContext.class);
  RequestDispatcher dispatcher = mock(RequestDispatcher.class);
  given(request.getMethod()).willReturn("GET");
  given(context.getNamedDispatcher("action")).willReturn(dispatcher);
  if (include) {
    given(request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE)).willReturn("somePath");
  }
  else {
    given(request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE)).willReturn(null);
  }
  StaticWebApplicationContext sac = new StaticWebApplicationContext();
  sac.setServletContext(context);
  sfc.setApplicationContext(sac);
  assertNull(sfc.handleRequest(request, response));
  if (include) {
    verify(dispatcher).include(request, response);
  }
  else {
    verify(dispatcher).forward(request, response);
  }
}

相关文章