• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Servlet specific attribute

 
Ranch Hand
Posts: 363
Firefox Browser Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have come across this statement on page # 186 in the book HFSJSP:
There is no servlet specific attribute (just use an instance variable).

I understand, we can't create an attribute specific for a particular servlet. However, what does using an 'instance variable' mean? Could anyone explain with an example.
Thanks in advance!
 
Ranch Hand
Posts: 623
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This relates to the scopes of attributes. As they mention the session, request and context scoped attributes, you might ask "how do I define servlet scope attribute?".

This information is an answer. There is no such thing as servlet scope attribute. If you want to achieve something like this, you can define an instance variable in your servlet class. And what does "instance variable" means? Well... it's no rocket science, as you must have passed the OCPJP ;-)

public class A {
    private int i; // THIS is an instance variable
}



More precisely - they mean this:

public class YourServlet extends HttpServlet {
    private int i; // THIS is an instance variable

    // Rest of the YourServlet details like doGet(...), doPost(...) and bunch of other stuff
}



Cheers!
 
Creator of Enthuware JWS+ V6
Posts: 3412
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Faisal,

Here an workable example, just try it:
web.xml

The servlet:

The jsp:


When you have the example working, you can just hit F5 and see the counter increasing.

Don't forget that you will probably never use an instance variable in a Servlet, and if you do you have to realize that all requests come to the same servlet, so you need to take care of synchronizing the access (which I didn't do in my example)

Regards,
Frits
 
Faisal Ahmad
Ranch Hand
Posts: 363
Firefox Browser Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's now clear.
Thanks so much!
 
Ranch Hand
Posts: 69
Mac OS X Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi !!
I have confussion about the session ..
for example
when we login in the G+ , so session starts , when I play games , or comments the same session continues , when I logout the session ends ...

is it so ? or enything more ?
please explain with example if possible...


 
Frits Walraven
Creator of Enthuware JWS+ V6
Posts: 3412
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

when we login in the G+ , so session starts , when I play games , or comments the same session continues , when I logout the session ends ...
is it so ? or enything more ?


Yes that is how it works in general. The session ends when you logout or when a time-out occurs (e.g. after 10 minutes of no activity)

There is much more but what book are you studying?

Regards,
Frits
 
munjal upadhyay
Ranch Hand
Posts: 69
Mac OS X Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Frits Walraven wrote:

when we login in the G+ , so session starts , when I play games , or comments the same session continues , when I logout the session ends ...
is it so ? or enything more ?



There is much more but what book are you studying?

Regards,
Frits



I prefer head first servlet and java , I am at 5th chapter , but having so many confussion in the end of the 5th chapter.

 
munjal upadhyay
Ranch Hand
Posts: 69
Mac OS X Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please send me some link from which I can get all the information about , the scopes of attributes(session,application,request,page) in detail .


 
Frits Walraven
Creator of Enthuware JWS+ V6
Posts: 3412
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try this link and see if it helps:
Java EE5 Tutorial

Regards,
Frits
 
Paddy spent all of his days in the O'Furniture back yard with this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic