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

Questions by ravikantiramu   answers by ravikantiramu

Showing Answers 1 - 28 of 28 Answers

shirshendu Shekhar Das

  • Mar 18th, 2006
 

 we can overwrite the init method in a jsp page by using declaration tag in our JSP page.

Pls let me know if i m correct.

  Was this answer useful?  Yes

Madhavikari

  • Apr 20th, 2006
 

we can override the init() and destroy methods. We cannot oerride the -jspService() method. If we implement the init() and destroy() methods in jsp, while transleting the servlet, it is overriding the servlet's init() and destroy() methods.

  Was this answer useful?  Yes

rajarshi

  • Apr 25th, 2006
 

Yes we can override init, service, & destroy method i jsp.In jsp the equivalent methods are: jspInit(), jspService(), & jspDestroy(). These methods can be overriden. But we shouldn't override the jspService() method. jspInit() may be overridden to perform some initialization task ( say initiating a DB connection, though in MVC architecture we don't use jsp to initiate DB connection). jspDestroy() may be overridden to manually release some resorces that the jsp file is holding.Once our jspDestroy() method terminates, the container sets the corresponding servlet instance to null so that it can wiped out by the GC.

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

  Was this answer useful?  Yes

manikanth

  • May 30th, 2006
 

hi ,

in jspwe cannot override service method . we can override init() ,destroy() methods. its 100% fact and true.

servlet life cycle methods:

init();

service();

destroy();

jsp lifecycle methods:

init();

_service();

destroy();

remember starting any method with _ means we cannot override this method.

for a good command and grip on servlets and jsp  refer

head first servlets and jsp.

manikanth.d.ra.

sun certified java programmer(s.c.j.p. 1.4)

sun certified web component developer(s.c.w.c.d)

  Was this answer useful?  Yes

ganesh

  • Oct 31st, 2006
 

You can't override _jspService(). But we can override _jspInit() and _jspDestroy().

  Was this answer useful?  Yes

Srinivas

  • Feb 12th, 2007
 

Yes, we can override jspinit method . just define it in jsp declaratice section

  Was this answer useful?  Yes

Ashish Kumar

  • Sep 30th, 2007
 

Yes, we can certainly overwrite the itit() and destroy() method in JSP, but we can't destroy _jspService() method bcz what ever we write in our JSP page, goes to _jspService method that further compiled by servlet. So we can't overwrite _jspService method. (_) symbol is the significance that the method won't be overridden. We can certainly overwrite jspinit and jspdestroy method in <% public void jspInit()
 {
   your code that you wan't to perform only at once probably at the time of loading your jsp like load your driver class to get connection;
}

similarly you can call jspDestroy() method to perform any task that is suppose to happen at last like closing your connection or the file you have opened.

          Please let me know if you have any doubt. I welcome your response.

Ashish.

  Was this answer useful?  Yes

We can overwite only jspinit(), and
jspDestroy()not _jspserive() method


<%!
int number;


public void jspInit()
{
     number = 5;
}


public void jspDestroy()
{
     number = 0;
}
%>

  Was this answer useful?  Yes

Akash Tyagi

  • Aug 16th, 2018
 

We cant be override _jspInit(),_jspDestroy(),_jspService()...it will give the method duplication error.Even _jspService() will be give the compile time error,but the _jspInit() and_jspDestroy() will be give the error at runtime.

Code
  1. <%@page import="java.io.IOException"%>

  2. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"

  3.         pageEncoding="ISO-8859-1"%>

  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

  5. <html>

  6. <head>

  7. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

  8. <title>SHow Jsp page</title>

  9. </head>

  10. <body>

  11.         <h2>Akash</h2>

  12.         <%!public void _jspInit() {

  13.                 System.out.println("init call");

  14.           }

  15.  

  16.         public void _jspDestroy() {

  17.                 System.out.println("destroy call");

  18.         }

  19.  

  20.        

  21.         %>

  22.  

  23.         <%

  24.                 out.println("Hello Jsp");

  25.         %>

  26. </body>

  27. </html>

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions