File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes HTML->Servlet(mutiple request) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "HTML->Servlet(mutiple request)" Watch "HTML->Servlet(mutiple request)" New topic

HTML->Servlet(mutiple request)

Angela D'souza
Ranch Hand

Joined: Jan 16, 2002
Posts: 469
I have one html called main.html where I want to make two request at the same time to servlets. Can I make it???
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

If you want, but you have to have a very good reason.
Firstly I have to ask why you need two requests and what it accomplishes. There are two ways to achieve what you want, but they're horrible hacks that are browser dependant and I'm not keen to mention them!
The first solution is to use an error in IE where a form can have a submit button that also executes a submit() method and causes the form to be submitted twice.
The second is to use Javascript and the JS Image object to make a call to load an image (passing all the params on the URL) to load the image in the background, similar to caching images when you're doing image roll-overs. All you care about is that the request is sent to the server.
If you ever quote me as saying you should implement either of these solutions, I'll come looking for you!
Depending on what your requirements are, there may be a third option which is preferable. This is the auto-submitting form. The client sends you the first request, and in the response you send a form with javascript at the end to submit the form. This takes care of request number two for you.
Hopefully this helps. I'm still interested in what is forcing you to send two requests from a single page though!
Gary McGath
Ranch Hand

Joined: Mar 15, 2003
Posts: 52
I don't know if this is related to what you have in mind, but I make use of a Java applet which calls a servlet to gather data. The applet opens an HttpURLConnection, then reads back the servlet's response by calling getInputStream(). If you do this, there are some tricky issues in Microsoft IE's implementation of URLConnection to watch out for. Also, if you want this servlet to handle cookies, you have to do some extra work.
David Hibbs
Ranch Hand

Joined: Dec 19, 2002
Posts: 374
You might want to consider forwarding the request/response after the initial processing completes rather than sending another response. This would be a much more standard way of handling the situation where you need multiple things done after a single form is submitted.
Form->Servlet 1->processing
->request dispatcher->forward
->Servlet 2->processing
->request dispatcher->forward
->Servlet 3
etc etc
This way you only need one request/response pair, you know the order, and you're not tied to any silliness or broken features of a particular browser.

"Write beautiful code; then profile that beautiful code and make little bits of it uglier but faster." --The team, Newsletter 039.
Angela D'souza
Ranch Hand

Joined: Jan 16, 2002
Posts: 469
I agree. Here's the link:
subject: HTML->Servlet(mutiple request)
It's not a secret anymore!