You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by Martin van Dijken <mv...@madocke.nl> on 2003/05/23 14:32:09 UTC

Calling a generated JSP servlet directly

Hey there,

We've been very busy creating good tag libraries and other tools to make creating JSP's easy. This works so well in fact, that we'd like to use JSP's for generating other content as well. For instance, we'd like to format the content of an HTML-newsmail using a JSP. The formatted content would then be used to create the body of an email.

So, the question arises how to use Tomcat to generate this content for us. Is there a way to get Tomcat to do this for us, or would I have to build something like this? Can anybody give me a pointer where to start?

Thanks in advance,

Martin van Dijken

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


Re: Calling a generated JSP servlet directly

Posted by Tim Funk <fu...@joedog.org>.
- tomcat-user@jakarta.apache.org
- google
- wget (web page puller) + cron (schedule)

-Tim

Martin van Dijken wrote:
> Hey there,
> 
> We've been very busy creating good tag libraries and other tools to make creating JSP's easy. This works so well in fact, that we'd like to use JSP's for generating other content as well. For instance, we'd like to format the content of an HTML-newsmail using a JSP. The formatted content would then be used to create the body of an email.
> 
> So, the question arises how to use Tomcat to generate this content for us. Is there a way to get Tomcat to do this for us, or would I have to build something like this? Can anybody give me a pointer where to start?
> 
> Thanks in advance,
> 
> Martin van Dijken 


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


RE: Calling a generated JSP servlet directly

Posted by Chad Johnson <cj...@wspackaging.com>.
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