Search in sources :

Example 6 with RequestDispatcher

use of jakarta.servlet.RequestDispatcher in project tomcat by apache.

the class JspRuntimeLibrary method include.

/**
 * Perform a RequestDispatcher.include() operation, with optional flushing
 * of the response beforehand.
 *
 * @param request The servlet request we are processing
 * @param response The servlet response we are processing
 * @param relativePath The relative path of the resource to be included
 * @param out The Writer to whom we are currently writing
 * @param flush Should we flush before the include is processed?
 *
 * @exception IOException if thrown by the included servlet
 * @exception ServletException if thrown by the included servlet
 */
public static void include(ServletRequest request, ServletResponse response, String relativePath, JspWriter out, boolean flush) throws IOException, ServletException {
    if (flush && !(out instanceof BodyContent)) {
        out.flush();
    }
    // FIXME - It is tempting to use request.getRequestDispatcher() to
    // resolve a relative path directly, but Catalina currently does not
    // take into account whether the caller is inside a RequestDispatcher
    // include or not.  Whether Catalina *should* take that into account
    // is a spec issue currently under review.  In the mean time,
    // replicate Jasper's previous behavior
    String resourcePath = getContextRelativePath(request, relativePath);
    RequestDispatcher rd = request.getRequestDispatcher(resourcePath);
    rd.include(request, new ServletResponseWrapperInclude(response, out));
}
Also used : BodyContent(jakarta.servlet.jsp.tagext.BodyContent) RequestDispatcher(jakarta.servlet.RequestDispatcher)

Example 7 with RequestDispatcher

use of jakarta.servlet.RequestDispatcher in project spring-framework by spring-projects.

the class DefaultServletHttpRequestHandler method handleRequest.

@Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Assert.state(this.servletContext != null, "No ServletContext set");
    RequestDispatcher rd = this.servletContext.getNamedDispatcher(this.defaultServletName);
    if (rd == null) {
        throw new IllegalStateException("A RequestDispatcher could not be located for the default servlet '" + this.defaultServletName + "'");
    }
    rd.forward(request, response);
}
Also used : RequestDispatcher(jakarta.servlet.RequestDispatcher)

Example 8 with RequestDispatcher

use of jakarta.servlet.RequestDispatcher in project spring-framework by spring-projects.

the class InternalResourceView method renderMergedOutputModel.

/**
 * Render the internal resource given the specified model.
 * This includes setting the model as request attributes.
 */
@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
    // Expose the model object as request attributes.
    exposeModelAsRequestAttributes(model, request);
    // Expose helpers as request attributes, if any.
    exposeHelpers(request);
    // Determine the path for the request dispatcher.
    String dispatcherPath = prepareForRendering(request, response);
    // Obtain a RequestDispatcher for the target resource (typically a JSP).
    RequestDispatcher rd = getRequestDispatcher(request, dispatcherPath);
    if (rd == null) {
        throw new ServletException("Could not get RequestDispatcher for [" + getUrl() + "]: Check that the corresponding file exists within your web application archive!");
    }
    // If already included or response already committed, perform include, else forward.
    if (useInclude(request, response)) {
        response.setContentType(getContentType());
        if (logger.isDebugEnabled()) {
            logger.debug("Including [" + getUrl() + "]");
        }
        rd.include(request, response);
    } else {
        // Note: The forwarded resource is supposed to determine the content type itself.
        if (logger.isDebugEnabled()) {
            logger.debug("Forwarding to [" + getUrl() + "]");
        }
        rd.forward(request, response);
    }
}
Also used : ServletException(jakarta.servlet.ServletException) RequestDispatcher(jakarta.servlet.RequestDispatcher)

Example 9 with RequestDispatcher

use of jakarta.servlet.RequestDispatcher in project spring-framework by spring-projects.

the class ServletForwardingController method handleRequestInternal.

@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;
}
Also used : ServletException(jakarta.servlet.ServletException) ServletContext(jakarta.servlet.ServletContext) RequestDispatcher(jakarta.servlet.RequestDispatcher)

Example 10 with RequestDispatcher

use of jakarta.servlet.RequestDispatcher in project tomcat by apache.

the class StandardHostValve method custom.

/**
 * Handle an HTTP status code or Java exception by forwarding control
 * to the location included in the specified errorPage object.  It is
 * assumed that the caller has already recorded any request attributes
 * that are to be forwarded to this page.  Return <code>true</code> if
 * we successfully utilized the specified error page location, or
 * <code>false</code> if the default error report should be rendered.
 *
 * @param request The request being processed
 * @param response The response being generated
 * @param errorPage The errorPage directive we are obeying
 */
private boolean custom(Request request, Response response, ErrorPage errorPage) {
    if (container.getLogger().isDebugEnabled()) {
        container.getLogger().debug("Processing " + errorPage);
    }
    try {
        // Forward control to the specified location
        ServletContext servletContext = request.getContext().getServletContext();
        RequestDispatcher rd = servletContext.getRequestDispatcher(errorPage.getLocation());
        if (rd == null) {
            container.getLogger().error(sm.getString("standardHostValue.customStatusFailed", errorPage.getLocation()));
            return false;
        }
        if (response.isCommitted()) {
            // Response is committed - including the error page is the
            // best we can do
            rd.include(request.getRequest(), response.getResponse());
            // written to the client
            try {
                response.flushBuffer();
            } catch (Throwable t) {
                ExceptionUtils.handleThrowable(t);
            }
            // Now close immediately as an additional signal to the client
            // that something went wrong
            response.getCoyoteResponse().action(ActionCode.CLOSE_NOW, request.getAttribute(RequestDispatcher.ERROR_EXCEPTION));
        } else {
            // Reset the response (keeping the real error code and message)
            response.resetBuffer(true);
            response.setContentLength(-1);
            rd.forward(request.getRequest(), response.getResponse());
            // If we forward, the response is suspended again
            response.setSuspended(false);
        }
        // Indicate that we have successfully processed this custom page
        return true;
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
        // Report our failure to process this custom page
        container.getLogger().error("Exception Processing " + errorPage, t);
        return false;
    }
}
Also used : ServletContext(jakarta.servlet.ServletContext) RequestDispatcher(jakarta.servlet.RequestDispatcher)

Aggregations

RequestDispatcher (jakarta.servlet.RequestDispatcher)14 ServletContext (jakarta.servlet.ServletContext)4 ServletException (jakarta.servlet.ServletException)4 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)4 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)3 IOException (java.io.IOException)2 Test (org.junit.jupiter.api.Test)2 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)2 Filter (jakarta.servlet.Filter)1 ServletRequest (jakarta.servlet.ServletRequest)1 ServletResponse (jakarta.servlet.ServletResponse)1 BodyContent (jakarta.servlet.jsp.tagext.BodyContent)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AsyncDispatcher (org.apache.catalina.AsyncDispatcher)1 Session (org.apache.catalina.Session)1 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)1 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)1 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)1