Forums Register Login

Could you explain the behavior of a simple servlet?

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
You have a delayed response because you wrote:

Thread.sleep(5000)

inside the servlet's processRequest() method.
+Pie Number of slices to send: Send
 

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)
+Pie Number of slices to send: Send
 

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?
1
+Pie Number of slices to send: Send
 

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.

+Pie Number of slices to send: Send
Oh my Got... Thank you, my fault.... I wanted to create misleading behavior... i wanted to check simultaneous access...
+Pie Number of slices to send: Send
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"...
Could you hold this puppy for a sec? I need to adjust this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1097 times.
Similar Threads
javax.servlet.ServletException: PWC1381: Error allocating a servlet instance
servlet showing blank page.
Not able to add up values in the table of a database
InitialContext Lookup returns the same Stateful Session Bean Reference everytime ...
user authentication and dao
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 13:26:27.