监听器类在服务器启动时不执行|| tomcat ||日食

时间:2011-10-15 05:39:10

标签: java eclipse tomcat

我正在研究eclipse gallileo,struts和tomcat。 以下是我的web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>streetmall</display-name>
 <context-param>
  <param-name>imagePath</param-name>
  <param-value>D:\\OrderID_images</param-value>
 </context-param>
 <listener>
  <listener-class>jaha.Customer.util.ApplicationScopeInit</listener-class>
 </listener>
 <servlet>
  <servlet-name>action</servlet-name>
  <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
  <init-param>
   <param-name>config</param-name>
   <param-value>/WEB-INF/struts-config.xml</param-value>
  </init-param>
  <init-param>
   <param-name>debug</param-name>
   <param-value>3</param-value>
  </init-param>
  <init-param>
   <param-name>detail</param-name>
   <param-value>3</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet>
  <servlet-name>action_tmp</servlet-name>
  <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
  <init-param>
   <param-name>config</param-name>
   <param-value>/WEB-INF/struts-config.xml</param-value>
  </init-param>
  <init-param>
   <param-name>debug</param-name>
   <param-value>3</param-value>
  </init-param>
  <init-param>
   <param-name>detail</param-name>
   <param-value>3</param-value>
  </init-param>
  <load-on-startup>0</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>action</servlet-name>
  <url-pattern>*.do</url-pattern>
 </servlet-mapping>
 <welcome-file-list>
  <welcome-file>welcome.jsp</welcome-file>
 </welcome-file-list>

 <login-config>
  <auth-method>BASIC</auth-method>
 </login-config>

  <description>MySQL Test App</description>
  <resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/TestDB</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref></web-app>

我的问题是当我启动我的tomcat服务器时,我的lister类没有被执行。甚至我已经推出了一些System.out.println()。这些消息也没有出现在控制台上..请帮我解决这个问题。

早些时候我发现了课程未找到异常。但后来我创建了新项目并进行部署。现在我遇到了这个问题 感谢

public class ApplicationScopeInit implements ServletContextListener 
{

    public void contextInitialized(ServletContextEvent event) 
    {
        try
        {
            ClassLoader loader = Thread.currentThread().getContextClassLoader();

            System.out.println("Use context classloader to read states.properties");
            InputStream iStream = loader.getResourceAsStream("jaha/Customer/states.properties");
            Properties props = new Properties();
            System.out.println("Load the stream into the properties object directly");
            props.load(iStream);

            //Look up by key and load them into a ArrayList as NameValuePair collection
            Enumeration keyEnum = props.propertyNames();
            // Use a Sorted Set to hold the state names and values
            // Define an anonymous inner class to provide
            // the comparison algorithm to the TreeSet
            @SuppressWarnings("unchecked")
            Set stateSet = new TreeSet(
                    new Comparator() 
                    {
                        public int compare(Object a, Object b)
                        {
                            LabelValueBean nvpA = (LabelValueBean) a;
                            LabelValueBean nvpB = (LabelValueBean) b;

                            String valA = nvpA.getLabel();
                            String valB = nvpB.getLabel();
                            return valA.compareTo(valB);
                        }
                    }
            );

            LabelValueBean nvp = null;
            String keyName = null;
            String label = null;
            while (keyEnum.hasMoreElements())
            {
                keyName = (String) keyEnum.nextElement();
                label = props.getProperty(keyName);
                nvp = new LabelValueBean(label, keyName);
                stateSet.add(nvp);
            }

            System.out.println("Get ServletContext and set the properties as a application scope object");
            ServletContext context = event.getServletContext();
            context.setAttribute("STRUTS_EXAMPLE_STATES", stateSet);


            //Load Carriers - FedEx, UPS etc..
            List carrierList = new ArrayList();
            carrierList.add(new LabelValueBean("UPS", "UPS"));
            carrierList.add(new LabelValueBean("USPS", "USP"));
            carrierList.add(new LabelValueBean("FedEx", "FDX"));

            context.setAttribute("STRUTS_EXAMPLE_CARRIERS",carrierList);
            //.setAttribute("STRUTS_EXAMPLE_CARRIERS",carrierList);
        }
        catch (IOException ioe)
        {
            ioe.printStackTrace();      
        }
        try{
            System.out.println("Populating category on server startup");
            HashMap<String, ArrayList<CategorydetailObject>>  hm= CategoryManager.PopulateCategory();
            ServletContext context = event.getServletContext();

            context.setAttribute("PRODUCTS", hm);



        }
        catch(Exception e)
        {
            System.out.println("$$$$$$$$$$$$$$$$$$$$$ PROBLEM IN INITIALIZATION ALL PARAMETERS");

        }
    }

    public void contextDestroyed(ServletContextEvent event) 
    {
        event.getServletContext().removeAttribute("STRUTS_EXAMPLE_STATES");
        event.getServletContext().removeAttribute("STRUTS_EXAMPLE_CARRIERS");
        event.getServletContext().removeAttribute("PRODUCTS");

    }

}

1 个答案:

答案 0 :(得分:0)

右键单击maven Web项目,然后在部署程序集下添加文件夹,其中包含目标文件夹中的侦听器类

例如,如果包 com.company.listeners 中存在ServletContextListener实现,则部署程序集应具有以下文件夹映射:

target/<applicationname>/WEB-INF/classes/com/company/listeners

应该映射到

WEB-INF/classes/com/company/listeners

我认为这与tomcat中的类加载器层次结构和用于加载侦听器的类加载器(以及可用于该类加载器的类)有关。