Example usage for javax.servlet RequestDispatcher include

List of usage examples for javax.servlet RequestDispatcher include

Introduction

In this page you can find the example usage for javax.servlet RequestDispatcher include.

Prototype

public void include(ServletRequest request, ServletResponse response) throws ServletException, IOException;

Source Link

Document

Includes the content of a resource (servlet, JSP page, HTML file) in the response.

Usage

From source file:com.util.ConvertJspToStringHtml.java

public static String generateHtml(String url, HttpServletRequest request, HttpServletResponse response) {
    String html = null;/*from  www .  j  a v a  2s .c om*/
    try {
        BufferedHttpResponseWrapper wrapper = new BufferedHttpResponseWrapper(response);
        ServletContext context = request.getSession().getServletContext();
        String urlEncode = response.encodeRedirectURL(url);
        RequestDispatcher dispatcher = context.getRequestDispatcher(urlEncode);
        dispatcher.include(request, wrapper);
        //            html = ASCII2Unicode(StringEscapeUtils.unescapeHtml(wrapper.getOutput()));

        html = html.replaceAll("\\r\\n|\\r|\\n", "");
        html = html.replaceAll(" ", "");
    } catch (Exception e) {
        System.err.println(e.getMessage());
    }
    return html;
}

From source file:com.adobe.acs.commons.util.ResourceDataUtil.java

public static String getIncludeAsString(final String path, final SlingHttpServletRequest slingRequest,
        final SlingHttpServletResponse slingResponse) {
    StringWriterResponse responseWrapper = null;

    try {//from w w w .ja va 2 s.  com
        responseWrapper = new StringWriterResponse(slingResponse);
        final RequestDispatcher requestDispatcher = slingRequest.getRequestDispatcher(path);

        requestDispatcher.include(slingRequest, responseWrapper);

        return StringUtils.stripToNull(responseWrapper.getString());
    } catch (Exception ex) {
        log.error("Error creating the String representation for: " + path, ex);
    } finally {
        if (responseWrapper != null) {
            responseWrapper.clearWriter();
        }
    }

    return null;
}

From source file:com.liferay.portal.struts.StrutsUtil.java

public static void include(String uri, ServletContext ctx, HttpServletRequest req, HttpServletResponse res)
        throws ServletException {

    String path = Constants.TEXT_HTML_DIR + uri;
    if (BrowserSniffer.is_wml(req)) {
        path = Constants.TEXT_WML_DIR + uri;
    }/* www. ja  v a 2 s. c om*/

    ServletContext portalCtx = ctx.getContext(PropsUtil.get(PropsUtil.PORTAL_CTX));
    if (portalCtx == null) {
        portalCtx = ctx;
    }

    RequestDispatcher rd = portalCtx.getRequestDispatcher(path);

    try {
        rd.include(req, res);
    } catch (IOException ioe) {
    }
}

From source file:org.entando.entando.aps.system.services.controller.executor.AbstractWidgetExecutorService.java

protected static String extractJspOutput(RequestContext reqCtx, String jspPath)
        throws ServletException, IOException {
    HttpServletRequest request = reqCtx.getRequest();
    HttpServletResponse response = reqCtx.getResponse();
    BufferedHttpResponseWrapper wrapper = new BufferedHttpResponseWrapper(response);
    ServletContext context = request.getSession().getServletContext();
    String url = response.encodeRedirectURL(jspPath);
    RequestDispatcher dispatcher = context.getRequestDispatcher(url);
    dispatcher.include(request, wrapper);
    return wrapper.getOutput();
}

From source file:com.netspective.sparx.util.HttpUtils.java

public static void includeServletResourceContent(Writer writer, HttpServletValueContext vc, String includePath,
        String valueContextAttrName) throws IOException, ServletException {
    ServletRequest request = vc.getRequest();
    ServletResponse response = vc.getResponse();

    RequestDispatcher rd = request.getRequestDispatcher(includePath);

    if (writer != response.getWriter())
        response = new AlternateOutputDestServletResponse(writer, response);

    request.setAttribute(valueContextAttrName, vc);
    rd.include(request, response);
    request.removeAttribute(valueContextAttrName);
}

