wood burning stoves 2.0*
The moose likes Servlets and the fly likes How to call multiple servlets from a single form? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to call multiple servlets from a single form?" Watch "How to call multiple servlets from a single form?" New topic
Author

How to call multiple servlets from a single form?

Travis Gibson
Ranch Hand

Joined: Oct 17, 2000
Posts: 100
Howdy fellow ranchers,
Is it possible to generate multiple requests from a single form which are passed to different servlets.
(i.e. The user hits submit and two different servlets receive requests?)
Any suggestions would be greatly appreciated.

------------------
Regards,
Travis M. Gibson, SCJP
Java Developer
www.travismgibson.com
travis@travismgibson.com


Regards,<BR>Travis M. Gibson, SCJP<BR>Java Developer<BR>www.travismgibson.com<BR>travis@travismgibson.com
Andrew Shafer
Ranch Hand

Joined: Jan 19, 2001
Posts: 338

Uhmm, why??
What functional advantage would you get if this was a possibility?
As far as I understand, forms have one action. A form will submit one request. What you do with that request could have an infinite number of servlets.

!_I_Know_Kung_Fu_!
Travis Gibson
Ranch Hand

Joined: Oct 17, 2000
Posts: 100
Andrew,
We have a JSP form. Once the form is complete it is written to a database however we also want to kick off an additional servlet
which will send an e-mail to the person who entered the form.
Forwarding the request through the database update servlet to the e-mail servlet is not an option.

------------------
Regards,
Travis M. Gibson, SCJP
Java Developer
www.travismgibson.com
travis@travismgibson.com
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Can't we do using JavaScript? (If you prefer to use JavaScript ). May be we can have another form in the same page. Like FormA and FormB. FormA's "action" call EmailServlet and FormB's action will call DbUpdateServlet.
But when FormB's Submit is clicked, you write an onClick JavaScript function which will first submit
formA and then formB. You may have to have hidden fields in FormA which are needed for EmailServet to send email.
regds
maha anna
Travis Gibson
Ranch Hand

Joined: Oct 17, 2000
Posts: 100
Thanks Maha,
I didn't even think of that but you are right. I can use an OnClick event.
You and you example have been an inspiration to me. I used your website a great deal while I was chasing my SCJP. Now that I am a Java Developer I still find your advice very valuable.

------------------
Regards,
Travis M. Gibson, SCJP
Java Developer
www.travismgibson.com
travis@travismgibson.com
sachin dabhade
Ranch Hand

Joined: Mar 10, 2001
Posts: 73
its not possible i suppose bcoz
for every submit of form, there is action defined in form defination and we can define only one action for one form.
correct me if i am wrong
sachin
Andrew Shafer
Ranch Hand

Joined: Jan 19, 2001
Posts: 338

I think Maha is right, as usual.
But it seems like you are better off handling things on the server side.
In order to have the form make two request, you will need to do JavaScript to read the input from the visible form and send this as a seperate request in an onClick event.
Seems like a big pain in the behind, when you could handle all this with one request on the server side.
If you don't want to forward anything from the Dbase servlet (why not?) then maybe a controling servlet that can call both the db servlet and the email servlet.
Benefits= one request + pure java
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Personal choice would be do all control,business logic AND form validation at server side and make the client as thin as possible.
The decision may also depend on the variety of audience. Whether you could control the users. For example some corporate web applications are used by only known audience( example : employees of a company) and they can be asked to turn on Javascript. But if it is going to reach wide audience, for example an e-commerce web appln, if you want to make this web application more successful, then better not to insist the user to use javascript.
I have talked to some companies who have ASP models say our clients will use only IE.X.X version wirh java and javascript enabled browser and still successful.

regds
maha anna
[This message has been edited by maha anna (edited April 26, 2001).]
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
What Travis wants to do is so tricky because he is using the wrong tool for the job. A HTTP servlet (or JSP, or ASP) is a component which receives HTTP client requests and generates a HTTP responses to those clients. Anything that is not part of a chain which starts with the HTTP request and ends with the HTTP response should not be a servlet, JSP, ASP or whatever.
The database update is best done in a JavaBean, class, custom tag, or EJB. If you want the update to happen asynchronously, i.e. move it out of the request->response chain, consider using JMS. Or in its most basic form, kick off a separate thread for the purpose.
The e-mail facility does not want to be a servlet at all. Again, a JavaBean, class, tag, or (session) EJB could all be appropriate. And here too, if you want it to happen asynchronously, use JMS or a thread. A slightly more sophisticated alternative to kicking off a Thread for every e-mail would be to have an application-bound Java object which receives sendEmail() requests and has its own permanently running Thread in which it sends them off to the mail server.
- Peter

[This message has been edited by Peter den Haan (edited April 27, 2001).]
lakshmi ananthamurthy
Greenhorn

Joined: Apr 26, 2001
Posts: 5
What you could possibly do is call a single servlet which has include statements to call different servlets. This will probably mimic the call to multiple servlets.
However, I doubt there is a way (even using javascript) that you can call multiple servlets with a single click. Even if you use javascript, once a servlet is called, it loses control over the client and the control passes to the server.
You could probably use client pull type code if you want to call multiple servlets after intervals, but frankly, how it is implemented is something I have not tried as yet.
manav kher
Ranch Hand

Joined: Nov 21, 2000
Posts: 98
sure its easy to submit a single form to multiple sources but once you submit which servlet do u want to receive the next page from?
let me know and i can be more specific abt the code that can help u do that ...but a more general example is by using javascript have a function that gets called when the form is submitted onSubmit="callSubmit(this.form,'http://mywebserver ort/servlets/myServlet1')")
function callSubmit(form, servletSrc)
{
form.target = "_new"; /* this will display the input from the servlet in a new page while the old page is still open in the background */
form.action = servletSrc;
form.submit();
}
i hope u get the picture ??
-manav
 
wood burning stoves
 
subject: How to call multiple servlets from a single form?
 
Similar Threads
Is a Servlet thread-safe
Servlet Container
Reset the Action form values
Q:17 JavaRanck mock exam
CGI and servlets