aspose file tools*
The moose likes Servlets and the fly likes Servlets, emails and Servlets...... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlets, emails and Servlets......" Watch "Servlets, emails and Servlets......" New topic
Author

Servlets, emails and Servlets......

Sam Tilley
Ranch Hand

Joined: Dec 05, 2001
Posts: 160
Hi,
I have written a little program which takes in data, manipulates it and writes it to a text file but i have a little problem getting it to do a few more things.
Basically i have my bookingServlet which takes in a few parameters from a jsp page, the client then enters their details and this sends their data along with the manipulated jsp data to receivedServlet which then manipulates the data and writes to a text file and has some dynamic text on it.
What i would like to do is when i press submit from bookingServlet my little program also emails my form data to someone but still ends up at my receivedServlet where i can do my data writing. Is this possible??? Can i have 2 actions, one to a cgi-bin and one to my servlet or is there a better way to do this. Unfortunately i don't have access to a database and am not too hot on ASP.
Any help would be appreciated.
Thanks


Sam Tilley SCJP, SCWCD
Sam Dalton
Author
Ranch Hand

Joined: Jul 26, 2001
Posts: 170
You could handle the emailing in the same servlet as you are writing the file. The JavaMail API provides a very convienient way to send mail via SMTP etc. Otherwise you could do it manually by opening up a URLConnection to the cgi-bin program and passing the request parameters to it.... this would be a quick and dirty way to do it though.
Cheers
Sam


<a href="http://www.samjdalton.com" target="_blank" rel="nofollow">Sam Dalton</a>,<br />Co-author of [http://www.amazon.com/exec/obidos/tg/detail/-/1590592255/qid=1068633302//ref=sr_8_xs_ap_i0_xgl14/104-4904002-9274339?v=glance&s=books&n=507846]Professional JSP 2.0[/URL] (October 2003)<br />Co-author of <a href="http://www.amazon.com/exec/obidos/ASIN/1861007701/ref=ase_electricporkchop" target="_blank" rel="nofollow">Professional SCWCD Certification</a><br />Co-author of <a href="http://www.amazon.com/exec/obidos/ASIN/186100561X/ref=ase_electricporkchop" target="_blank" rel="nofollow">Professional Java Servlets 2.3</a>
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12823
    
    5
You can certainly compose and send email from a servlet with the JavaMail API. Because sending mail requires the cooperation of a mail server, you have to handle situations in which the mail server is slow, not up right now, etc. without causing your sevlet to hang.
I suggest you have the servlet pass the mail to a separate object running its own Thread.
Bill
Paul Done
Greenhorn

Joined: Jan 03, 2003
Posts: 22
I prefer to use JMS rather than spawning my own threads to achieve asychronous processing. From the servlet (or a filter if you want to seperate out concerns fully) I would use JMS to put the mail information into a message and onto a queue, before processing the rest of the servlet. Create a seperate Message Driven Bean which listens to this queue and processes the messages in its own time, using Java Mail.
Paul
Sam Tilley
Ranch Hand

Joined: Dec 05, 2001
Posts: 160
Thanks guys for the great (and quite distinguished) feedback, ill have a look at the different options and see how i get on.
[ January 08, 2003: Message edited by: Sam Tilley ]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12823
    
    5
Yeah - JMS is cool and can be made to persist the messages in case the server goes down. Which implementation do you use Paul?
Bill
Ken Shamrock
Ranch Hand

Joined: Jan 23, 2002
Posts: 139
>I suggest you have the servlet pass the mail to >a separate object running its own Thread.
Do you know any sample source code for opening a separate Thread doing tasks?Thanks


Thanks everyone who helped me
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12823
    
    5
It is really pretty straightforward, a class that implements Runnable. You create an instance with a Thread that has a low priority. The run method sleeps for some reasonable amount of time, when it wakes up, it looks to see if somebody has left it a job in a Vector or similar collection. Naturally you have to pay close attention to synchronization.
Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlets, emails and Servlets......