Why can't we initialize the servlet using the constructor itself?

Why do we need to override the init () method in Servlets, while we can initialize in the constructor and have a web container that calls the constructor, passing the ServletConfig link to the servlet when the constructor is called?

The exchange container must use reflection for this, but the container must use reflection in any case to invoke a simple no-arg constructor

+3
source share
4 answers

, "" API Servlet, . , Java , destroy, init API .

/ , .

+14

, . , . . Servlet.

, http://oreilly.com/catalog/jservlet/chapter/ch03.html :

init() - , . ? , JDK 1.0 ( ) Java (, ) .

, , init() , ServletConfig.

, Java . , javax.servlet.Servlet , ServletConfig. , init(). , , ServletConfig ServletException.

+5

, ServletConfig . ServletConfig init(). , .

+2

, init() Servlet - : Java, , , Servlet -, Servlet ServletConfig , , ServletContext, .

- , javax.servlet, HttpServlet - , , Java, , Java. , . , Servlet , Java-, . -, Tomcat Jetty, Java Reflection Servlet, . , , - , . , Java , . Servlet.

+1
source

Source: https://habr.com/ru/post/1747346/


All Articles