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.
Most Recent jsp Faqs
- How to get the real requested URI from inside the error page?
- Can I define a method in a JSP page?
- What is the difference between request.getParameter() and request.getAttribute()?
- What is difference between page and pageContext in JSP pages?
- How to config a JSP file in web.xml?
- What is the difference between jsp:forward and response.sendRedirect?
- How to retrieve values from HTML form input elements in JSP?
Most Viewed jsp Faqs
- How to config a JSP file in web.xml?
- What is difference between page and pageContext in JSP pages?
- How to disable browser caching for a specific JSP?
- How to retrieve values from HTML form input elements in JSP?
- What is the difference between jsp:forward and response.sendRedirect?
- What is the difference between request.getParameter() and request.getAttribute()?
- How to get the current JSP page name?