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:

  1. servlet constructor , init() method
  2. 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 servletcontextlisteners. note can set in order servlets initialized.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -