Two Laptop Bag*
The moose likes Servlets and the fly likes A doubt about a listener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "A doubt about a listener" Watch "A doubt about a listener" New topic
Author

A doubt about a listener

Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

I have a listener class that implemets servletContextListener. The contextInitialized method sets an attribute and the listener is configured properly in my web.xml file. My doubt is... if i use a System.out.println statement in the java source will the output appear on the web container's console ? Here are the files

web.xml:

<listener>
<listener-class>myListener</listener-class>
</listener>

This is placed properly inside web-app tag.


myListener.java

import javax.servlet.*;

class myListener implements ServletContextListener
{
public void contextInitialized ( ServletContextEvent event )
{
System.out.println("Initializing Context...");
ServletContext sc = event.getServletContext();
sc.setAttribute( "data" , sc.getInitParameter("bull") );
}

public void contextDestroyed( ServletContextEvent event )
{}
}




When i try to get the attribute i get a null value so i thought may be the listener is not running as yet. I cannot find the String Initializing Context... anywhere on my console. Does this mean the Listener is not running at all ? Why is it not detecting when my context is initialized ? Any ideas ?


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
hi john

Where you are setting Initparameter bull
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

Ahh yes i forgot to mention...

<context-param>
<param-name>bull</param-name>
<param-value>dozer</param-value>
</context-param>

Its directly under the web-app tag like it should be. I have verified that i can get the context param from a servlet by printing it out on the console, from another servlet. However it seems to me that the context listener is not running when my app context is loaded. i wonder why
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
hi john

while adding the entry in listener class add the class name along with package structure
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

The listener is in my classes directory directly under WEB-INF. That is where my servlet is too. The container is able to detect the servlet but not my listener I think i ll change the container's vendor and see what happens.
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

The listener is in my classes directory directly under WEB-INF.


Thats NOT sufficient.
You got specify package directory structure. Some thing like



<listener>
<listener-class>com.example.myListener</listener-class>
</listener>


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by John Meyers:
... if i use a System.out.println statement in the java source will the output appear on the web container's console ?


Where the println statements go depends on your server and how it's configured. Which are you using?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

Hey i solved it. I ran it under tomcat and it worked. I forgot to give the listener class the access modifier public and i guess the weblogic server failed very quietly. Thanks for the help ! I also put my listener in a package.
 
Don't get me started about those stupid light bulbs.
 
subject: A doubt about a listener