【问题标题】:Base minimum WAR configuration基本最低 WAR 配置
【发布时间】:2012-06-06 15:58:02
【问题描述】:

我有一个突然需要重新打包为 WAR 的 JAR(不要问为什么)。它将部署在 Tomcat 上。我想知道最低限度的设置是什么(就web.xml 和任何其他conf 文件而言)以指示Tomcat 部署应用程序并运行通常由JAR 的main(String[]) 方法调用的方法:

public class AppDriver {
    public static void main(String[] args) {
        AppDriver app = new AppDriver();
        app.run();
    }

    // This is the method I need ran by Tomcat at deploy-time.
    public void run() {
        // ...
    }
}

由于我没有使用 Spring 或任何其他类型的 MVC 框架,并且由于没有 servlet 或需要 CDI/上下文,我想我的 web.xml 可以非常简单;但令人惊讶的是,我找不到任何解释我需要什么的东西。

我还想我需要以某种方式注释run() 方法,或者在web.xml 中引用它。但这就是我卡住的地方。提前致谢。

【问题讨论】:

  • 您使用的是哪个版本的 Servlet 规范?对于其中一些,您不需要 web.xml(它们都可以是基于注释的)。

标签: java tomcat servlets war web.xml


【解决方案1】:

您可以实现ServletContextListener,在这种情况下,当tomcat部署您的应用程序并启动它时,您的contextInialized方法将被调用,在该方法中您调用您的run方法。

然后在 web.xml 中,您将在 listener 标签下为您的课程添加一个条目。

以上方法适用于 2.5 及以下的 servlet 规范

如果您使用的是 Servlet Spec 3.0,您只需在您的类中添加 @ServletContextListener 注释,无需在 web.xml 中进行任何配置,当您的应用程序启动时,它将被您的容器调用。

【讨论】:

  • 因此,如果我只是将AppDriver 列为侦听器,让它实现ServletContextListenerweb.xml 并定义我自己的无参数构造函数,那么Tomcat 会知道调用我的自定义no-参数?
  • 是的,你的监听器默认构造函数会被tomcat调用。
【解决方案2】:

如果您想与您的 web 应用程序进行外部通信,您将需要一个 WSDL。除此之外,这里是一个示例 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app
  xmlns="http://java.sun.com/xml/ns/j2ee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
  http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
  version="2.4">
  <display-name>SemanticAdaptationCoreService</display-name>
  <welcome-file-list>
    <!-- You can skip this part -->
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
  <!-- context-params allow you to pass parameters to your app at runtime -->
  <context-param>
    <param-name>param1</param-name>
    <param-value>value1</param-value>
  </context-param>

  <!-- You can provide a listener class to handle WSDL requests -->
  <listener>
    <listener-class>your.listener.AClass</listener-class>
  </listener>

  <!-- the initializer servlet allows you to run custom methods when the webapp is loaded -->
  <servlet>
    <servlet-name>initializer</servlet-name>
    <servlet-class>your.Initializer</servlet-class>
    <!-- a 1 value requests running the class at load time -->
    <load-on-startup>1</load-on-startup>
  </servlet>
</web-app>

确保您的类扩展 HttpServlet 并覆盖将在加载时调用的 public void init() 方法。

【讨论】:

    【解决方案3】:

    修改您的AppDriver 类,如Daemon thread 并从ServletContextListener.contextInitialized() 方法启动线程。

    @WebListener
    //Use the above annotation if it is Servlet 3.0
    AppDriverInitializer implements ServletContextListener
    {
    
       public void contextInitialized(ServletContext ctx)
       { 
          Thread appDriverThread = new Thread() {
             public void run() {
             //Have your AppDriver class run() method implemented here...
             }
          }
          appDriverThread.start();
       }
    }
    

    在你的 WAR 中定义 ServletContextListener 监听器,两种方式中的任一种

    • 在你的 web.xml 中定义监听器

      AppDriverInitializer

    • 如果您使用 Servlet 3.0 容器,请使用 @WebListener 注释

    【讨论】:

      猜你喜欢
      • 2018-10-22
      • 2010-09-13
      • 1970-01-01
      • 2018-02-27
      • 1970-01-01
      • 2014-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多