File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Running same servlet in different browser windows Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Running same servlet in different browser windows" Watch "Running same servlet in different browser windows" New topic
Author

Running same servlet in different browser windows

Viorel Craescu
Greenhorn

Joined: Apr 21, 2008
Posts: 26
Hello,

I'm have the following servlet, deployed on a Tomcat 6.0 server:

When i access it from multiple browser windows, simulating multiple clients, the browser waits to finish the first request, before it start the second one. Sometimes it's possible to access it in more than one browser window, but not more than 6-7 accesses of the same servlet in the same time. Waits for one to finish, before it start another one.
It doesn't make sense since servlet container should create a separate thread for each servlets, and the number of max threads is 150.

Any ideea what is wrong?

Thanks!
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
Any ideea what is wrong?


Why do you think something is wrong? The exact sequence by which all of those browser requests get started on the client side depends on threads managed by the browser and your operating system.

On the server side, the various Threads have to contend with each other and other stuff the operating system is doing.

What is it you are expecting to happen?

Bill
Viorel Craescu
Greenhorn

Joined: Apr 21, 2008
Posts: 26
Thank you for your reply!

I expect all servlets to run in the same time.
Every servlets should run in it's own thread.
The problem is that is not happening all the time. Sometimes they run in sequence, one waiting for another to finish before it starts. Code is not synchronized, so there is no reason to wait.
It's not about the way they start running, some of them just DON'T even start.
It's just like multiple clients accessing same web page. All of them need to be served. If i'm wrong, please correct me.
I don't know if the problem is from the client-side or from the server-side. I didn't get chance to access the web page from more stations. Just opened few browser session and tried to load same servlet. That's why i put there an infinite loop.

Is this a browser issue or the server should act like this?

Viorel
[ July 24, 2008: Message edited by: Viorel Craescu ]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
Every servlets should run in it's own thread.


NO! each HttpServletRequest runs in its own thread. A thread created and managed by the servlet container. There is only one servlet object.

Browser issue? Like I said, the way the browser starts request threads is up to the browser and these threads compete with other operating system threads for CPU time.

Should the server act like this? Be aware that your while(true) loop is hardly the way to test server operation since it tries to keep the port open indefinately. If that is really your code, what did you expect it to do? How can it possibly "finish the first request" before browser or servlet times out the connection?

Bill
Viorel Craescu
Greenhorn

Joined: Apr 21, 2008
Posts: 26
Thank you for you answer!

Yes i know there is just once servlet instance and multiple threads. I just messed my words. )

I think you just answered to my question. Thank you.

But i come up now with another question:




What is the difference between 2 codes from above?
If i put the first one, in the example above, the prinwriter buffered is flushed when i invoke flush() method. If i switch it with second code from above, the buffered IS NOT flushed when i invoke flush() method from print writer class. Is there any difference between those 2 codes? (except they don't work the same)
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
If i put the first one, in the example above, the prinwriter buffered is flushed when i invoke flush() method. If i switch it with second code from above, the buffered IS NOT flushed when i invoke flush() method from print writer class. Is there any difference between those 2 codes?


I think it is extremely unlikely that this is what actually happens, but in any case you should set the response headers before doing anything with the output stream.

Bill
Viorel Craescu
Greenhorn

Joined: Apr 21, 2008
Posts: 26
That's what actually happens on my pc.
Did you test it?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Setting the content type after getting the output stream might be risky. You must know that the server can send the response headers any time after you get the output stream to the client. So if you get the output stream to the client before setting the response content type, then the server might automatically send a content type for the response. Then when you manually set the response content type, it will throw an IllegalStateException. So please set content type before trying to send any response to the client...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Running same servlet in different browser windows