main()方法在JSP / Servlet应用程序中的位置是什么? [英] Where is the main() method In a JSP/Servlet application?

查看:124
本文介绍了main()方法在JSP / Servlet应用程序中的位置是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问这个的原因是我想编写一个代码,一旦它启动就初始化应用程序并在以后清理。

The reason I'm asking this is that I want to write code that initializes the application once it starts and cleans up later on.

我不想使用servlet init()方法,因为它是每个servlet。

I dont want to use a servlet init() method since it is per servlet.

推荐答案

Servlet中没有 main()方法。

There is no main() method in Servlet.

如果


我之所以这样说是因为我想编写代码,一旦启动它就会初始化应用程序并在以后清理它。

The reason I'm asking this is that I want to write code that initializes the application once it starts and cleans up later on.

您可以使用 ServletContextListener 已实现

You can use ServletContextListener implemented

public class MyServletContext implements ServletContextListener{
    ServletContext context;
    public void contextInitialized(ServletContextEvent contextEvent) {
        System.out.println("Context Created");

    }
    public void contextDestroyed(ServletContextEvent contextEvent) {

        System.out.println("Context Destroyed");
    }
}

web.xml

<listener>
    <listener-class>
        com.yourpackage.MyServletContext
    </listener-class>
  </listener>

这篇关于main()方法在JSP / Servlet应用程序中的位置是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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