JSP life cycle is also managed by container. Usually every web container that contains servlet container also contains JSP container for managing JSP pages.
- conversion
- Compilation
- Instantiation
- Initialization
- service
- Finalization
- Destruction
Conversion
The JSP converter/translator is a part of webserver that is responsible to translate the JSP page into servlet.
Compilation
If the translation is successful, then container compiles the generated servlet source file to generate class file.
Instantiation
In this phase, Servlet Container Instantiate the servlet class.When a container loads a JSP it invokes the jspInit() method before servicing any requests. If you need to perform JSP-specific initialization, override the jspInit() method:
public void jspInit() { // Initialization code... }
Typically initialization is performed only once and as with the servlet init method, you generally initialize database connections, open files, and create lookup tables in the jspInit method.
Service
This phase of the JSP life cycle represents all interactions with requests until the JSP is destroyed.
Whenever a browser requests a JSP and the page has been loaded and initialized, the JSP engine invokes the _jspService() method in the JSP.
The _jspService() method takes an HttpServletRequest and an HttpServletResponse as its parameters as follows:
void _jspService(HttpServletRequest request, HttpServletResponse response) { // Service handling code... }
Finalization
The destruction phase of the JSP life cycle represents when a JSP is being removed from use by a container.
The jspDestroy() method is the JSP equivalent of the destroy method for servlets. Override jspDestroy when you need to perform any cleanup, such as releasing database connections or closing open files.
public void jspDestroy() { // Your cleanup code goes here. }
Destruction
Destroy the servlet instance.
Download the code
0 responses on "2. Jsp life cycle"