javax.servlet.ServletContext.getRequestDispatcher()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(11.4k)|赞(0)|评价(0)|浏览(194)

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

ServletContext.getRequestDispatcher介绍

[英]Returns a RequestDispatcher object that acts as a wrapper for the resource located at the given path. A RequestDispatcher object can be used to forward a request to the resource or to include the resource in a response. The resource can be dynamic or static.

The pathname must begin with a "/" and is interpreted as relative to the current context root. Use getContext to obtain a RequestDispatcher for resources in foreign contexts. This method returns null if the ServletContext cannot return a RequestDispatcher.
[中]返回一个RequestDispatcher对象,该对象充当位于给定路径的资源的包装器。RequestDispatcher对象可用于将请求转发给资源,或将资源包含在响应中。资源可以是动态的,也可以是静态的。
路径名必须以“/”开头,并被解释为相对于当前上下文根。使用getContext为外国环境中的资源获取RequestDispatcher。如果ServletContext无法返回[$5$],则此方法返回null

代码示例

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

/**
 * Forward to page path relative to the root of the ServletContext.
 */
public static boolean forwardAbsolute(final ServletContext context, final ServletRequest request, final ServletResponse response, final String resource) throws IOException, ServletException {
  RequestDispatcher dispatcher = context.getRequestDispatcher(resource);
  if (dispatcher != null) {
    dispatcher.forward(request, response);
    return true;
  }
  return false;
}

代码示例来源:origin: org.codelabor/codelabor-system-file

/**
 * 페이지를 전환한다.
 * 
 * @param request
 *            요청
 * @param response
 *            응답
 * @param path
 *            경로
 * @throws Exception
 *             예외
 */
protected void dispatch(HttpServletRequest request, HttpServletResponse response, String path) throws Exception {
  logger.debug("dispatch path: ", path);
  RequestDispatcher dispatcher = servletConfig.getServletContext().getRequestDispatcher(path);
  dispatcher.forward(request, response);
}

代码示例来源:origin: org.dd4t/dd4t-mvc-support

/**
 * Dispatch a url to a request dispatcher while buffering the output in a string
 * (and not directly to the response's writer).
 */
public static String dispatchBufferedRequest(final HttpServletRequest request, final HttpServletResponse
    response, final String url) throws ServletException, IOException {
  long time = System.currentTimeMillis();
  final RequestDispatcher dispatcher = request.getServletContext().getRequestDispatcher(url);
  final HttpServletResponse responseWrapper = new HttpServletResponseWrapper(response) {
    private CharArrayWriter output = new CharArrayWriter();
    @Override
    public String toString() {
      return output.toString();
    }
    @Override
    public PrintWriter getWriter() {
      return new PrintWriter(output);
    }
  };
  dispatcher.include(request, responseWrapper);
  time = System.currentTimeMillis() - time;
  LOG.debug("dispatchBufferedRequest {}, takes: {} ms.", url, time);
  return responseWrapper.toString();
}

代码示例来源:origin: com.github.dblock.waffle/waffle-tomcat5

private void redirectTo(final Request request, final Response response, final String url) {
    try {
      this.log.debug("redirecting to: {}", url);
      final ServletContext servletContext = this.context.getServletContext();
      final RequestDispatcher disp = servletContext.getRequestDispatcher(url);
      disp.forward(request.getRequest(), response);
    } catch (IOException e) {
      this.log.error(e.getMessage());
      this.log.trace("{}", e);
      throw new RuntimeException(e);
    } catch (ServletException e) {
      this.log.error(e.getMessage());
      this.log.trace("{}", e);
      throw new RuntimeException(e);
    }
  }
}

代码示例来源:origin: org.onehippo.cms7.hst/hst-client

protected void doDispatch(String dispatchPath, HstRequest request, HstResponse response) throws HstComponentException {
  if (dispatchPath != null) {
    try {
      servletContext.getRequestDispatcher(dispatchPath).include(request, response);
    } catch (ServletException e) {
      throw new HstComponentException(e);
    } catch (IOException e) {
      throw new HstComponentException(e);
    }
  } else {
    if (log.isDebugEnabled()) {
      log.debug("The dispatch path is null. The lifecycle phase is {} and the dispatch lifecycle phase is {}", 
          request.getLifecyclePhase(), 
          request.getAttribute(LIFECYCLE_PHASE_ATTRIBUTE));
    }
  }
}

