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

Doubt in Servlet threading

Nitin Dubey
Ranch Hand

Joined: Oct 30, 2000
Posts: 126

Hello,

As far as I know only 1 instance of the servlet is created. They are said to be by default Multi Threaded.

1. Can anyone tell me how are they multithreaded ?
2. Lets, assume that our servlet is taking a lot of time in getting the results from database. Now when I Open three different instances of browser and put the url to fetch the records from database what I observe is that only when the first request is complete the second requests is processed. And similarly after second is complete the third request is processed (I could track this down putting a display msg just when you enter any method) Is this not single threaded model ?

FYI: M using Tomcat 5.5.9, J2SDK 1.5.0_03

Cheers,

Nitin
Sharad Agarwal
Ranch Hand

Joined: Sep 11, 2002
Posts: 167
Nitin - servlets are indeed multithreaded. The application server manages the single instance of your servlet and all its multithreading for you. An application developer is thereby empowered to focus on the business problem by implementing specific methods in the servlet.

As for your experiment, it might seem that way due to the sequence in which the requests are being made and the fact that they are taking about the same time to complete. If your third request were for something that would take significantly less time than the first two, you might see it succeed before them.

If you wish to experiment more, you might also want to create instance variables in your servlet class and play with them. You will notice that the value last set is available in all browser instances. Maybe a counter of some sort that counts the number of times the servlet has been invoked.

Hope this helps.

[ June 13, 2005: Message edited by: Sharad Agarwal ]
[ June 13, 2005: Message edited by: Sharad Agarwal ]

Alco-Haul: We move spirits.
Demented Deliberations of a Dilettante
Nitin Dubey
Ranch Hand

Joined: Oct 30, 2000
Posts: 126

Sharad,

Thanks for your reply. But it doesn't seem to work as expected.

[If your third request were for something that would take significantly less time than the first two, you might see it succeed before them.]
- When I send my third request to the server (while the first two are stil processing) it should at least show me a System.out.println("in doGet()"); which is the first statement in my doGet() method of the implementation. But the console does not show me any such msg. Only after the response of either of the first two requests is complete then it shows me doGet() and then doPost() msgs.

FYI: M calling doPost() from doGet(). Logic resides in doPost()

Cheers,


Nitin
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Logging isn't always asynchronous so it may be misleading.

Create Servlet or JSP that takes one parameter "timeout".
Have the thread sleep for n seconds (as determined by the timeout parameter).

Open 3 or 4 browsers and hit that page with all of them.
Use long timeout values for the first couple and short timeouts for the next.

If the servlet is single threaded, the 3rd and forth hits won't return until the first and second are done.

To prove that you are indeed working with the same servlet, do as Sharad instructed and add a second argument that sets an instance variable. You will see that all requests are working with the same servlet.

[ June 13, 2005: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Nitin Dubey
Ranch Hand

Joined: Oct 30, 2000
Posts: 126

Thanks Ben,

It will work as you said. In the meanwhile I could identify the problem. I was using Firefox for testing. As per HTTP - RFC the recommended browser's limitation is to open two requests to a given server at a time. Firefox seems to follow that. As in case of IE this is not the case.

I could not find any option in Firefox where we can change this value. I will put up a request to Mozilla to at least have this configurable as a property.

Regards,

Nitin
Sharad Agarwal
Ranch Hand

Joined: Sep 11, 2002
Posts: 167
Originally posted by Nitin Dubey:
I could not find any option in Firefox where we can change this value. I will put up a request to Mozilla to at least have this configurable as a property.


Firefox is amazingly configurable. In the address bar, type in:
about:config

The parameter you seem to be looking for is:
network.http.max-persistent-connections-per-server

Alternatively multiple firefox instances would do the same thing that multiple IE instances do. Multiple tabs in Firefox run within the same process instance.

Hope this helps.
[ June 13, 2005: Message edited by: Sharad Agarwal ]
 
wood burning stoves
 
subject: Doubt in Servlet threading