Загрузка...

71 Understanding flow of execution related to both init method Scenario 3 | Adv java Servlet Tutoria

#adv java #Advance #Java #Servlet #Programming #Tutorial #Understanding #flow of #execution #related to #both #init #method:
- The javax.servlet.GenericServlet class of servlet API contains both init(ServletConfig sc) method , init() method.

Scenario 3:
----------
//GenericServlet.java (Pre-defined java class)
public abstract class GenericServlet implements Servlet
{
ServletConfig config;
public void init(ServletConfig cg)throws ServletException{
//(6)
config=cg; //initialization logic of ServletConfig object
----
init();//
}

public void init()throws ServletException{
//null body method
(7)
}

public ServletConfig getServletConfig(){
----
return config;
}
------
-------//other methods of GenericServlet class
------
}

//Scenario3.java
(1)(2)(3)(4)
//public class Scenario3 extends GenericServlet
//or
public class Scenario3 extends HttpServlet{

public void init(ServletConfig cg)throws ServletException{
//(5)
super.init(cg); //calling super class init(-)

--------//(8)
--------//our servlet program related initialization logic
}

public void service(ServletRequest req,ServletResponse res)throwse ServletException, IOException{
ServletConfig cg=getServletConfig();
---------
//(9)
--------//our servlet program related service logic
}
}

Notes:
1)End user gives first request to our Servlet Program (TestSrv.java) (let us assume no load-on-starupup is enabled on this servlet program)

2) Servlet Container creates our servlet class object using 0-param constructor.

3) Servlet container creates servletConfig object as right hand object for our servlet class object and also it raises instantiation event.

4) To process instantiation event servlet container calls init(servletConfin) life cycle method on our servlet class object having servletconfig object as argument value, Since this init(servletConfig) method is not available in our servlet(TestSrv.java) class, the super class(i.e GenericServlet or HttpServlet) init(ServletConfig ) method will be executed.

5) This super (i.e GenericServlet or HttpServlet) class init(sc) method contains the logic to initialize the ServletConfig Object and also calls init() (with no parameter) method at the end.

6) init() method of our servlet (TestSrv.java) class executes and this completes the initialization process or our servlet program.

7) Servlet container raises request arrival event and it calls public service(-,-) method on our servlet (TestSrv.java) class object. This method execution will process the request and sends the response to browser window.
Notes :
In Scenario 3 Programmer can use getServletConfig() to get access to ServletConfig object to its servlet program.
For this we must call super.init(sconfig) method explicitely without forget/ignorance
Let us see this scenario 3 implementation practically : 24Scenario3

Видео 71 Understanding flow of execution related to both init method Scenario 3 | Adv java Servlet Tutoria канала tech fort
Яндекс.Метрика
Все заметки Новая заметка Страницу в заметки
Страницу в закладки Мои закладки
На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.
О CookiesНапомнить позжеПринять