servlet listener classes

Is it possible to pass in parameters to a Listener class in the web.xml simular to as you do with filters and servlets?
inzaghiAsked:
Who is Participating?
 
bloodredsunConnect With a Mentor Commented:
here's the full example for the ServletContextListener picking up a web.xml parameter:

---------------
package com.bloodredsun.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;


public class ServContextListener implements ServletContextListener {

      private String name = "" ;

      public void contextInitialized( ServletContextEvent sce ) {
            System.err.println("The webapp '" + sce.getServletContext().getServletContextName() + "' has been started");
            ServletContext sc = sce.getServletContext() ;
            String dbDriverName = sc.getInitParameter("dbDriverName");
            System.err.println("initparam:" + dbDriverName) ;
      }

      public void contextDestroyed( ServletContextEvent sce ) {
            System.err.println("The webapp '" + name + "' (instance variable) was destroyed");
            
      }
}
0
 
bloodredsunCommented:
Yes, for ServletContextListener:

      public void contextInitialized( ServletContextEvent sce ) {
            ServletContext sc = sce.getServletContext() ;
                String dbDriverName = sc.getInitParameter("dbDriverName");
      }
0
 
bloodredsunCommented:
You can't directly with HttpSessionListener as you do not have access to the servlet context.
0
All Courses

From novice to tech pro — start learning today.