capturing the output of a JSP

Discussion in 'Java' started by Andy Fish, Feb 11, 2004.

  1. Andy Fish

    Andy Fish Guest

    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
     
    Andy Fish, Feb 11, 2004
    #1
    1. Advertising

  2. Andy Fish

    Erwin Moller Guest

    Andy Fish wrote:

    > 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
     
    Erwin Moller, Feb 11, 2004
    #2
    1. Advertising

  3. Andy Fish

    Andy Fish Guest

    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"
    <> wrote in
    message news:4029fc12$0$560$4all.nl...
    > Andy Fish wrote:
    >
    > > 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
     
    Andy Fish, Feb 11, 2004
    #3
  4. Andy Fish

    Erwin Moller Guest

    Andy Fish wrote:

    > 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
     
    Erwin Moller, Feb 11, 2004
    #4
  5. Andy Fish

    Chris Smith Guest

    Andy Fish wrote:
    > 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
     
    Chris Smith, Feb 11, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rick Strahl [MVP]

    Capturing ASPX output from another page

    Rick Strahl [MVP], Jul 9, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    484
    Steve C. Orr, MCSD
    Jul 9, 2003
  2. DigitalUncle
    Replies:
    0
    Views:
    916
    DigitalUncle
    Nov 2, 2004
  3. Replies:
    0
    Views:
    391
  4. Replies:
    2
    Views:
    525
  5. Replies:
    0
    Views:
    4,399
Loading...

Share This Page