Why we use init() rather Constructor

前端 未结 4 1469
情歌与酒
情歌与酒 2021-01-20 05:00

why do we need init() rather than a constructor?

Please answer in reference of Servlet and Applet.
How does t

4条回答
  •  南方客
    南方客 (楼主)
    2021-01-20 05:54

    The init() method creates and loads the servlet. But the servlet instance is first created through the constructor (done by Servlet container). We cannot write constructors of a servlet class with arguments in servlet (It will throw Exception). So, They provided a init() method that accepts an ServletConfig object as an argument. ServletConfig object supplies a servlet with information about its initialization (init) parameters. Servlet class cannot declare a constructor with ServletConfig object as a argument and cannot access ServletConfig object.

    More info at: http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/Servlets6.html

提交回复
热议问题