If JSP turns into a Servlet why there are different life cycle methods for example jspInit()
and init()
?
-
1no it has http://javapapers.com/jsp/jsp-life-cycle-explain/ – Harshana Jan 22 '13 at 17:14
-
Servlet lifecycle is init->service->destroy (in short) – Arpit Jan 22 '13 at 17:15
-
only the 5,6,7 are common nothing else. – Arpit Jan 22 '13 at 17:16
-
this doesn't look like a real question as you already have the link to answer... – Alonso Dominguez Jan 22 '13 at 17:16
-
A lot difference is there. jsp is slower ther servlets. – Arpit Jan 22 '13 at 17:19
-
1@LuiggiMendoza You can delete your first comment because it is not true - _JSP doesn't have a lifecycle because it will only generate HTML for the client._ Indeed it does have a lifecycle. – Fallup Jan 22 '13 at 17:23
2 Answers
The reason there is a jspInit
separate from the servlet init
method is one is there for the servlet implementing the JSP (created by the developer implementing the servlet container), the other is there for the JSP code (used by the application developer). If the JSP used the init method it could override whatever the servlet implementation was doing. With separate methods the JSP implementer can take the code from the jspInit
and add it to the init
method of the generated servlet.

- 94,330
- 19
- 181
- 276
I'll try to explain it correctly in the most simple way (as addition to the correct @Nathan Hughes answer) :
From the perspective of HTML vs JAVA code, servlet is more like HTML wrapped with JAVA. It gives a strong support for handling application layer in the multilayered architecture. On the other hand JSPs were created to support the creating of presentation layer. The init()
method of a servlet is called only once during initialization of the servlet.
So the first point : Servlets were here before JSPs.
Now to JSPs. Again from the perspective of HTML and JAVA, JSP is more like JAVA wrapped with HTML.
WEB CONTAINER performs a translation of the JSP "source code" to the equivalent Servlet java code. This translated java Servlet source code is then compiled and the WEB CONTAINER handles the realisation of the Servlet. Simply:
MyPage.jsp --> (translate) --> MyPage_jsp.java --> (compile) --> MyPage_jsp.class --> (load) --> Java Servlet
The jspInit()
method is called by WEB CONTAINER as a part of the initialization phase of the JSP lifecycle.
So to your question : jspInit()
not equals init()
.

- 2,417
- 19
- 30