This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

servlet  RSS feed

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

Public void doget(HttpServletRequest req,HttpservletResponse res)
{
req.setAttribute("a","b");
req.setAttribute("a","c");
req.removeAttirbute("a");
}
public void attributeAdded(ServletRequestAttributeEvent ev)
{
System.out.pritnln("A:" + ev.getName() +"->"+ev.getValue());
}
public void attributeRemoved(ServletRequestAttributeEvent ev)
{
System.out.pritnln("M:" + ev.getName() +"->"+ev.getValue());
}
public void attributeReplaced(ServletRequestAttributeEvent ev)
{
System.out.pritnln("P:" + ev.getName() +"->"+ev.getValue());
}


The output of the above code is
A:a->b P:a->b M:a->c


I have question on this getValue method.
Why this getValue method returns the OLD value of the attribute if the attribute was replaced?
 
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jaffrin,
Welcome to JavaRanch!

In an effort to help you get the most from our forums, we've compiled a
list of tips for asking questions here. You can find the list in our
FAQ section here. In particular, please see UseAMeaningfulSubjectLine
.


That you posted in a 'Servlets' forum is enough for us to know that your question is about servlets. The subject line "servlet" gives us no hint as to the nature of your question.

Again, welcome to JavaRanch and good luck with your question.
-Ben
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the API:
"Returns the value of the attribute that has been added removed or replaced. If the attribute was added, this is the value of the attribute. If the attribute was removed, this is the value of the removed attribute. If the attribute was replaced, this is the old value of the attribute."

My feeling is that since getValue() returns the most recent attribute value, when an attribute is added or removed, I suspect that the API developers decided that if an event notification was going to be generated when the value is replaced it might be wise to be able to get the value of the
attribute before it was replaced.

Note that if you need to get the new value of the request attribute, ServletRequestAttributeEvent inherits a getServletRequest() method which can then be used to get at the attribute's current (NEW) value.

Hope this helps!
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!