JSP jspInit and jspDestroy

JSP JSPInit and JSPDestroy methods

JSP has two callback methods that you can define within a declaration scripting element.

The JSP container will call them at two different points during the life of a JSP page.

The following table describes when the container calls these methods:

Signature

Description

publicvoid jspInit(void)

The method is called during the initialization of the JSP page, and then
before the first request is processed by side.
The method can be used to obtain any global resources that could be used
in request processing logic. The method is optional for a JSP page
and in most JSP pages not necessary.

publicvoid jspDestory(void)

The methode is called by the container just before destroying the JSP page. This
method can be used to release the resources that it acquired in
jspInit(). Most JSP pages have no need to declare this method

Every JSP is actually a Java class. A JSP Java class implements the
javax.servlet.Servlet interface. In other words, a JSP is always a servlet.

Many of the JSP servlet resources are ready for use when jspInit() is executed.
As an example, you can bind attributes with object of any kind to the servlet context using the method getServletContext().

Example of using JSPInit_and_JSPDestroy.

In the example we use Netbeans IDE and Glassfish Server.

You can download this example here(needed tools can be found in the right menu on this page).

This file starts with the normal xml tag for a XML file and
the root tag for the deployment descriptor is web-app. Every ting
inside the last tag is to tell the server about our application,
which in this case is a JSP file.

With a servlet tag we give the JSP file a servlet name,
which is used in the servlet-mapping tag to specify a url
for the JSP file.

In this way we can have many urls for the same JSP file.

If no session-timeout (the server ends the service
of the application after this time) is given a standard timeout for the server is used
as timeout for the application.

The welcome-file tag specifies the startup for our application,
which in this case and our application is the welcome file with url JSPCars.
Reorganize the welcome-file-list to what is shown above.

Creating Web-server Deployment descriptor.

The context-root (in example /JSPInit_and_JSPDestroy) for the application
will in most cases be specified by a server vendor deployment descriptor.