From source file:com.act.web.includeservletasstring.IncludeServletAsString.java

static public String invokeServletAndReturnAsString(String url, HttpServletRequest servletRequest,
        HttpServletResponse servletResponse) throws IOException, ServletException {

    if (log.isDebugEnabled()) {
        log.debug("Including url \"" + url + "\"...");
    }//  w w w  .  j a  v  a2s  . c o m

    RequestDispatcher requestDispatcher = servletRequest.getRequestDispatcher(url);

    if (requestDispatcher == null) {
        IllegalArgumentException iae = new IllegalArgumentException(
                "Failed to get RequestDispatcher for url: " + url);
        log.error(iae.getMessage(), iae);
        throw iae;
    }

    BufferedResponse bufferedResponse = new BufferedResponse(servletResponse);

    requestDispatcher.include(servletRequest, bufferedResponse);

    byte[] buffer = bufferedResponse.getBufferAsByteArray();
    if (log.isDebugEnabled()) {
        log.debug("Buffer returned with " + buffer.length + " bytes.");
    }

    String bufferString = new String(buffer, servletResponse.getCharacterEncoding());

    return bufferString;
}

From source file:org.apache.struts2.components.Include.java

public static void include(String aResult, Writer writer, ServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String resourcePath = getContextRelativePath(request, aResult);
    RequestDispatcher rd = request.getRequestDispatcher(resourcePath);

    if (rd == null) {
        throw new ServletException("Not a valid resource path:" + resourcePath);
    }/*from   ww w.j  a va2 s.  c o  m*/

    PageResponse pageResponse = new PageResponse(response);

    // Include the resource
    rd.include((HttpServletRequest) request, pageResponse);

    //write the response back to the JspWriter, using the correct encoding.
    String encoding = getEncoding();

    if (encoding != null) {
        //use the encoding specified in the property file
        pageResponse.getContent().writeTo(writer, encoding);
    } else {
        //use the platform specific encoding
        pageResponse.getContent().writeTo(writer, null);
    }
}

From source file:org.dd4t.mvc.utils.RenderUtils.java

/**
 * Dispatch a url to a request dispatcher while buffering the output in a string
 * (and not directly to the response's writer).
 *///  w w w  . j  a v  a2 s  .c o  m
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();
}

From source file:com.feilong.servlet.http.RequestUtil.java

/**
 *  {@link RequestDispatcher} ??????,????.
 * //  w  w w. j  a va 2  s .  c  o m
 * <p>
 * Includes the content of a resource (servlet, JSP page,HTML file) in the response. <br>
 * In essence, this method enables programmatic server-side includes.
 * </p>
 * 
 * <p>
 * :?Servlet????????,???.<br>
 * The {@link ServletResponse} object has its path elements and parameters remain unchanged from the caller's. <br>
 * The included servlet cannot change the response status code or set headers; any attempt to make a change is ignored.
 * </p>
 * 
 * <p>
 * The request and response parameters must be either the same objects as were passed to the calling servlet's service method or be
 * subclasses of the {@link ServletRequestWrapper} or {@link ServletResponseWrapper} classes that wrap them.
 * </p>
 * 
 * @param path
 *            the path
 * @param request
 *            a {@link ServletRequest} object,that contains the client's request
 * @param response
 *            a {@link ServletResponse} object,that contains the servlet's response
 * @see javax.servlet.RequestDispatcher#include(ServletRequest, ServletResponse)
 * @see "org.springframework.web.servlet.ResourceServlet"
 * @since 1.2.2
 */
public static void include(String path, HttpServletRequest request, HttpServletResponse response) {
    RequestDispatcher requestDispatcher = request.getRequestDispatcher(path);
    try {
        requestDispatcher.include(request, response);
    } catch (ServletException | IOException e) {
        LOGGER.error("", e);
        throw new RequestException(e);
    }
}

From source file:com.redhat.rhn.frontend.taglibs.list.ListTagUtil.java

/**
 * Includes arbitrary _local_ url as content
 * @param ctx caller's page context/*from w w w. j  av  a2 s  .  c o  m*/
 * @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);
        }
    }
}