capturing the output of a JSP

A

Andy Fish

Hi,

From my servlet I would like to be able to invoke a JSP (as part of the same
session), but rather than returning the output to the user I would like to
capture it as a stream or string or something.

from the servlet spec, it seems that the best way would be to use
RequestDispatcher to invoke the JSP, but then I would have to implement
ServletResponse myself to catch the output, which all seems rather onerous.

What I am actually trying to achive is an email template system, and JSP
just seemed like a very neat way to do it. However, any alternative
suggestions would be welcomed - at the moment my requirement is only for
simple variable substitution into plain text emails.

Andy
 
E

Erwin Moller

Andy said:
Hi,

From my servlet I would like to be able to invoke a JSP (as part of the
same session), but rather than returning the output to the user I would
like to capture it as a stream or string or something.

from the servlet spec, it seems that the best way would be to use
RequestDispatcher to invoke the JSP, but then I would have to implement
ServletResponse myself to catch the output, which all seems rather
onerous.

What I am actually trying to achive is an email template system, and JSP
just seemed like a very neat way to do it. However, any alternative
suggestions would be welcomed - at the moment my requirement is only for
simple variable substitution into plain text emails.

Andy

Hi Andy,

Do yourself a huge favor and just use variable substitution on a template
instead of catching JSP-output. That is easy to implement.

Unless, of course, you want to do a lot of learning on the (quite complex
IMHO) servletinternals and the Response and Request objects.
It is also quite complex to create a fresh valid ServletRequest from within
your code. And you will need that to 'feed' to your JSP.

My advise would be to go for some substitution on a template and email that.

just my 2 cents..

Regards,
Erwin Moller
 
A

Andy Fish

thanks for quick response.

do you know of any standard classes that I can use to substitute named
values into a piece of template text, or should I just roll my own?

"Erwin Moller"
 
E

Erwin Moller

Andy said:
thanks for quick response.

do you know of any standard classes that I can use to substitute named
values into a piece of template text, or should I just roll my own?

I know a few classes are out there for that, but I couldn't name them.
I rolled my own.
Just some basic search/replace routine will do the trick.
I did it like this:

String strBody = "";
strBody += "Hello *USERNAME*,\n";
strBody += "\n";
strBody += "Is this (*USEREMAIL*) still your email? ";

and just search replace the placeholders.

(In my case I pulled the template out of a database, and Adminpeople could
edit thoose templates.)

Regards,
Erwin Moller
 
C

Chris Smith

Andy said:
thanks for quick response.

do you know of any standard classes that I can use to substitute named
values into a piece of template text, or should I just roll my own?

Andy,

For simple needs, it may really be simpler to roll your own than to
apply some existing API. If your needs grow more complex in the future,
then this looks like an ideal application for Jakarta Velocity. It's
initially intended as a replacement for JSP, but it's also easy to apply
outside of a web app context.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,278
Latest member
BuzzDefenderpro

Latest Threads

Top