Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Attributes Doubt??

 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

I'm not able to understand the following question from the HFSJ book!,

public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException{
req.setAttribute("a", "b");
req.setAttribute("a", "c");
req.setAttribute("a");
}

public void attributeAdded(ServletRequestAttributeEvent ev){
System.out.println("A:" + ev.getName() + "->" + ev.getValue());
}

public void attributeAdded(ServletRequestAttributeEvent ev){
System.out.println("M:" + ev.getName() + "->" + ev.getValue());
}

public void attributeAdded(ServletRequestAttributeEvent ev){
System.out.println("P:" + ev.getName() + "->" + ev.getValue());
}

How will this print A:a->b P:a->b M:a->c??

Please help me guys??

Thanks in advance!
 
Micheal John
Ranch Hand
Posts: 344
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException{
req.setAttribute("a", "b");
req.setAttribute("a", "c");
req.setAttribute("a");
}

public void attributeAdded(ServletRequestAttributeEvent ev){
System.out.println("A:" + ev.getName() + "->" + ev.getValue());
}

public void attributeAdded(ServletRequestAttributeEvent ev){
System.out.println("M:" + ev.getName() + "->" + ev.getValue());
}

public void attributeAdded(ServletRequestAttributeEvent ev){
System.out.println("P:" + ev.getName() + "->" + ev.getValue());
}

How will this print A:a->b P:a->b M:a->c??


If your are taking from HFSJ, then It should be like this:


NO surprise in the o/p:
First your adding the attribute so A:a->b
Then your replacing the value for the same attribute "a", so P:a->b
Finally removing..

I think the problem for you is with P:a->b instead of P:a->c..the getValue() method returns the old value of the attribute if the attribute was replaced
[ January 06, 2007: Message edited by: Micheal John ]
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael,

Thanks for the help, I was dubious about the way attributes work with respect to the above question. Could you tell me where I can find a suitable explanation for the question given on that page in the HFSJ book??
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!