java - 服务器启动时监听器类未执行 || Tomcat || eclipse

标签 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 服务器时,我的列表器类没有被执行。甚至我已经输入了一些 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");

    }

}

最佳答案

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

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

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

应该映射到

WEB-INF/classes/com/company/listeners

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

关于java - 服务器启动时监听器类未执行 || Tomcat || eclipse ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7776040/

相关文章:

tomcat - 通过本地数据源领域的 Servlet3 登录在 Tomcat 7.0.29 中有效,但在 7.0.54 及更高版本中失败

apache - PowerShell - 过去 24 小时内上次启动 Tomcat 的时间

java - <%@ taglib 前缀 ="c"uri ="http://java.sun.com/jsp/jSTL/core"%> Eclipse photon jdk 10 Tomcat 9

java - hashmap、java8、null/empty 检查

java - 打印树的最小和路径

eclipse - 不同类型的eclipse java补全建议

java - 我可以将 JMS 与常规(即非 Java EE)Java 应用程序一起使用吗?

java - 为什么 gradlew build 命令不能仅编译和构建已更改的内容并使过程更快?

java - 不稳定的使用会降低性能吗

java - 强制 Spring 使用同一配置类中的 bean