JSP Lifecycle

JSP life cycle follows some stages like servlet. JSP are created to support the creating of presentation layer.
Let’s give a closer look:

• JSP is translating to Servlet.
• Compilation of Servlet into .class file.
• class file is loaded by the class loader sub system.
• Servlet instance is created.
• jspInit() method is invoked
• _jspService() method is invoked
• jspDestroy() method is invoked


img

Here the JSP page is first converted into Servlet(.java) file by the JSP translator which is a part of web server. Then the .java file is compiled by java compiler to generate .class file which is loaded by the class loader sub system. Then the Servlet instance is created for whole application which is called as Servlet context object. Then JSP life cycle methods jspInit(), _jspService() and jspDestroy() is invoked by the web container to execute a JSP page.


jspInit():

When web container loads a JSP, jspInit() method is invoked before servicing any requests. If you need to perform JSP-specific initialization, override the jspInit() method:

public void jspInit()
{
//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.


jspService():

This phase of the JSP life cycle provides the service as per the request. 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 object and an HttpServletResponse object as its parameters as follows:

void _jspService(HttpServletRequest request, HttpServletResponse response)
{
//code
}

The jspService() method of a JSP is invoked once per a request and is responsible for generating the response for that request and this method is also responsible for generating responses to all seven of the HTTP methods ie. GET, POST, DELETE etc.


jspDestroy():

When a JSP is being removed from use by a container, then jspDestroy() method is invoked. 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. The jspDestroy() method has the following form:

public void jspDestroy()
{
//code
}

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext