• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JSP translated into a Servlet

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic