JSP lifecycle method and Servlet

If JSP turns into Servlet, why are there different lifecycle methods like jspInit()

and init()

?

+3


source to share


2 answers


I'll try to explain it correctly in the simplest way possible (as an addition to @ Nathan Hughes correct answer):

In terms of HTML and JAVA code, the servlet is more like HTML wrapped in JAVA. This provides strong support for application layer processing in a multilayer architecture. On the other hand, JSPs were created to support presentation tier creation. The init()

servlet method is called only once during servlet initialization .

So first point: Servlets were here before JSPs.

Now JSP. In terms of HTML and JAVA, JSP is more like JAVA wrapped in HTML.



WEB CONTAINER translates the JSP source code to the equivalent Java servlet code. This translated Java Servlet source code will be compiled and the WEB CONTAINER handles the Servlet implementation. Just:

MyPage.jsp -> (translate) -> MyPage_jsp.java -> (compile) -> MyPage_jsp.class -> (load) -> Java Servlet

The method jspInit()

is called by WEB CONTAINER as part of the initialization phase of the JSP lifecycle.

So to your question: jspInit()

not equal init()

.

+2


source


The reason there is jspInit

, apart from the servlet method init

, there is one for the servlet implementing the JSP (created by the developer implementing the servlet container) and the other for the JSP code (used by the application developer). If the JSP was using the init method, it could override all the actions the servlet takes. With separate methods, the JSP developer can take the code from jspInit

and add it to the method of the init

generated servlet.



+3


source







All Articles