Can servlet have a constructor ?

Servlet can have a constructor. But you never call the constructor for the servlet / instantiate them , because the container handles it.
So you are better of doing initialization / one-time setup code in the init method of the servlet. Also, the container passes the ServletConfig object to the servlet only when it calls the init method. So ServletConfig will not be accessible in the constructor.However, you might still need the init parameters, so initialization is done in init instead of a constructor.


3 Responses

  1. very useful

  2. Thanks

  3. bhul bhal

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: