This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Servlets and the fly likes Could you explain the behavior of a simple servlet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "Could you explain the behavior of a simple servlet?" Watch "Could you explain the behavior of a simple servlet?" New topic
Author

Could you explain the behavior of a simple servlet?

Alex Pi
Greenhorn

Joined: Jan 08, 2013
Posts: 17

I am a newbie. I do first steps in servlets.



I use NetBeans IDE 7.3 & GlassFish Server 3.1.2.

I open my JSP several times in a few tabs (Firefox) and send a request to my servlet. Every time i have a result 50. Every next time when i wait a response i need to wait (i guess there is a delay because a servlet is sleeping. But i thought so... There is only one servlet class. Will i have only one instance of servlet for all requests or N-instances of servlet for N-requests? And if i have separated threads for each request (a new thread for a new request) - why i have delay with response? (every time i need to wait longer)?
In fact i expected to have an concurrent access problem (100 -50 -50 -50...)
Thanks.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42028
    
  64
Will i have only one instance of servlet for all requests or N-instances of servlet for N-requests?

You will have one instance, so the "a" field will be shared between all threads, whether they happen concurrently or not.


Ping & DNS - my free Android networking tools app
Nam Ha Minh
Ranch Hand

Joined: Oct 31, 2011
Posts: 498

You have a delayed response because you wrote:

Thread.sleep(5000)

inside the servlet's processRequest() method.


PM Me If You Want to get Urgent Help on Java Programming
Alex Pi
Greenhorn

Joined: Jan 08, 2013
Posts: 17

Ulf Dittmer wrote:
Will i have only one instance of servlet for all requests or N-instances of servlet for N-requests?

You will have one instance, so the "a" field will be shared between all threads, whether they happen concurrently or not.


But why do i have always the result 50.
I thought i will subtract 50 every time. (the result will be 50, 0, -50, -100... so on)
Alex Pi
Greenhorn

Joined: Jan 08, 2013
Posts: 17

Nam Ha Minh wrote:You have a delayed response because you wrote:

Thread.sleep(5000)

inside the servlet's processRequest() method.


Yes. But it's not so easy.
I modified my code like that:


(4 requests from different tabs) produced following result:

Value is: 50
Thread name is: http-thread-pool-8080(5)

Value is: 50
Thread name is: http-thread-pool-8080(4)

Value is: 50
Thread name is: http-thread-pool-8080(1)

Value is: 50
Thread name is: http-thread-pool-8080(2)

It looks like i have different instances for each request-thread, and different threads for each request. Is it correct?
Bhavesh Saraswat
Greenhorn

Joined: Jan 20, 2013
Posts: 18

Alex Pi wrote:
Ulf Dittmer wrote:
Will i have only one instance of servlet for all requests or N-instances of servlet for N-requests?

You will have one instance, so the "a" field will be shared between all threads, whether they happen concurrently or not.


But why do i have always the result 50.
I thought i will subtract 50 every time. (the result will be 50, 0, -50, -100... so on)



Hi ,

The code is fine and working correctly.

Actually you should try to go through your model class (Asynch) once again.
here x is an instance variable, so it is initialized only once when you create a object of Asynch class. So in get method basically you are subtracting 50 from x which is 100 in first iteration. So if condition gets true in first iteration and x is assigned a new value i.e. 50. from now on if condition will never gets true because 50 > 50 is false. So from now on each iteration the value of x remains unchanged and 50.

In this way you will get same value for each iteration.


OCPJP SE6
Alex Pi
Greenhorn

Joined: Jan 08, 2013
Posts: 17

Oh my Got... Thank you, my fault.... I wanted to create misleading behavior... i wanted to check simultaneous access...
Alex Pi
Greenhorn

Joined: Jan 08, 2013
Posts: 17

I changed my code:


Hurra! I received what i wanted:
Value is: 60
Thread name is: http-thread-pool-8080(4)

Value is: 20
Thread name is: http-thread-pool-8080(5)

Value is: -20
Thread name is: http-thread-pool-8080(1)

I wanted to receive "Race condition"...
 
GeeCON Prague 2014
 
subject: Could you explain the behavior of a simple servlet?