• Post Reply Bookmark Topic Watch Topic
  • New Topic

JSP translated into a Servlet

 
Faber Siagian
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear ranchers,

From Head First Servlet and JSP i know that a JSP will be translated to be a Servlet by the container.

And I tried the example :


I refreshed the page, the count got increased. This is normal.
But when I open a new window and open the jsp page, the count continued, while i expect it reset to zero.

And the most confusing part for me, the count still continued even i close the page.

Would someone explain to me about case above?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You declared your int to be shared across all invocations. Why should you be surprised?
 
Faber Siagian
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok let me tell you about how do i look this case.

The value of count will be increased though i open more than one window, because every request is handled by a thread, with the same servlet.

Next..
I think the servlet instance will be destroyed if all of the request is processed completely. So when i close all of the browser, then i open a new one, count's value should be reset to zero.

But actually the value of count still continued even though i close the browser. Here where i get confused.
 
Brij Garg
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int is a part of the declaration section in you code.
Whatever we declare in this section becomes instance memeber of the class.

You must be knowing when container invokes the servlet,init method is called just once.All request by threads are handled by service method.

In your case there is just one instance of this class in the memory and Whne you opened new session, instance is still there in the memory.
So you have two different threads.
instance variable are shared by threads.

I hope this hepls you.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Faber Siagian:

Next..
I think the servlet instance will be destroyed if all of the request is processed completely.

Why would you think this?
 
Faber Siagian
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually i missed something.
A servlet instance will be destroyed if the servlet container is shut down or the container frees up the memory.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!