Saturday, 28 January 2017

What are JSP lifecycle methods ?

Their are 3 life cycle methods of JSP :
  1. jspInit()
  2. _jspService()
  3. jspDestroy()

jspInit() : method is used to initialize the jsp file,it only initialize when jsp is compiled into class file. This method is once called by container during life cycle of jsp. This method can also overridden to initialize something like database connection or any resources.

1
2
3
public void jspInit() 
{
  // do your code here
}

_jspService() : method is called by container everytime when client request, this service method starts with underscore to distinguish from other methods because this method is always get executed and it can not be override.This method takes HttpServletRequest and HttpServletResponse objects as a parameter.

1
2
3
4
void _jspservice(HttpServletRequest request HttpServletResponse response)
{
 //handling all request and responses
}

jspDestroy() : method is called by container when particular servlet or jsp is no longer in use. When this method is calls the life cycle of jsp ends and jsp submits itself for the garbage collection. This method can be override if any cleanup operation is to be performed like for closing database connection or for closing any file.

1
2
3
4
public void _jspdestroy()
{
            //all clean up code
}



      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



No comments:

Post a Comment