代码示例来源:origin: stackoverflow.com

public class AccountServlet extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

  List<Account> accounts = getAccountListFromSomewhere();

  String url="..."; //relative url for display jsp page
  ServletContext sc = getServletContext();
  RequestDispatcher rd = sc.getRequestDispatcher(url);

  request.setAttribute("accountList", accounts );
  rd.forward(request, response);
 }
}

代码示例来源:origin: Waffle/waffle

/**
 * Redirect to.
 *
 * @param request
 *            the request
 * @param response
 *            the response
 * @param url
 *            the url
 */
private void redirectTo(final Request request, final HttpServletResponse response, final String url) {
  try {
    this.log.debug("redirecting to: {}", url);
    final ServletContext servletContext = this.context.getServletContext();
    final RequestDispatcher disp = servletContext.getRequestDispatcher(url);
    disp.forward(request.getRequest(), response);
  } catch (final IOException | ServletException e) {
    this.log.error(e.getMessage());
    this.log.trace("", e);
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: org.onehippo.ecm.hst/hst-client

protected void doDispatch(String dispatchPath, HstRequest request, HstResponse response) throws HstComponentException {
  if (dispatchPath != null) {
    try {
      getServletContext().getRequestDispatcher(dispatchPath).include(request, response);
    } catch (ServletException e) {
      throw new HstComponentException(e);
    } catch (IOException e) {
      throw new HstComponentException(e);
    }
  } else {
    if (log.isDebugEnabled()) {
      log.debug("The dispatch path is null. The lifecycle phase is {} and the dispatch lifecycle phase is {}", 
          request.getLifecyclePhase(), 
          request.getAttribute(LIFECYCLE_PHASE_ATTRIBUTE));
    }
  }
}

代码示例来源:origin: openzipkin/brave

@Override
 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException {
  req.getServletContext().getRequestDispatcher("/foo").forward(req, resp);
 }
}

代码示例来源:origin: Waffle/waffle

/**
 * Redirect to.
 *
 * @param request
 *            the request
 * @param response
 *            the response
 * @param url
 *            the url
 */
private void redirectTo(final Request request, final HttpServletResponse response, final String url) {
  try {
    this.log.debug("redirecting to: {}", url);
    final ServletContext servletContext = this.context.getServletContext();
    final RequestDispatcher disp = servletContext.getRequestDispatcher(url);
    disp.forward(request.getRequest(), response);
  } catch (final IOException | ServletException e) {
    this.log.error(e.getMessage());
    this.log.trace("", e);
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: org.onehippo.ecm.hst.components/hst-core

log.debug("Invoking dispatcher of url: {}", dispatchUrl);
  disp = requestContainerConfig.getServletContext().getRequestDispatcher(dispatchUrl);
} else {
  disp = requestContainerConfig.getServletContext().getNamedDispatcher(dispatchUrl);

代码示例来源:origin: apache/nifi

private void forwardToMessagePage(final HttpServletRequest httpServletRequest, final HttpServletResponse httpServletResponse, final String message) throws Exception {
  httpServletRequest.setAttribute("title", OIDC_ERROR_TITLE);
  httpServletRequest.setAttribute("messages", message);
  final ServletContext uiContext = httpServletRequest.getServletContext().getContext("/nifi");
  uiContext.getRequestDispatcher("/WEB-INF/pages/message-page.jsp").forward(httpServletRequest, httpServletResponse);
}

代码示例来源:origin: Waffle/waffle

/**
 * Redirect to.
 *
 * @param request
 *            the request
 * @param response
 *            the response
 * @param url
 *            the url
 */
private void redirectTo(final Request request, final HttpServletResponse response, final String url) {
  try {
    this.log.debug("redirecting to: {}", url);
    final ServletContext servletContext = this.context.getServletContext();
    final RequestDispatcher disp = servletContext.getRequestDispatcher(url);
    disp.forward(request.getRequest(), response);
  } catch (final IOException | ServletException e) {
    this.log.error(e.getMessage());
    this.log.trace("", e);
    throw new RuntimeException(e);
  }
}

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

private static void doCustomReport(HttpServletRequest httpRequest,
    HttpServletResponse httpResponse, String reportName)
    throws ServletException, IOException {
  final String customReportPath = Parameters.getParameterValueByName(reportName);
  if (!customReportPath.isEmpty() && customReportPath.charAt(0) == '/'
      && Parameters.getServletContext().getRequestDispatcher(customReportPath) != null) {
    Parameters.getServletContext().getRequestDispatcher(customReportPath)
        .forward(httpRequest, httpResponse);
  } else {
    httpResponse.sendRedirect(customReportPath);
  }
}

代码示例来源:origin: com.github.waffle/waffle-tomcat85

/**
 * Redirect to.
 *
 * @param request
 *            the request
 * @param response
 *            the response
 * @param url
 *            the url
 */
private void redirectTo(final Request request, final HttpServletResponse response, final String url) {
  try {
    this.log.debug("redirecting to: {}", url);
    final ServletContext servletContext = this.context.getServletContext();
    final RequestDispatcher disp = servletContext.getRequestDispatcher(url);
    disp.forward(request.getRequest(), response);
  } catch (final IOException | ServletException e) {
    this.log.error(e.getMessage());
    this.log.trace("", e);
    throw new RuntimeException(e);
  }
}

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

final RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(templateReference);
if (dispatcher == null) {
  throw new ContainerException(LocalizationMessages.NO_REQUEST_DISPATCHER_FOR_RESOLVED_PATH(templateReference));
  wrapper.forward(requestProviderRef.get().get(), new HttpServletResponseWrapper(responseProviderRef.get().get()) {

代码示例来源:origin: Waffle/waffle

/**
   * Redirect to.
   *
   * @param request
   *            the request
   * @param response
   *            the response
   * @param url
   *            the url
   */
  private void redirectTo(final Request request, final HttpServletResponse response, final String url) {
    try {
      this.log.debug("redirecting to: {}", url);
      final ServletContext servletContext = this.context.getServletContext();
      final RequestDispatcher disp = servletContext.getRequestDispatcher(url);
      disp.forward(request.getRequest(), response);
    } catch (final IOException | ServletException e) {
      this.log.error(e.getMessage());
      this.log.trace("", e);
      throw new RuntimeException(e);
    }
  }
}

代码示例来源:origin: resteasy/Resteasy

@Override
public void forward(String path)
{
 try
 {
   wasForwarded = true;
   servletContext.getRequestDispatcher(path).forward(request, servletResponse);
 }
 catch (ServletException e)
 {
   throw new RuntimeException(e);
 }
 catch (IOException e)
 {
   throw new RuntimeException(e);
 }
}

代码示例来源:origin: com.github.waffle/waffle-tomcat9

/**
 * Redirect to.
 *
 * @param request
 *            the request
 * @param response
 *            the response
 * @param url
 *            the url
 */
private void redirectTo(final Request request, final HttpServletResponse response, final String url) {
  try {
    this.log.debug("redirecting to: {}", url);
    final ServletContext servletContext = this.context.getServletContext();
    final RequestDispatcher disp = servletContext.getRequestDispatcher(url);
    disp.forward(request.getRequest(), response);
  } catch (final IOException | ServletException e) {
    this.log.error(e.getMessage());
    this.log.trace("", e);
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: opensourceBIM/BIMserver

protected void redirect(HttpServletRequest request, HttpServletResponse response, String pathInfo) throws ServletException {
  String theServletPath = dispatcherServletPath == null ? "/" : dispatcherServletPath;
  ServletContext sc = super.getServletContext();
  RequestDispatcher rd = dispatcherServletName != null ? sc.getNamedDispatcher(dispatcherServletName) : sc.getRequestDispatcher(theServletPath + pathInfo);
  if (rd == null) {
    throw new ServletException("No RequestDispatcher can be created for path " + pathInfo);
  }
  try {
    HttpServletRequestFilter servletRequest = new HttpServletRequestFilter(request, pathInfo, theServletPath);
    rd.forward(servletRequest, response);
  } catch (Throwable ex) {
    throw new ServletException("RequestDispatcher for path " + pathInfo + " has failed");
  }
}

相关文章

微信公众号

最新文章

更多

ServletContext类方法