服务器启动时侦听器类不执行 ||雄猫 ||蚀

发布于 2024-12-10 00:28:30 字数 6435 浏览 1 评论 0原文

我正在研究 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 服务器时,我的列表器类没有被执行。甚至我也放了一些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");

    }

}

I am working on eclipse gallileo,struts and tomcat.
following is my web.xml file

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

My Problem is when I am startig my tomcat server, my lister class is not getting executed. even I have putted some System.out.println(). those messages are also not coming on console.. please help me how to get rid off this.

earlier i was getting class not found exception. but then I created new project and deploy it. now I am in this problem
Thanks

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");

    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

¢蛋碎的人ぎ生 2024-12-17 00:28:30

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

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

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

应映射到

WEB-INF/classes/com/company/listeners

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

Right click on the maven Web project then under deployment assembly add folder with the listener classes in target folder.

For example if ServletContextListener implementation is present in the package com.company.listeners, deployment assembly should have the following folder mapping:

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

Should be mapped to

WEB-INF/classes/com/company/listeners

I think this is related to class loader hierarchy in tomcat and the class loader used for loading listeners (and classes available for that class loader).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文