• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting session scoped variable in TagHandler class

 
Jim So
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Michael Rios
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic