tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Chad Johnson" <cjohn...@wspackaging.com>
Subject RE: Calling a generated JSP servlet directly
Date Fri, 23 May 2003 20:23:01 GMT
Hey,
  I do understand what your saying (I do this myself).  Here's the
approach I took.

1.) From a servlet do a Request Dispatch Forward
[RequestDispatcher.forward(ServletRequest request, ServletResponse
response)]

2.) Notice you feed the Forward a ServletResponse.  Implement your own
ServletResponse that stores all the output in a String.  I have a class
attached below.  Note this is a rather weak implementation of
ServletResponse.  But it gets the job done for me.  Usage would be
something like:

StringHttpServletResponse stringResponse = new
StringHttpServletResponse();
RequestDispatch.forward(request,stringResponse);
String output = stringResponse.toString();

-Chad Johnson

===================================
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class StringHttpServletResponse implements HttpServletResponse {
    private StringWriter dataString;

    public StringHttpServletResponse() {
        this.dataString = new StringWriter();
    }

    public PrintWriter getWriter() throws IOException {
        return new PrintWriter(dataString);
    }
    
    public ServletOutputStream getOutputStream() throws IOException {
        return new ServletOutputStream() {
            public void write(int i) {
                byte b[] = new byte[1];
                b[0] = (byte)i;
                dataString.write(new String(b));
            }
        };
    }

    public String toString() {
        return dataString.toString();
    }

    public String getCharacterEncoding() {
        return "ISO-8859-1";
    }

    public int getBufferSize() {
        return 512;
    }

    public boolean isCommitted() {
        return false;
    }

    public Locale getLocale() {
        return Locale.US;
    }

    public boolean containsHeader(String arg0) {
        return false;
    }

    public String encodeURL(String arg0) {
        return arg0;
    }

    public String encodeRedirectURL(String arg0) {
        return arg0;
    }

    public String encodeUrl(String arg0) {
        return arg0;
    }

    public String encodeRedirectUrl(String arg0) {
        return arg0;
    }

    public void addCookie(Cookie arg0) {}
    public void sendError(int arg0, String arg1) throws IOException {}
    public void sendError(int arg0) throws IOException {}
    public void sendRedirect(String arg0) throws IOException {}
    public void setDateHeader(String arg0, long arg1) {}
    public void addDateHeader(String arg0, long arg1) {}
    public void setHeader(String arg0, String arg1) {}
    public void addHeader(String arg0, String arg1) {}
    public void setIntHeader(String arg0, int arg1) {}
    public void addIntHeader(String arg0, int arg1) {}
    public void setStatus(int arg0) {}
    public void setStatus(int arg0, String arg1) {}
    public void setContentLength(int arg0) {}
    public void setContentType(String arg0) {}
    public void setBufferSize(int arg0) {}
    public void flushBuffer() throws IOException {}
    public void resetBuffer() {}
    public void reset() {}
    public void setLocale(Locale arg0) {}
}


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-dev-help@jakarta.apache.org


Mime
View raw message