Can you give some more info about your application?
- DD servlet mappings from web.xml
- DD listener declaration from web.xml
- write down the steps that led to that output
I suppose that Employee request is mapped to First servlet so doGet() method is called. Inside this method you try to get ServletContext attribute called employee but I don't see any place where you are setting this attribute, so I can't understand why this code don't throw an exception but I suppose that some other code is setting that parameter before calling FirstServlet.
i think the output of console is wrong and it should appear:
Well your expectation is almost like my output
This is what I got on my console (running on a Tomcat 5.5 server):
counter= 1 name= employee value= model.Employee@b3cac9 // because of the setAttribute() in EmployeeListener
Attriburte Removed // because of submit from html
counter= 0 name= employee value= model.Employee@b3cac9 // because of removeAttribute in FirstServlet
Make some changes in ContextAttributeTracker:
What I don't get is why your counter starts at 2, have a look in your console what is written on the counter=1 line
As you will probably see is that there are more attributes in the ServletContext than you added. This is done by your Tomcat Server.
How do I implement the same thing in tomcat server 6 ?
Joined: Jun 01, 2011
Servlet 3.0 Specification
Attributes are objects associated with a request. Attributes may be set by the
container to express information that otherwise could not be expressed via the API,
or may be set by a servlet to communicate information to another servlet (via the
Creator of Enthuware JWS+ V6
ContextAttributeTracker before adding employee lines twice in the console output ?
The second time you see the ContextAttributeTracker is because of the JspApplicationContextImplvalue that is added to the ServletContext, obviously not added by you but by the container. Funny, but I don't see that happening
Are you sure that you don't have any other Servlets or JSP's in your web-app? Try creating a new workspace, new Dynamic Web Project and copy the files to the new environment. See if that helps, because I don't see it happening with the exact same code and Tomcat version...