Solved

HttpServlet and ServletRequestAttributeListener explantion

Posted on 2015-02-01
3
39 Views
Last Modified: 2015-02-11
This is from the book I am reading:

Given this code from an otherwise valid HttpServlet that has also been
registered as a ServletRequestAttributeListener:

public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
         req.setAttribute(“a”, “b”);
         req.setAttribute(“a”, “c”);
          req.removeAttribute(“a”);
}
        public void attributeAdded(ServletRequestAttributeEvent ev) {
        System.out.print(“ A:” + ev.getName() + “->” + ev.getValue());
}
       public void attributeRemoved(ServletRequestAttributeEvent ev) {
       System.out.print(“ M:” + ev.getName() + “->” + ev.getValue());
}
       public void attributeReplaced(ServletRequestAttributeEvent ev) {
       System.out.print(“ P:” + ev.getName() + “->” + ev.getValue());
}

Open in new window


What logging output is generated?

And answer is:
C. A:a->b P:a->b M:a->c

And explanation from book is:
Tricky! The getValue method returns the OLD value of the attribute if the attribute was replaced.

My question is how this could be?
Particularly this part of sequence is not clear to me: P:a->b
Why would it be once again P:a->b instead of P:a->c ?
0
Comment
Question by:KPax
  • 2
3 Comments
 
LVL 26

Accepted Solution

by:
dpearson earned 500 total points
ID: 40583362
Just to clarify this output, can we call them "Added", "Replaced" and "Removed" so we're looking at this:

Added:a->b
Replaced:a->b
Removed:a->c

Now the question is why does Replaced return "b" for the value instead of "c"?

The simple answer is because that's what the docs say it should do:
http://docs.oracle.com/javaee/7/api/javax/servlet/ServletRequestAttributeListener.html

void attributeReplaced(ServletRequestAttributeEvent srae)
Receives notification that an attribute has been replaced on the ServletRequest.
Parameters:
srae - the ServletRequestAttributeEvent containing the ServletRequest and the name and (old) value of the attribute that was replaced


Now maybe the more interesting question is why are they doing this?

Well usually APIs like this are designed to pass you the old value, because you always have the option to ask for the current value in the callback.  So if they pass you the old value - you have more information available to you than if they just passed in the current value.  (There's no way to ask "what value did this attribute use to have?" after it's gone).

So with this API design you could write a listener than took some action whenever the "a:b" was removed - either by an explicit remove call or by replacing it with another value.  If they only passed in the new value, you couldn't write that listener (without storing the values that were added yourself).

Hope that helps make it clearer why it's this way.

Doug
0
 

Assisted Solution

by:KPax
KPax earned 0 total points
ID: 40593317
I found this explanation as well

The getName() method returns the String name of the attribute that triggered the event. The getValue() method returns the object value of the attribute that triggered the event. Watch out! It returns the old value, not the new one. In other words, it returns the value the attribute had BEFORE the change that triggered the event!
0
 

Author Closing Comment

by:KPax
ID: 40602721
I found this solution in a book  "Servlets and JSP, 2 ed"
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Non-recursive backtracking, using a stack 1 103
advertisement module in core php 4 147
get weblogic logged in user in java 2 58
web services creation SOAP vs REST 5 20
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now