简体   繁体   中英

How to run some code when java servlet is deployed on Tomcat server?

I have simple Servlet:

@WebServlet(urlPatterns = { "/doCourseRegister" })
public class DoCourseRegisterServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
.....
}

I need to run some code when servlet is first created(or deployed) on Tomcat server. How to do it?

Should I use servlet constructor?

You can add a listener in web.xml.

<listener>
    <listener-class>Your class</listener-class>
</listener>

Your class should implement HttpSessionListener .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM