This program explains you how to pass initialization parameters from web.xml and capture it to servlet.
Iam taking two files to write this program:
1) web.xml : Here we will pass init-params information.
2) InitParamsServlet : In this java class we will capture params information. Continue reading
Servlets follow three phase life cycle initialization,service and destruction.
1) init() method :
1) init() method : init method is called when the servlet is first created.
2) init() method is called exactly once after the instantiating Servlet.
3) init() method is used to write initialization code such as loading database drivers and initialization values so on.
4) When invoking this method servlet container passes a Servlet-Config.
5) One of the most important and common task that init() method performs is reading server specific initialization parameters.
6) The init method is important because the servlet container passes a ServletConfig object, which contains the configuration values given in the web.xml file.
Signature : public void init(ServletConfig servletConfig) throws ServletException
init() method can throw servlet exception.When init() method throws Servlet exception servlet container does not executes service() method.
servletException is also thrown when the method does not return with in the time period.
Advantages of Servlets over CGI
|1) In traditional CGI, a new process is started for each HTTP request.
||1) In servlets, the Java virtual machine stays running and handles each request with a lightweight Java thread, not a heavyweight operating system process. Continue reading