FAQ


Advertisement



How to overwrite the init and destroy method in a jsp page?

You can not override the _jspService() method within a JSP page. You can however, override the jspInit() and jspDestroy() methods within a JSP page. jspInit() may be overridden to perform some initialization task i.e., allocating resources like database connections, network connections, and so forth for the JSP page. jspDestroy() may be overridden to manually release some resorces that the jsp file is holding. It is good programming practice to free any allocated resources within jspDestroy().

The jspInit() and jspDestroy() methods are each executed just once during the lifecycle of a JSP page and are typically declared as JSP declarations:

<%!
public void jspInit() {
. . .
}
%>
<%!
public void jspDestroy() {
. . .
}
%>

The jspDestroy() method is only called when the JSP container is shut down, not after processing each request. It doesn't have access to any of the implicit JSP objects (since they are local variables in the _jspService() method). For example, the following JSP code will have the compiler complains unrecognized variable "request":

<%!   
public void jspDestroy() {
Httpsession session = request.getSession();
new File((String)session.getValue("outfile1")).delete();
new File((String)session.getValue("outfile2")).delete();
}
%>

But most of the time we don't override these life cycle methods.


Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend

Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |