File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet specific attribute

 
Faisal Ahmad
Ranch Hand
Posts: 355
Chrome Java Redhat
  • 0
  • Mark post as helpful
  • send pies
  • 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!
 
Piotr Nowicki
Ranch Hand
Posts: 610
IntelliJ IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • 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!
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2167
57
Android Chrome Eclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • 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: 355
Chrome Java Redhat
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's now clear.
Thanks so much!
 
munjal upadhyay
Ranch Hand
Posts: 69
Java Mac OS X Oracle
  • 0
  • Mark post as helpful
  • send pies
  • 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
Saloon Keeper
Pie
Posts: 2167
57
Android Chrome Eclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • 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
Java Mac OS X Oracle
  • 0
  • Mark post as helpful
  • send pies
  • 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
Java Mac OS X Oracle
  • 0
  • Mark post as helpful
  • send pies
  • 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
Saloon Keeper
Pie
Posts: 2167
57
Android Chrome Eclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this link and see if it helps:
Java EE5 Tutorial

Regards,
Frits
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic