tomcat自动启动servlet [英] tomcat auto start servlet

查看:26
本文介绍了tomcat自动启动servlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个标准的 GWT 应用程序,它当然在后端使用 Java servlet.该 servlet 部署在 Tomcat 和 Windows Server 上.

I have a standard GWT application, and it of course uses a Java servlet on the backend. This servlet is deployed on Tomcat and Windows Server.

我知道这违反了规则/建议,但是我在这个 servlet 中有一个线程,它在 servlet 初始化时启动(servlet 的init"方法).线程是一种调度程序,其目的是在特定时间执行不同的数据库任务,完全独立于 GWT 应用程序/接口本身.

I know it's against the rules / suggestions, but I have one thread in this servlet that get's started when the servlet initializes (the "init" method of the servlet). The thread is a scheduler of sorts, its purpose is to perform different database tasks at certain times, totally independent of the GWT application / interface itself.

我需要的是在部署战争后立即调用 servlet 的init"方法.现在我一直在做的是,每次对应用程序进行升级时,我都会将 war 放到正确的目录中,然后我必须登录"到应用程序 GWT 应用程序,以便调用它的init"方法.我希望在战争更新后立即调用 servlet 的 init 方法,这样我就不必登录 GWT 应用程序来执行此操作.

What I need is for the servlet's "init" method to be called as soon as the war is deployed. Right now what I've been doing is, everytime there is an upgrade to the application, I drop the war into the right directory, then I have to "login" to the application GWT application so that its "init" method is called. I would like for the servlet's init method to be called as soon as the war is updated so that I don't have to login to the GWT application to do this.

有什么想法吗?

推荐答案

你可以使用 servlet 上下文侦听器.更具体地说,您可以在 contextInitialized 方法中启动您的线程:

you could use the servlet context listener. More specifically you could start your thread in the contextInitialized method:

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

public class MyListener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent sce) {
         // start the thread
    }

    public void contextDestroyed(ServletContextEvent sce) {
         // stop the thread
    }
}

然后添加:

<listener>
    <description>ServletContextListener</description>
    <listener-class>MyListener</listener-class>
</listener>

在你的 web.xml 中

in you web.xml

这篇关于tomcat自动启动servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