• Post Reply Bookmark Topic Watch Topic
  • New Topic

second request before first gets completed

 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How the container handle this situation? What happens to the first request?

Making a second request before receiving the response of first request.

Thanks.

[ September 04, 2006: Message edited by: rathi ji ]
[ September 04, 2006: Message edited by: Bear Bibeault ]
 
Amit Mohan Tiwari
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

you did not tell whether implementing SingleThreadModel interface or Or not

If SingleThread Model is implemented then only one request can be processed at a time until the response is delivered to client

If SingleThread Model is not implemented,since service() method is not Threadsafe so if any request is submitted before the response of earlier request then exception is occured
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rathi,
Please fix the spelling in the subject line of this thread.
You spelled "second" correctly in the body of the post.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Amit Mohan Tiwari:

If SingleThread Model is not implemented,since service() method is not Threadsafe so if any request is submitted before the response of earlier request then exception is occured


I think, this is not correct.
 
Jan Groth
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi there,

to my best knowledge each request spawns one thread. so two requests at the same time (which is not soooooo uncommon should you have a real web-app) will of course not throw any exception related to the effect that they _exist_.

but should your code depend on the fact that it's only executed once per call, you should design it in a thread safe way...

hope this catches your question,
jan
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The entire servlet architecture is designed around handling multiple requests by the same servlet object "at one time." This is possible because each request runs in its own Thread with its own request and response objects as controlled by the servlet container.

Yes, this programming environment is REALLY different if your previous programming experience is with single user applications. Don't worry, it all works out.

Bill
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks All.

I think, I couldn't make my question clear.

I know that it all works automatically. But what will happen to that first request if second's response is received before or so...

Hope it is clear now.

Thanks.
[ September 04, 2006: Message edited by: rathi ji ]
 
Dave Robbins
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rathi

I've been wondering about this question so I just read the API docs on SingleThreadModel

look here

http://java.sun.com/javaee/5/docs/api/

and scroll down and read about the interface
it explains it pretty well

Dave

[edit]doh!!, it's been depricated
can I assume all servlets now behave like this??
[ September 04, 2006: Message edited by: Dave Robbins ]
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But what will happen to that first request if second's response is received before or so.

The first request continues to run in its own Thread. IF the servlet has been coded properly with no user data being stored in servlet instance variables, there will be no interference. The API provides the session concept to help keep the user data separate. If both requests are from the same client browser, the browser will keep the responses separate.

If the servlet has been coded improperly the responses may get each other's data mixed up but the actual response output will still go to the right place.

The key phrase here is "coded properly" - understand how the data must be kept "Thread safe" and your doubts should vanish. If not, figure out another way to state them.

The question of which Thread gets CPU time and finishes first is decided by the JVM just like any other multithreaded program.

Bill
[ September 04, 2006: Message edited by: William Brogden ]
 
Vijay Kokatnur
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by William Brogden:

The first request continues to run in its own Thread. IF the servlet has been coded properly with no user data being stored in servlet instance variables, there will be no interference. The API provides the session concept to help keep the user data separate. If both requests are from the same client browser, the browser will keep the responses separate.


Yep. That's rite. I have faced a situation when continuous request from one user was generating separate responses. That means, on "Create Row" user action, the browser was rendering the rows equal to number of requests submitted before the first response being serviced!!!

I solved it using a Javascript!!! The easiest i believe.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!