jsp - Is it necessary to use init() in every servlet I use in my web application? -
i have jsp/servlet web application jsp pages , servlets. have read following questions:
- servlet constructor , init() method
- using special auto start servlet initialize on startup , share application data
they helpful, have new question: need initialize every servlet use? or have init()
first serlvet called in web application?
because servlet container controls initialization of servlet, don't have choice use init()
method initialize instance fields servlet might need. example if servlet depended on service class interact resource, following
public class myservlet extends httpservlet { private myservice myservice; public void init() { myservice = new myservice(); myservice.setsomeproperty("propertyvalue"); } ... }
this way can initialize fields. if need it, can use init(servletconfig)
method instead or call getservletconfig()
access servletcontext
may contain attributes added either other servlet init()
or servletcontextlistener
s. note can set in order servlets initialized.
Comments
Post a Comment