aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Getting session scoped variable in TagHandler class 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 "Getting session scoped variable in TagHandler class" Watch "Getting session scoped variable in TagHandler class" New topic
Author

Getting session scoped variable in TagHandler class

Jim So
Greenhorn

Joined: Mar 18, 2009
Posts: 28
GOt a question:

public class TestTagHandler extends TagSupport
{
public int doStartTag()
{
//insert code here
//other valid code
}
//other valid code
}

Which of the following statements when inserted in the above code independent of each other will get the value of a session scoped variable 'cartid'?




Select 2 correct options
a String cartid = (String) pageScope.getAttribute("cartid");

b String cartid = (String) session.getAttribute("cartid");


c String cartid = (String) pageContext.findAttribute("cartid");

d String cartid = (String) pageScope.getAttribute("cartid", "session");

e String cartid = (String) pagecontext.getAttribute("cartid", PageContext.SESSION_SCOPE);


Correct answers are c and e.

But whats wrong with b? Is it because you have access to the session object here?
If so what do you have access to? e.g. request, response, pageContext...

THanks in advance for your help.

Kamal Tripathi
Ranch Hand

Joined: Oct 02, 2008
Posts: 86
Jim So wrote:
But whats wrong with b? Is it because you have access to the session object here?
If so what do you have access to? e.g. request, response, pageContext...



In Classic handler you have access to only pageContext implicit object. That too because you might be remembering that in lifecycle of class tag setPageContext() method runs where pageContext is set in the page. So you can access only pageContext similar to how you used it in jsp page. NO OHTER implicit objects are accessible.

So to get session use pageContext.getSession(). Hope that helps

Kamal Tripathi
SCJP 1.4 90%, SCWCD5 94%, Next SCDJWS--> In Naescent stage. Researching abt exam and material itself.
Michael Rios
Greenhorn

Joined: Jul 05, 2006
Posts: 8

pageContext object is the only implicit object in Classic Tag handler. The only way to access session scoped variable is through pageContext object.

pageContext.getAttribute("cartid", PageContext.SESSION_SCOPE);
pageContext.getSession().getAttribute("cartid");

i think the question was lacking of something like "there is no other attribute named cartid in all other scopes." if this is included in the question i would say option C is definitely correct, but you have to choose two correct answers so option C is definitely the best choice compared to options A, B, D.

Im also having problems in questions like this. Can anybody help on this one. If "Select 2 correct options" is not stated then option C is definitely Confusing, isnt it?


SCJP 1.4, SCWCD 5
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting session scoped variable in TagHandler class
 
Similar Threads
HTML Event that gets a value from JSP Session
I know local objects are thread safe. But why not obj here too?
Toughest Questions for Oracle JSP Servlet (SCWCD 5.0) Doubts
Is realy Enthuware commercial SCWCD 1.4 test studio such a ... crap??