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;
}
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);
}
}
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);
}
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);
}
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);
}
Aggregations