简体   繁体   English

是否必须重写servlet init()或init(ServletConfig配置)和destroy()方法

[英]Is it mandatory to override servlet init() or init(ServletConfig config) and destroy() methods

您能否告诉我,在我们编写的新servlet(ex:HelloServlet)中重写init()或init(ServletConfig配置)和destroy()方法是强制性的吗?

These two methods are a part of the javax.servlet.Servlet interface; 这两个方法是javax.servlet.Servlet接口的一部分。 so, they must somehow be implemented, either by your servlet or by a base class of your servlet. 因此,必须以某种方式由您的servlet或servlet的基类实现它们。

Usually, you'd write a servlet that does not directly implement javax.servlet.Servlet , but extend the javax.servlet.GenericServlet class or the javax.servlet.http.HttpServlet class. 通常,您会编写一个不直接实现javax.servlet.Servlet的servlet,而是扩展javax.servlet.GenericServlet类或javax.servlet.http.HttpServlet类。 If you extend either of these, then you don't have to override init or destroy - as long as the superclass' implementation is OK with you. 如果您扩展了其中任何一个,那么您就不必重写initdestroy只要超类的实现对您来说就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java Servlet重写init(ServletConfig配置) - Java Servlets Overriding init(ServletConfig config) 我们应该从 Servlet 的 init() 和 service() 方法中调用 destroy() 方法吗? - Shall we call the destroy() method from the init() and service() methods in Servlet? 如果没有初始化参数,Servlet容器会创建ServletConfig对象吗? - Does Servlet Container create ServletConfig object if there are no init-params? Servlet初始化方法必须重写 - Servlet init method override necessary 将init params添加到init中的列表(ServletConfig) - adding init params to list in init(ServletConfig) 即使使用init(ServletConfig con)在我的servlet程序中也找不到main()方法 - main() method not found in my servlet program even upon using init(ServletConfig con) 使用service方法调用servlet的init和destroy方法 - Calling servlet's init and destroy method withing service method 如果我在HttpServlet #init(ServletConfig)中分配实例字段,Servlet规范是否保证我可以在doGet()中读取它们? - If I assign instance fields in HttpServlet#init(ServletConfig), does the Servlet spec guarantee I can read them in doGet()? 建议在servlet的init()和destroy()中使用get / close数据库连接吗? - Is the use get/close DB connection in init() and destroy() of a servlet recommended? Tomcat:如何覆盖web.xml servlet init-param - Tomcat: How to override web.xml servlet init-param
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM