You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by Sasha Haghani <sh...@brightspark.com> on 2001/04/26 21:38:45 UTC

Redirecting the Response of a JSP to a String

Hi there,

------
DISCLAIMER:  This a Tomcat question, where the solution will likely require
some tinkering with the implementation.  What I'm to do is probably a
non-standard use case.  I think knowledge of the Tomcat internals is
required to answer this.  I hope this question is appropriate for this list
-- if not I apologize in advance.  Also, sorry for the length...
------

I'd like to be able to invoke a JSP on-demand from a Java class and redirect
the response output stream to a String.  This could be used to dynamically
generate the body of an e-mail, for instance.  The key difference is that
there isn't a *real* corresponding HTTP request or browser socket connection
which would trigger JSP compilation and execution.

I've come up with 3 potential solutions.  Any input or comment on these from
this list is much appreciated.

------
SOLUTION 1: Connect to the Servlet Engine like a browser using a
URLConnection object and retrieve the response from there.

-> This solution is probably the most portable (across Servlet Engines) but
likely isn't the most performant.

------
SOLUTION 2: Using Tomcat Interceptors.

-> I don't know anything about these but would there be anyway to catch and
redirect the response using an Interceptor.  I assume such a solution would
only work with Tomcat.

------
SOLUTION 3: A custom Interceptor Servlet with a custom Response object.

-> A custom method could be called on such a GenericServlet and these
servlet would include the body of the JSP using the
RequestDispatcher.include method.  A custom Response object would output the
Writer stream to a String or StringBuffer.

I've attached some sample source code which outlines what I have in mind.

Could someone give me a reality check on whether this is even feasible or
advisable.  Particularly with regard to rolling my own Response
implementation which redirects Writer to a String.  Would this kind of
kludge cause any problems for Tomcat?

------
Any comments, help or suggestions that anyone can provide are much, much
appreciated.  Also, I'd love to hear alternative solutions.  Thanks so much.


Regards,

Sasha Haghani,
Toronto, Canada.

Re: Redirecting the Response of a JSP to a String

Posted by Glenn Nielsen <gl...@voyager.apg.more.net>.
Jon Stevens wrote:
> 
> on 4/26/01 2:11 PM, "Glenn Nielsen" <gl...@voyager.apg.more.net> wrote:
> 
> > A fifth solution for just sending email is to use the Mailer taglib at
> > http://jakarta.apache.org/taglibs/mailer/
> >
> > Regards,
> >
> > Glenn
> 
> The requested URL /taglibs/mailer/ was not found on this server.
> 
> :-)
> 
> -jon

Arghh...  Make that:

http://jakarta.apache.org/taglibs/doc/mailer-doc/intro.html

Thanks Jon!

Glenn

----------------------------------------------------------------------
Glenn Nielsen             glenn@more.net | /* Spelin donut madder    |
MOREnet System Programming               |  * if iz ina coment.      |
Missouri Research and Education Network  |  */                       |
----------------------------------------------------------------------

Re: Redirecting the Response of a JSP to a String

Posted by Jon Stevens <jo...@latchkey.com>.
on 4/26/01 2:11 PM, "Glenn Nielsen" <gl...@voyager.apg.more.net> wrote:

> A fifth solution for just sending email is to use the Mailer taglib at
> http://jakarta.apache.org/taglibs/mailer/
> 
> Regards,
> 
> Glenn

The requested URL /taglibs/mailer/ was not found on this server.

:-)

-jon


Re: Redirecting the Response of a JSP to a String

Posted by Glenn Nielsen <gl...@voyager.apg.more.net>.
A fifth solution for just sending email is to use the Mailer taglib at
http://jakarta.apache.org/taglibs/mailer/

Regards,

Glenn

Sasha Haghani wrote:
> 
> Hi there,
> 
> ------
> DISCLAIMER:  This a Tomcat question, where the solution will likely require
> some tinkering with the implementation.  What I'm to do is probably a
> non-standard use case.  I think knowledge of the Tomcat internals is
> required to answer this.  I hope this question is appropriate for this list
> -- if not I apologize in advance.  Also, sorry for the length...
> ------
> 
> I'd like to be able to invoke a JSP on-demand from a Java class and redirect
> the response output stream to a String.  This could be used to dynamically
> generate the body of an e-mail, for instance.  The key difference is that
> there isn't a *real* corresponding HTTP request or browser socket connection
> which would trigger JSP compilation and execution.
> 
> I've come up with 3 potential solutions.  Any input or comment on these from
> this list is much appreciated.
> 
> ------
> SOLUTION 1: Connect to the Servlet Engine like a browser using a
> URLConnection object and retrieve the response from there.
> 
> -> This solution is probably the most portable (across Servlet Engines) but
> likely isn't the most performant.
> 
> ------
> SOLUTION 2: Using Tomcat Interceptors.
> 
> -> I don't know anything about these but would there be anyway to catch and
> redirect the response using an Interceptor.  I assume such a solution would
> only work with Tomcat.
> 
> ------
> SOLUTION 3: A custom Interceptor Servlet with a custom Response object.
> 
> -> A custom method could be called on such a GenericServlet and these
> servlet would include the body of the JSP using the
> RequestDispatcher.include method.  A custom Response object would output the
> Writer stream to a String or StringBuffer.
> 
> I've attached some sample source code which outlines what I have in mind.
> 
> Could someone give me a reality check on whether this is even feasible or
> advisable.  Particularly with regard to rolling my own Response
> implementation which redirects Writer to a String.  Would this kind of
> kludge cause any problems for Tomcat?
> 
> ------
> Any comments, help or suggestions that anyone can provide are much, much
> appreciated.  Also, I'd love to hear alternative solutions.  Thanks so much.
> 
> Regards,
> 
> Sasha Haghani,
> Toronto, Canada.

-- 
----------------------------------------------------------------------
Glenn Nielsen             glenn@more.net | /* Spelin donut madder    |
MOREnet System Programming               |  * if iz ina coment.      |
Missouri Research and Education Network  |  */                       |
----------------------------------------------------------------------

Re: Redirecting the Response of a JSP to a String

Posted by Jon Stevens <jo...@latchkey.com>.
on 4/26/01 12:38 PM, "Sasha Haghani" <sh...@brightspark.com> wrote:

> Any comments, help or suggestions that anyone can provide are much, much
> appreciated.  Also, I'd love to hear alternative solutions.  Thanks so much.

Solution 4)

Use Turbine as your Controller. Turbine is a servlet that essentially
proxy's connections to a JSP page. Process looks like this:

Request -> Turbine -> JSP Page is executed         -> Response is sent
                      data is buffered in Turbine

It is possible within Turbine to directly call a JSP page to be rendered and
pass in the right req/res objects to generate the page.

This is a model which you could either use or emulate.

Note: Velocity is a FAR FAR FAR better solution for this because unlike JSP,
Velocity was designed to be used as more than just a Servlet template
system. You can therefore *easily* use Velocity to process your email
template to send the email...

@see <http://jakarta.apache.org/velocity/ymtd/ymtd-embedded.html>

You make the decision.

-jon

-- 
If you come from a Perl or PHP background, JSP is a way to take
your pain to new levels. --Anonymous
<http://jakarta.apache.org/velocity/ymtd/ymtd.html>


Re: Redirecting the Response of a JSP to a String

Posted by Jon Stevens <jo...@latchkey.com>.
on 4/26/01 1:43 PM, "Craig R. McClanahan" <cr...@apache.org> wrote:

> A fourth solution (also portable) is to look at the Filter capability in
> the servlet 2.3 API (supported by Tomcat 4.0).
> 
> Craig McClanahan

The only problem with this is that Catalina is bleeding edge and while it
will be in the 2.3 API, not all containers are going to support it right off
the bat and I don't think that Catalina's web connector is well tested in
production use yet. :-)

Reality sets in...

-jon


Re: Redirecting the Response of a JSP to a String

Posted by "Craig R. McClanahan" <cr...@apache.org>.
A fourth solution (also portable) is to look at the Filter capability in
the servlet 2.3 API (supported by Tomcat 4.0).


Craig McClanahan


On Thu, 26 Apr 2001, Sasha Haghani wrote:

> Hi there,
> 
> ------
> DISCLAIMER:  This a Tomcat question, where the solution will likely require
> some tinkering with the implementation.  What I'm to do is probably a
> non-standard use case.  I think knowledge of the Tomcat internals is
> required to answer this.  I hope this question is appropriate for this list
> -- if not I apologize in advance.  Also, sorry for the length...
> ------
> 
> I'd like to be able to invoke a JSP on-demand from a Java class and redirect
> the response output stream to a String.  This could be used to dynamically
> generate the body of an e-mail, for instance.  The key difference is that
> there isn't a *real* corresponding HTTP request or browser socket connection
> which would trigger JSP compilation and execution.
> 
> I've come up with 3 potential solutions.  Any input or comment on these from
> this list is much appreciated.
> 
> ------
> SOLUTION 1: Connect to the Servlet Engine like a browser using a
> URLConnection object and retrieve the response from there.
> 
> -> This solution is probably the most portable (across Servlet Engines) but
> likely isn't the most performant.
> 
> ------
> SOLUTION 2: Using Tomcat Interceptors.
> 
> -> I don't know anything about these but would there be anyway to catch and
> redirect the response using an Interceptor.  I assume such a solution would
> only work with Tomcat.
> 
> ------
> SOLUTION 3: A custom Interceptor Servlet with a custom Response object.
> 
> -> A custom method could be called on such a GenericServlet and these
> servlet would include the body of the JSP using the
> RequestDispatcher.include method.  A custom Response object would output the
> Writer stream to a String or StringBuffer.
> 
> I've attached some sample source code which outlines what I have in mind.
> 
> Could someone give me a reality check on whether this is even feasible or
> advisable.  Particularly with regard to rolling my own Response
> implementation which redirects Writer to a String.  Would this kind of
> kludge cause any problems for Tomcat?
> 
> ------
> Any comments, help or suggestions that anyone can provide are much, much
> appreciated.  Also, I'd love to hear alternative solutions.  Thanks so much.
> 
> 
> Regards,
> 
> Sasha Haghani,
> Toronto, Canada.
>