aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Servlet specific attribute Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Servlet specific attribute" Watch "Servlet specific attribute" New topic
Author

Servlet specific attribute

Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 340

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

Joined: Jul 13, 2010
Posts: 610

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!


OCP Java SE 6 Programmer, OCM Java SE 6 Developer, OCE Java EE 6 JSPSD, OCE Java EE 6 EJBD, OCE Java EE 6 JPAD, Spring 3.0 Core Professional.
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1529
    
  22

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

Joined: Aug 31, 2006
Posts: 340

It's now clear.
Thanks so much!
munjal upadhyay
Ranch Hand

Joined: Sep 18, 2010
Posts: 69

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
Bartender

Joined: Apr 07, 2010
Posts: 1529
    
  22

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

Joined: Sep 18, 2010
Posts: 69

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

Joined: Sep 18, 2010
Posts: 69

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
Bartender

Joined: Apr 07, 2010
Posts: 1529
    
  22

try this link and see if it helps:
Java EE5 Tutorial

Regards,
Frits
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet specific attribute
 
Similar Threads
Question about inheritance
init()
Can some one help me to understand answer to this question in the Head-first JSP and servlet book?
Deleting session deletes all my sessions
Difference between value attribute and binding attribute