• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to handle multiple response in a servlet

 
muusa muzammil
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey friends i am stuck up with an issue.
I am making my servlet to give to responses at the same time but my servlet only recognizes the first response .
In that case i realized the servlet could handle only one response at a time and it gets commited.
so is there any way around i could handle the other response.
here downloader is a method which returns a response tht is downloading a file
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The HTTP protocol does not allow multiple responses. A request comes in, and the server serves a response. End of story. That's why, once a response is commited, following code will not have any effect on the response.
 
muusa muzammil
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any other work around for the other response to be handled
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check this link
http://snippets.dzone.com/posts/show/4629
 
muusa muzammil
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote the code for file download i knw but how can i handle the other response in the same project.or like can i use requestdispatcher() to handle the other response
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no "other response". One request == one response. What "other response" are you talking about?
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you asking how to support multiple request/response pairs at once?

Say Bear and Pat both use their browsers to request something from your servlet, and you want to handle both at the same time? Then while those two are in process (say they have slow links) Sebastian uses his browser to make a request, so now you have three in process at once?

As others have said upthread, there can be only one response to a request. Its the way the protocol was defined.

Another hack is to use AJAX, where I can make a request, the response contains a bit of AJAX that makes a second call without me knowing it. Then your servlet can answer the second (hidden) response and more data can flow down.
 
afsar khan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even if you try to do something with second response servlet container will through a exception with "Response already committed".
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
afsar khan wrote: container will throw a exception with "Response already committed".


Which is correct. Its really not a 'container' issuer, its the basic RFC of the HTTP protocol.

One request gets one and only one response. Its a feature
 
afsar khan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for correcting me.
 
muusa muzammil
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
see what is happening is that there is one request and two responses to that request.first response return an html page containing the output and the second response for which i have used a function downloader returns the download class file .is there a way in which it could be done
 
muusa muzammil
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
let me give a description of the scenario .
there is a source file on my server .I am compiling the java source file which is giving me a class file in the same location.
now i am executing the class file which is returning me an input stream and that i m writing as an response to the servlet .
now at the same time i while the response is written i want another response which returns me download the file.
I am able to do both things separately but when i write the code together the first response only works.
now if the situation is clear i would like to ask is there a way that after first response my second response ie download also work.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As has been pointed out several times by now, one request means one response. Unless there is a second request, there won't be a second response.

I don't fully understand what you're trying to do, so I can't advise if there are other ways to accomplish something similar. You want to return both some content generated by a method in a class, and the class file that contains that method?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13073
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One request gets one response.

The normal way to handle this would be to create the first response as HTML with links to the other resources you want to return or display.

Think about a HTML page with an image in it, that was NOT created all in one response, the HTML page had an img tag that will cause the browser to create an additional request which returns the response.

Bill
 
muusa muzammil
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey thanks friends .If this could be done the HTML page containing other request that would handle the response would do.Could you give me an example so that i implement it into my problem.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic