aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes ContextListener Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "ContextListener Problem" Watch "ContextListener Problem" New topic
Author

ContextListener Problem

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

Model


Listener


Servlet



EmployeeListener


web.xml


HTML


console output:

counter=2name=org.apache.jasper.runtime.JspApplicationContextImplvalue=org.apache.jasper.runtime.JspApplicationContextImpl@149b290
Attriburte Removed
counter=1name=org.apache.jasper.runtime.JspApplicationContextImplvalue=org.apache.jasper.runtime.JspApplicationContextImpl@149b290

browser output:
employee_number is100

i think the output of console is wrong and it should appear:

counter=1name=employeelvalue=null
Attriburte Removed
counter=0name=employeevalue=employeeObject


please help

OCPJP 6.0 93%
OCPJWCD 5.0 98%
Stoian Azarov
Ranch Hand

Joined: Jun 01, 2011
Posts: 113
Can you give some more info about your application?
E.g.:
- 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.
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

I have updated the code Stoian Azarov .
It's my request to fellow ranchers to help me in solving the problem.
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1696
    
  25

Hi Mohitkumar,

i think the output of console is wrong and it should appear:
counter=1name=employeelvalue=null
Attriburte Removed
counter=0name=employeevalue=employeeObject

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
FirstServlet
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:
and
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.

Try this and tell us what you are seeing.

Regards,
Frits
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

COnsole output:
Name of attribute: employee

Name of attribute: org.apache.catalina.WELCOME_FILES

Name of attribute: javax.servlet.context.tempdir

Name of attribute: org.apache.catalina.jsp_classpath

Name of attribute: org.apache.jasper.runtime.JspApplicationContextImpl

Name of attribute: org.apache.catalina.resources

Name of attribute: org.apache.AnnotationProcessor

counter=2name=org.apache.jasper.runtime.JspApplicationContextImplvalue=org.apache.jasper.runtime.JspApplicationContextImpl@1a12495
Attriburte Removed
counter=1name=employeevalue=model.Employee@1a7508a


I am using tomcat 6.0
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1696
    
  25

Attriburte Removed
counter=1name=employeevalue=model.Employee@1a7508a

ok, this makes more sense, you are removing the Employee.

But I don't see counter=1 before counter=2 is coming, try this in EmployeeListener:
and this in ContextAttributeTracker:
what are you seeing now?

Regards,
Frits
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

i ran the program with the changes told on tomcat 5.5 server

Console output:

EmployeeListener before adding employee:
Name of attribute: org.apache.catalina.jsp_classpath

Name of attribute: org.apache.catalina.WELCOME_FILES

Name of attribute: javax.servlet.context.tempdir

Name of attribute: org.apache.catalina.resources

EmployeeListener setting attribute
ContextAttributeTracker before adding employee:
Name of attribute: org.apache.catalina.jsp_classpath

Name of attribute: org.apache.catalina.WELCOME_FILES

Name of attribute: javax.servlet.context.tempdir

Name of attribute: employee

Name of attribute: org.apache.catalina.resources

Attribute Added
counter=1name=employeevalue=model.Employee@1506dc4
EmployeeListener after adding employee:
Name of attribute: org.apache.catalina.jsp_classpath

Name of attribute: org.apache.catalina.WELCOME_FILES

Name of attribute: javax.servlet.context.tempdir

Name of attribute: employee

Name of attribute: org.apache.catalina.resources

Attriburte Removed
counter=0name=employeevalue=model.Employee@1506dc4

Questions:


1.what are these other attributes in the output ?
2.why is output different when run on tomcat 5 and tomcat 6 ?
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1696
    
  25

1.what are these other attributes in the output ?

Attributes added by the people that have made this servlet-container (Tomcat)
2.why is output different when run on tomcat 5 and tomcat 6 ?

Newer version of tomcat, so it is a different implementation and you have proven that it uses the ServletContext for another attribute

Regards,
Frits
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

thanks for the reply

How do I implement the same thing in tomcat server 6 ?
Stoian Azarov
Ranch Hand

Joined: Jun 01, 2011
Posts: 113
Servlet 3.0 Specification
3.3 Attributes
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
RequestDispatcher).
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1696
    
  25

I just tried the code on a tomcat 6.0.33-server and it works the same as the tomcat 5.5 implementation except for the extra
Name of attribute: org.apache.AnnotationProcessor.


I don't have this org.apache.jasper.runtime.JspApplicationContextImpl attribute in my ServletContext...

Regards,
Frits
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

i ran the code on tomcat 6.0 and got the following output:

Console:

EmployeeListener before adding employee:
Name of attribute: org.apache.catalina.WELCOME_FILES

Name of attribute: javax.servlet.context.tempdir

Name of attribute: org.apache.catalina.jsp_classpath

Name of attribute: org.apache.catalina.resources

Name of attribute: org.apache.AnnotationProcessor

EmployeeListener setting attribute
ContextAttributeTracker before adding employee:
Name of attribute: employee

Name of attribute: org.apache.catalina.WELCOME_FILES

Name of attribute: javax.servlet.context.tempdir

Name of attribute: org.apache.catalina.jsp_classpath

Name of attribute: org.apache.catalina.resources

Name of attribute: org.apache.AnnotationProcessor

counter=1name=employeevalue=model.Employee@f0c85e
EmployeeListener after adding employee:
Name of attribute: employee

Name of attribute: org.apache.catalina.WELCOME_FILES

Name of attribute: javax.servlet.context.tempdir

Name of attribute: org.apache.catalina.jsp_classpath

Name of attribute: org.apache.catalina.resources

Name of attribute: org.apache.AnnotationProcessor

Sep 22, 2011 11:22:05 PM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
Sep 22, 2011 11:22:05 PM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
Sep 22, 2011 11:22:05 PM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/30 config=null
Sep 22, 2011 11:22:05 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 338 ms
ContextAttributeTracker before adding employee:
Name of attribute: employee

Name of attribute: org.apache.catalina.WELCOME_FILES

Name of attribute: javax.servlet.context.tempdir

Name of attribute: org.apache.catalina.jsp_classpath

Name of attribute: org.apache.jasper.runtime.JspApplicationContextImpl

Name of attribute: org.apache.catalina.resources

Name of attribute: org.apache.AnnotationProcessor

counter=2name=org.apache.jasper.runtime.JspApplicationContextImplvalue=org.apache.jasper.runtime.JspApplicationContextImpl@19e8329
Attriburte Removed
counter=1name=employeevalue=model.Employee@f0c85e

why am i getting EmployeeListener setting attribute
ContextAttributeTracker before adding employee lines twice in the console output ?
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1696
    
  25

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...

Regards,
Frits
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: ContextListener Problem