Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A doubt about a listener

 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi john

Where you are setting Initparameter bull
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi john

while adding the entry in listener class add the class name along with package structure
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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>
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic