?
Solved

Using jsp engine within a servlet for generating dynamic email messages based on jsp pages

Posted on 2003-03-25
10
Medium Priority
?
238 Views
Last Modified: 2010-04-01
I know. It sounds crazy.

But, I'm trying to generate email messages dynamically within a servlet and send them with inputs that I got from the servlet request.

Since my application is using JSP pages and this seperates the view from the model I thought of using the same engine for my internal use. This way users could change the email template as they wish and my application will keep on plugging in the appropriate data.

One posibility that I came up with was to use a template builder such as Apache's Velocity, but that will just introduce a new scripting language on top of JSP for my users.

I thought of creating my own request and response that will buffer the response, but I'm not sure how to invoke the jsp engine from within my servlet. I will also need the control back from the engine in order to email the message and send response back to my web client.

I'm running on top of an Apache Tomcat v4.1.18, but would rather find a solution which is cross servlet/JSP engines. I will need it to run as part of a war file that will also be installed on a BEA WebLogic and IBM WebSphere.

Thanks for the help,
Guy.
0
Comment
Question by:optimal
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 

Expert Comment

by:javaexpert
ID: 8209356
I'm not sure if I understand your question correctly.

If you are trying to send request to a servlet via a JSP page, then you can do one of two things:

1) create a <form> object in JSP where the action attribute points to the name mapped to your servlet, like

     <form name="email_form" action="/servlet/email_servlet" method="post">
     ... ...
     </form>

2) or if you want to control some data validation and manipulation at the JSP page itself, then you can forward the request to servlet after the JSP is done with its task, like

     <%
request.getRequestDispatcher("/servlet/email_servlet").forward(request, response);
     %>


Hope this helps.  Otherwise, please explain your problem again.


0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8211003
It doesn't sound crazy to me :) although I haven't done it myself, I can see it is feasible.
I assume you know how to use javamail in your servlet to send out email. you'll need to implement your own response object, the outputstream of your response object will be a ByteArrayOutputStream. Use that response object to forward to your jsp page that generates email message. for example:

// MyEmailResponse implments HttpServletResponse
HttpServletResponse email = new MyEmailResponse();
RequestDispatcher emailJsp = getServletContext().getRequestDispatcher( "/path/email_template.jsp" );
emailJsp.forward( request, email );

// process email object to get the content of email generated by email_template.jsp

let me know if you have any thought, I'll be happy to be involved when you need to work into the detail of implementing your own response object.

0
 

Author Comment

by:optimal
ID: 8211061
I'm trying to take the same approach as the basic servlet/JSP MVC design pattern takes. But unlike the standard approach, where a servlet does the control and the JSP just the view, I would like to add another step where the servlet is using another JSP page in order to generate an email message. This other JSP page is my template for the email message.
The control should than get back to the servlet which should send the generated email message and only than give control to the response JSP page that will generate a response to the client.

I hope this is a bit clearer.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 19

Expert Comment

by:cheekycj
ID: 8211126
if you attach all required objects the JSP needs to the request object and forward to the email jsp in the servlet, after the jsp is done processing your control is returned to the servlet.

Is it that simple or am I missing something?

CJ
0
 

Author Comment

by:optimal
ID: 8211130
Thanks kennethxu,

I have the email piece working and I agree I will probably have to write my own Response object (actually, I already did) and forward it instead of the original one to the "email" JSP.
But the question is how can I gain control back in the servlet after the forward command:
emailJsp.forward( request, email );

Is it gurantied that the message template had finished generating the message?

I suppose it might be running in a new thread, otherwise the servlet engine might get locked.

Is it a requirement in the spec that RequestDispatcher will work in a synchronous manner? or is it possible that different engine implementations are just generating a new thread which is processing the next phase asynchronously?
0
 

Author Comment

by:optimal
ID: 8211159
Hi cheekycj,

I agree that the control gets back to the servlet, but say the JSP is huge and sending the result to the outputstream takes time. Is it gurantied that the servlet is locked or can it be that I will get the control back in the servlet before the JSP finished sending the stream to the response output stream?
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 8211185
not sure about that.

AFAIK, control is returned to the Servlet before the response is sent back to the client browser so the JSP can finish adding to thre response output stream and then the servlet can do whatever it wants.

If this weren't the case doing includes of JSPs inside of JSPs wouldn't work properly right?

CJ
0
 
LVL 19

Accepted Solution

by:
cheekycj earned 1500 total points
ID: 8211201
remember JSPs are converted to Servlets.

so when you do an include/forward.  if control wasn't returned to the main JSP (i.e. servlet) for it to continue with its processing it would be a major flaw in the design :-)

I know that we have several servlets that forward to JSPs and then clear session vars etc after the forward statement so after the JSP is done processing we can clear our sesssion data.

CJ
0
 

Author Comment

by:optimal
ID: 8211246
I guess you must be right. I was a bit worried because I'm running on Tomcat and Struts and I wasn't sure that Struts' control flow is not managed through some differet mechanism.
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8211378
after emailJsp.forward( request, email );, the control will sure return back, regardless of what add on MVC been used. this is by specification. you just need to process the email object which contains the content fo email, but remember that it includes all the http headers, I think using emailJsp.include( request, email ); might eliminate those http headers.

actually, forward() and include() are basically the samething, except that forward will clear anything already written to the output buffer and clear http headers too.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The well known Cerber ransomware continues to spread this summer through spear phishing email campaigns targeting enterprises. Learn how it easily bypasses traditional defenses - and what you can do to protect your data.
Hey fellow admins! This time, I have a little fairy tale for you. As many tales do, it starts boring and then gets pretty gory. I hope you like it. TL;DR: It is about an important security matter, you should read it if you run or administer Windows …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month14 days, 17 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question