Java Code Examples for javax.servlet.RequestDispatcher#include()
The following examples show how to use
javax.servlet.RequestDispatcher#include() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JspRuntimeLibrary.java From Tomcat7.0.67 with Apache License 2.0 | 6 votes |
/** * 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)); }
Example 2
Source File: ResourceServlet.java From lams with GNU General Public License v2.0 | 6 votes |
/** * Include the specified resource via the RequestDispatcher. * @param request current HTTP request * @param response current HTTP response * @param resourceUrl the URL of the target resource * @throws ServletException if thrown by the RequestDispatcher * @throws IOException if thrown by the RequestDispatcher */ private void doInclude(HttpServletRequest request, HttpServletResponse response, String resourceUrl) throws ServletException, IOException { if (this.contentType != null) { response.setContentType(this.contentType); } String[] resourceUrls = StringUtils.tokenizeToStringArray(resourceUrl, RESOURCE_URL_DELIMITERS); for (String url : resourceUrls) { String path = StringUtils.cleanPath(url); // Check whether URL matches allowed resources if (this.allowedResources != null && !this.pathMatcher.match(this.allowedResources, path)) { throw new ServletException("Resource [" + path + "] does not match allowed pattern [" + this.allowedResources + "]"); } if (logger.isDebugEnabled()) { logger.debug("Including resource [" + path + "]"); } RequestDispatcher rd = request.getRequestDispatcher(path); rd.include(request, response); } }
Example 3
Source File: DefaultServletRequestDispatcherTest.java From piranha with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * Test include method. * * @throws Exception when a serious error occurs. */ @Test public void testInclude() throws Exception { TestWebApplicationRequest request = new TestWebApplicationRequest(); TestWebApplicationResponse response = new TestWebApplicationResponse(); DefaultWebApplication webApp = new DefaultWebApplication(); webApp.addServlet("Echo", TestEcho1Servlet.class); webApp.initialize(); webApp.start(); webApp.linkRequestAndResponse(request, response); RequestDispatcher dispatcher = webApp.getNamedDispatcher("Echo"); dispatcher.include(request, response); response.flushBuffer(); String responseText = new String(response.getResponseBytes()); webApp.unlinkRequestAndResponse(request, response); webApp.stop(); assertTrue(responseText.contains("ECHO")); }
Example 4
Source File: Servlet3MultiRequest.java From boubei-tss with Apache License 2.0 | 6 votes |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); XmlPrintWriter writer = new XmlPrintWriter(response.getWriter()); writer.println("<Responses>"); List<Element> requestNodes = XMLDocUtil.selectNodes(parseRequestXML(request), "/Requests/Request"); for (Element requestNode : requestNodes) { String servletPath = requestNode.attributeValue("url"); //获取子请求的ServletPath XHttpServletRequest rRequest = XHttpServletRequestWrapper.wrapRequest(request); rRequest.setHeader(RequestContext.MULTI_REQUEST, "true"); rRequest.setServletPath(servletPath); XmlHttpDecoder.decode(requestNode, rRequest); RequestDispatcher rd = request.getRequestDispatcher(servletPath); // 将多个单功能的文件例如.jsp文件、servlet请求 整合成一个总的Servlet文件,相当于:jsp中 include file="xyz.jsp" rd.include(rRequest, response); } writer.println("</Responses>"); }
Example 5
Source File: RendererHelper.java From uyuni with GNU General Public License v2.0 | 6 votes |
/** * Renders a URL and returns the content generated as a string * @param url content to generate * @param req incoming request * @param resp current respnose * @return rendered content * @throws ServletException something goes wrong * @throws IOException something goes wrong */ public static String renderRequest(String url, HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String retval = null; RequestDispatcher dispatcher = req.getRequestDispatcher(url); if (dispatcher != null) { CachingResponseWrapper wrapper = new CachingResponseWrapper(resp); dispatcher.include(req, wrapper); retval = wrapper.getCachedContent(); if (retval != null) { retval = retval.trim(); } } return retval; }
Example 6
Source File: ListTagUtil.java From uyuni with GNU General Public License v2.0 | 6 votes |
/** * Includes arbitrary _local_ url as content * @param ctx caller's page context * @param url local url * @throws JspException if something goes wrong * * Note: Local means Urls in the same application */ public static void includeContent(PageContext ctx, String url) throws JspException { HttpServletRequest request = (HttpServletRequest) ctx.getRequest(); HttpServletResponse response = (HttpServletResponse) ctx.getResponse(); RequestDispatcher rd = request.getSession(true).getServletContext().getRequestDispatcher(url); if (rd == null) { ListTagUtil.write(ctx, "<!-- " + url + " not found -->"); } else { try { BufferedResponseWrapper wrapper = new BufferedResponseWrapper(response); rd.include(request, wrapper); wrapper.flush(); ListTagUtil.write(ctx, wrapper.getBufferedOutput()); } catch (Exception e) { throw new JspException(e); } } }
Example 7
Source File: ServletForwardingController.java From java-technology-stack with MIT License | 5 votes |
@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; }
Example 8
Source File: AtlasHttpServlet.java From atlas with Apache License 2.0 | 5 votes |
protected void includeResponse(HttpServletRequest request, HttpServletResponse response, String template) { try { response.setContentType(TEXT_HTML); response.setHeader(XFRAME_OPTION, DENY); ServletContext context = getServletContext(); RequestDispatcher rd = context.getRequestDispatcher(template); rd.include(request, response); } catch (Exception e) { LOG.error("Error in AtlasHttpServlet", template, e); } }
Example 9
Source File: TestInclude4Servlet.java From piranha with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Process GET request. * * @param request the request. * @param response the response. * @throws IOException when an I/O error occurs. * @throws ServletException when a Servlet error occurs. */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { RequestDispatcher rd = request.getServletContext().getRequestDispatcher("/include"); rd.include(request, response); rd = request.getServletContext().getRequestDispatcher("/include3"); rd.include(request, response); }
Example 10
Source File: AbstractJspServletCallback.java From n2o-framework with Apache License 2.0 | 5 votes |
protected void include(HttpServletRequest req, HttpServletResponse res, String view, Map<String, Object> model) throws ServletException, IOException { RequestDispatcher dispatcher = servletContext.getRequestDispatcher(getSrcPath(view)); if (dispatcher != null) { setAttributes(req, model); dispatcher.include(req, res); } else throw new IllegalStateException("dispatcher is null"); }
Example 11
Source File: InternalResourceView.java From lams with GNU General Public License v2.0 | 5 votes |
/** * 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 resource [" + getUrl() + "] in InternalResourceView '" + getBeanName() + "'"); } rd.include(request, response); } else { // Note: The forwarded resource is supposed to determine the content type itself. if (logger.isDebugEnabled()) { logger.debug("Forwarding to resource [" + getUrl() + "] in InternalResourceView '" + getBeanName() + "'"); } rd.forward(request, response); } }
Example 12
Source File: InternalResourceView.java From java-technology-stack with MIT License | 5 votes |
/** * 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); } }
Example 13
Source File: IncludeServlet.java From quarkus-http with Apache License 2.0 | 5 votes |
@Override protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().write(MESSAGE); RequestDispatcher dispatcher; if (req.getHeader("name") != null) { dispatcher = req.getServletContext().getNamedDispatcher(req.getHeader("include")); } else { dispatcher = req.getRequestDispatcher(req.getHeader("include")); } dispatcher.include(req, resp); }
Example 14
Source File: InternalResourceView.java From spring-analysis-note with MIT License | 5 votes |
/** * 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); } }
Example 15
Source File: StandardHostValve.java From Tomcat7.0.67 with Apache License 2.0 | 4 votes |
/** * 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()); } 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); } }
Example 16
Source File: TestApplicationMapping.java From Tomcat8-Source-Read with MIT License | 4 votes |
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { RequestDispatcher rd = req.getServletContext().getNamedDispatcher("Mapping"); rd.include(req, resp); }
Example 17
Source File: TestApplicationMapping.java From Tomcat8-Source-Read with MIT License | 4 votes |
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { RequestDispatcher rd = req.getRequestDispatcher("/mapping"); rd.include(req, resp); }
Example 18
Source File: StandardHostValve.java From Tomcat8-Source-Read with MIT License | 4 votes |
/** * 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()); } 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; } }
Example 19
Source File: TestInclude3Servlet.java From piranha with BSD 3-Clause "New" or "Revised" License | 3 votes |
/** * Process GET request. * * @param request the request. * @param response the response. * @throws IOException when an I/O error occurs. * @throws ServletException when a Servlet error occurs. */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { RequestDispatcher rd = request.getServletContext().getRequestDispatcher("/include"); rd.include(request, response); }
Example 20
Source File: TestIncludeServlet.java From piranha with BSD 3-Clause "New" or "Revised" License | 3 votes |
/** * Process GET request. * * @param request the request. * @param response the response. * @throws IOException when an I/O error occurs. * @throws ServletException when a Servlet error occurs. */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { RequestDispatcher rd = request.getServletContext().getRequestDispatcher("/include2"); rd.include(request, response); }