Search in sources :

Example 31 with RequestDispatcher

use of javax.servlet.RequestDispatcher in project guice by google.

the class ManagedServletPipeline method getRequestDispatcher.

/**
   * @return Returns a request dispatcher wrapped with a servlet mapped to the given path or null if
   *     no mapping was found.
   */
RequestDispatcher getRequestDispatcher(String path) {
    final String newRequestUri = path;
    for (final ServletDefinition servletDefinition : servletDefinitions) {
        if (servletDefinition.shouldServe(path)) {
            return new RequestDispatcher() {

                @Override
                public void forward(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
                    Preconditions.checkState(!servletResponse.isCommitted(), "Response has been committed--you can only call forward before" + " committing the response (hint: don't flush buffers)");
                    // clear buffer before forwarding
                    servletResponse.resetBuffer();
                    ServletRequest requestToProcess;
                    if (servletRequest instanceof HttpServletRequest) {
                        requestToProcess = wrapRequest((HttpServletRequest) servletRequest, newRequestUri);
                    } else {
                        // This should never happen, but instead of throwing an exception
                        // we will allow a happy case pass thru for maximum tolerance to
                        // legacy (and internal) code.
                        requestToProcess = servletRequest;
                    }
                    // now dispatch to the servlet
                    doServiceImpl(servletDefinition, requestToProcess, servletResponse);
                }

                @Override
                public void include(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
                    // route to the target servlet
                    doServiceImpl(servletDefinition, servletRequest, servletResponse);
                }

                private void doServiceImpl(ServletDefinition servletDefinition, ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
                    servletRequest.setAttribute(REQUEST_DISPATCHER_REQUEST, Boolean.TRUE);
                    try {
                        servletDefinition.doService(servletRequest, servletResponse);
                    } finally {
                        servletRequest.removeAttribute(REQUEST_DISPATCHER_REQUEST);
                    }
                }
            };
        }
    }
    //otherwise, can't process
    return null;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletResponse(javax.servlet.ServletResponse) RequestDispatcher(javax.servlet.RequestDispatcher)

Example 32 with RequestDispatcher

use of javax.servlet.RequestDispatcher in project grails-core by grails.

the class UrlMappingUtils method includeForUrl.

/**
     * Includes the given URL returning the resulting content as a String
     *
     * @param includeUrl The URL to include
     * @param request The request
     * @param response The response
     * @param model The model
     * @return The content
     */
@SuppressWarnings({ "unchecked", "rawtypes" })
public static IncludedContent includeForUrl(String includeUrl, HttpServletRequest request, HttpServletResponse response, Map model) {
    RequestDispatcher dispatcher = request.getRequestDispatcher(includeUrl);
    HttpServletResponse wrapped = WrappedResponseHolder.getWrappedResponse();
    response = wrapped != null ? wrapped : response;
    WebUtils.exposeIncludeRequestAttributes(request);
    Map toRestore = WebUtils.exposeRequestAttributesAndReturnOldValues(request, model);
    final GrailsWebRequest webRequest = GrailsWebRequest.lookup(request);
    final Object previousControllerClass = webRequest.getAttribute(GrailsApplicationAttributes.GRAILS_CONTROLLER_CLASS_AVAILABLE, WebRequest.SCOPE_REQUEST);
    final Object previousMatchedRequest = webRequest.getAttribute(UrlMappingsHandlerMapping.MATCHED_REQUEST, WebRequest.SCOPE_REQUEST);
    try {
        webRequest.removeAttribute(GrailsApplicationAttributes.GRAILS_CONTROLLER_CLASS_AVAILABLE, WebRequest.SCOPE_REQUEST);
        webRequest.removeAttribute(UrlMappingsHandlerMapping.MATCHED_REQUEST, WebRequest.SCOPE_REQUEST);
        webRequest.removeAttribute("grailsWebRequestFilter" + OncePerRequestFilter.ALREADY_FILTERED_SUFFIX, WebRequest.SCOPE_REQUEST);
        final IncludeResponseWrapper responseWrapper = new IncludeResponseWrapper(response);
        try {
            WrappedResponseHolder.setWrappedResponse(responseWrapper);
            dispatcher.include(request, responseWrapper);
            if (responseWrapper.getRedirectURL() != null) {
                return new IncludedContent(responseWrapper.getRedirectURL());
            }
            return new IncludedContent(responseWrapper.getContentType(), responseWrapper.getContent());
        } finally {
            webRequest.setAttribute(GrailsApplicationAttributes.GRAILS_CONTROLLER_CLASS_AVAILABLE, previousControllerClass, WebRequest.SCOPE_REQUEST);
            webRequest.setAttribute(UrlMappingsHandlerMapping.MATCHED_REQUEST, previousMatchedRequest, WebRequest.SCOPE_REQUEST);
            WrappedResponseHolder.setWrappedResponse(wrapped);
        }
    } catch (Exception e) {
        throw new ControllerExecutionException("Unable to execute include: " + e.getMessage(), e);
    } finally {
        WebUtils.cleanupIncludeRequestAttributes(request, toRestore);
    }
}
Also used : IncludeResponseWrapper(org.grails.web.util.IncludeResponseWrapper) HttpServletResponse(javax.servlet.http.HttpServletResponse) ControllerExecutionException(org.grails.web.servlet.mvc.exceptions.ControllerExecutionException) GrailsWebRequest(org.grails.web.servlet.mvc.GrailsWebRequest) IncludedContent(org.grails.web.util.IncludedContent) HashMap(java.util.HashMap) Map(java.util.Map) RequestDispatcher(javax.servlet.RequestDispatcher) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) ControllerExecutionException(org.grails.web.servlet.mvc.exceptions.ControllerExecutionException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 33 with RequestDispatcher

use of javax.servlet.RequestDispatcher in project groovy-core by groovy.

the class ServletBinding method include.

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);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) RequestDispatcher(javax.servlet.RequestDispatcher)

Example 34 with RequestDispatcher

use of javax.servlet.RequestDispatcher in project groovy-core by groovy.

the class ServletBinding method forward.

public void forward(String path) throws ServletException, IOException {
    HttpServletRequest request = (HttpServletRequest) super.getVariable("request");
    HttpServletResponse response = (HttpServletResponse) super.getVariable("response");
    RequestDispatcher dispatcher = request.getRequestDispatcher(path);
    dispatcher.forward(request, response);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) RequestDispatcher(javax.servlet.RequestDispatcher)

Example 35 with RequestDispatcher

use of javax.servlet.RequestDispatcher in project LIMaS by Kasabrella.

the class SearchMemberServlet method gotoPage.

/**
	 * ページ遷移
	 * @param request
	 * @param response
	 * @param page 遷移先
	 */
private void gotoPage(HttpServletRequest request, HttpServletResponse response, String page) throws ServletException, IOException {
    RequestDispatcher rd = request.getRequestDispatcher(page);
    rd.forward(request, response);
}
Also used : RequestDispatcher(javax.servlet.RequestDispatcher)

Aggregations

RequestDispatcher (javax.servlet.RequestDispatcher)203 ServletException (javax.servlet.ServletException)63 HttpSession (javax.servlet.http.HttpSession)58 IOException (java.io.IOException)41 SQLException (java.sql.SQLException)31 HttpServletRequest (javax.servlet.http.HttpServletRequest)30 HttpServletResponse (javax.servlet.http.HttpServletResponse)26 Properties (java.util.Properties)14 ServletContext (javax.servlet.ServletContext)12 Test (org.junit.Test)10 ArrayList (java.util.ArrayList)9 WebUser (org.compiere.util.WebUser)9 ClienteDAO (br.senac.tads3.pi03b.gruposete.dao.ClienteDAO)8 PrintWriter (java.io.PrintWriter)8 ServletResponse (javax.servlet.ServletResponse)8 Cliente (br.senac.tads3.pi03b.gruposete.models.Cliente)7 ServletRequest (javax.servlet.ServletRequest)7 User (org.opennms.netmgt.config.users.User)7 HotelDAO (br.senac.tads3.pi03b.gruposete.dao.HotelDAO)6 VooDAO (br.senac.tads3.pi03b.gruposete.dao.VooDAO)6