cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Akber Choudhry <>
Subject Re: how to call a servlet from a generator
Date Wed, 13 Mar 2002 19:21:15 GMT

This might be of help to you - another
implementation of JspEngineImpl - for use with Servlet API 2.3 - Tomcat

This uses the Request Dispatcher to execute the URI - whether it is a
sevlet or a JSP (through servlet) regardless of vendor.

Both the JspReader and JSPGenerator work with this implemenataion.

So - type="jsp" will work. And, src="/wholeURIasSeenByWebApp". The whole
request object will be passed on to
your servlet. It uses the same role and interface as cocoon 2 jsp

Caveats: Headers added by servlet will be ignored. Character encoding
implications to be worked out.  Here is the code (experimental).  It
compiles to two classes:

========== begin ==========================

package org.apache.cocoon.components.jsp;

import org.apache.avalon.framework.logger.AbstractLoggable;
import org.apache.avalon.framework.thread.ThreadSafe;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

public class JSPEngineImpl extends AbstractLoggable
    implements JSPEngine, ThreadSafe {

    public byte[] executeJSP(String url, HttpServletRequest httpRequest,
HttpServletResponse httpResponse, ServletContext context)
        throws IOException, ServletException, Exception {

        byte[] bytes = null;

        // get ServletEngine Dispatcher.
        javax.servlet.RequestDispatcher rd = context.getRequestDispatcher(
url );
        ByteResponseWrapper responsewrapper = new
        if (rd != null) {
          rd.include( httpRequest, responsewrapper );
		  bytes= responsewrapper.getBytes();
          getLogger().info( "Successfully retrieved " + url + " from
ServletEngine Dispatcher" );
        } else {
          getLogger().error( "Error getting " + url + " from ServletEngine
Dispatcher" );

        return bytes;

    public class ByteResponseWrapper extends HttpServletResponseWrapper {
    private CharArrayWriter output; //TODO - ByteArrayOutputStream

    public byte[] getBytes() {
        return output.toString().getBytes(); //TODO - toByteArray - get
bytes from JSP response instead of string - CharSet implications

    public ByteResponseWrapper(HttpServletResponse response) {
        output = new CharArrayWriter();

    public PrintWriter getWriter() {
        return new PrintWriter(output);


On Wed, 13 Mar 2002, Walter Gamba wrote:
> Any help would be appreciated!
> Thanx

Akber Choudhry
Dyanet Inc.

Please check that your question has not already been answered in the
FAQ before posting. <>

To unsubscribe, e-mail: <>
For additional commands, e-mail: <>

View raw message