Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to retrieve generated HTML

 
Azz Romaysa
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

I've a servlet that generates an order as HTML code. The result of this order can be viewed in a jsp file.

My problem is how can I get the generated HTML so that I can send it to the user in an email seeing that the order contains one of more items + billing address etc..

Thanks,
 
Jesus Angeles
Ranch Hand
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can use a filter

you will need to make a response wrapper too

that wrapper will contain the html generated
 
Azz Romaysa
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how do i then save my data into wrapper?
and how to extract these data from wrapper before sending it via mail?

i realy do not have a good picture of what you mean, could you please some sample code??

thanks alot
 
Jesus Angeles
Ranch Hand
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you will need to learn Filter interface, and HttpServletResponseWrapper

the filter will allow you to 'intercept' things that happen during the request, in relation to servlet or jsp.

the wrapper will 'cheat' the servlet into passing the html into the wrapper first, and not directly to client. as you will learn in filter thing, after the dochain method, your wrapper will then contain the html.

at that point, can do what you want with the html.

i suggest you learn those 2 nice things, the Filter and HttpServletResponseWrapper. Here's a million dollar tip: 2 of marty hall's books contain lots of code. im 99% sure one of those books have what you want. the books are "Core Servlets and JSP", and "More Servlets and JSP"
 
Azz Romaysa
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a method which can retrieve "generated HTML by jsp" from a servlet?
The generated HTML by jsp (this is what the client sees: this contains data) is the content we want to send by email.
 
Heonkoo Lee
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seeems like Jesus gave you a good direction. If you put your existing code here, you will get more specific advice.
 
Azz Romaysa
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My JSP Order View contains next code:
while(iteratorValue.hasNext())
{
Shoppingcartitem shoppingcartitem = null;
shoppingcartitem = (Shoppingcartitem)iteratorValue.next();
%>
<tr>
<td><%= shoppingcartitem.getDescription() %></td>
<td><%= shoppingcartitem.getQuantity() %></td>
<td><%= shoppingcartitem.getItemPriceString() %></td>
<td><%= shoppingcartitem.getLineItemPriceString() %></td>
</tr>
<%
}
%>
<tr>
<td></td>
<td><strong>Totaal:</strong></td>
<td></td>
<td><strong><%= shoppingcart.getTotalPriceString() %></strong></td>
</tr>
<tr>
<td></td>
<td><strong>Verzenkosten:</strong></td>
<td></td>
<td><strong><%= currency.getCurrency(shipping) %></strong>

This jsp generates orderview containing items, prices,.., and conditions.
Now i want to send by mail to clients the same view as the orderview presented by the webbrowser. But how can i read the html (orderview) in serverside and send it by mail?

my mail code:
try
{
message.setSubject(subject);
message.setContent(content, "text/html");
}
catch(MessagingException messagingexception)
{
log.error("Error while trying to send subject and/or content "+
messagingexception);
}

And my servlet sent the mail by next code:
mailer.setTO(customerRecipient);
mailer.setBCC(webShopAdminRecipient);
mailer.setFROM(webShopAdminSender);
mailer.setContent("Overzicht van Uw bestelling bij mysoyq","Uw overzicht");
mailer.send();

regards,
 
Heonkoo Lee
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can think of two ways to achieve the goal.

1. Generate the HTML in a servlet, store it in a string, and put it in the request object. Then forward to a JSP to display the HTML. After that, put the logic sending email in the servlet.


2. Use ServletResponseWrapper. Here is an article from Sun, http://java.sun.com/products/servlet/Filters.html
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://simple.souther.us/capture.war does this.

It captures the output and writes it to stdout.
 
Azz Romaysa
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot for this good example..but in my case i have a servlet witch dispatches evey request to a destination. so i can not define my url patterns in web.xml. How can i call the filter from servlet and receice the html in my servlet?
 
Jesus Angeles
Ranch Hand
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what do you mean "which dispatches every request to a destination"

when you say 'destination', do you mean, the browser who triggered the servlet?

or do you mean, another servlet, or jsp?
 
Azz Romaysa
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My webapplication is designed as MVC: for each scenario i use a servlet as controller and dispatcher (depending on requests from the user some action happens: for example a forward to a jsp) read requests from View and takes some action.In my case I have a servlet reading inputs and forwards to jsp's en again from jsp's the servlet is called agian...and somewhere in my servlet i need to reed the HTML code and send that to a customer. By destination i mean a jsp.
 
Jesus Angeles
Ranch Hand
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
filter can be used both for servlet or jsp.

for you, you would use it for the jsp - the jsp that would generate the html that you will send via email.

you would also need a response wrapper, as i mentioned above.

so you need 2 classes, the filter and the wrapper; and you need to define this filter on the web.xml, as a filter to the concerned jsp.
[ September 30, 2005: Message edited by: Jesus Angeles ]
 
Jesus Angeles
Ranch Hand
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
from the sample application provided by Ben above,

in CaptureOutputFilter.java,

you will see

"System.out.println(responseWrapper.toString());"

this is exactly where you will insert your business logic to send the email. As you can see, responseWrapper.toString() returns exactly what you need - the entire html that is about to go back to the client.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic