cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Akber Choudhry <ak...@dyanet.com>
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
4.0.x

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
component.

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;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.CharArrayWriter;


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
ByteResponseWrapper(httpResponse);
        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) {
        super(response);
        output = new CharArrayWriter();
    }

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

}
}
==================end============


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


-- 
Akber Choudhry
Dyanet Inc.
http://www.dyanet.com/


---------------------------------------------------------------------
Please check that your question has not already been answered in the
FAQ before posting. <http://xml.apache.org/cocoon/faqs.html>

To unsubscribe, e-mail: <cocoon-users-unsubscribe@xml.apache.org>
For additional commands, e-mail: <cocoon-users-help@xml.apache.org>


Mime
View raw message