Life-Cycle methods in jsp with example

Life-Cycle methods in jsp with example

The life-cycle of a JSP (JavaServer Page) involves several methods that correspond to different stages of the JSP’s existence. These methods are executed by the JSP container and define the behavior of the JSP from creation to destruction. The main life-cycle methods of a JSP are:

  •  jspInit() : This method is called when the JSP is first initialized.
  •  jspService() : This method is called to handle requests.
  •  jspDestroy() : This method is called before the JSP is destroyed.

Life-Cycle methods

Detailed Explanation and Examples

1. jspInit()

  • Purpose : The jspInit() method is called once when the JSP is initialized. This is similar to the init() method in servlets. It’s used for one-time setup tasks, such as initializing resources.
  • When Called : It is called by the JSP container when the JSP is loaded for the first time or when it is reloaded.

Example
java
public void jspInit() {
    // Initialization code here
    System.out.println("jspInit() method called");
}

2. jspService()

  • Purpose : The _jspService() method is called for each request that the JSP handles. It processes the request and generates a response. This method is automatically implemented by the JSP container, so you don’t override it in your JSP.
  • When Called : It is called by the JSP container for every request made to the JSP.

Example
java
public void _jspService(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    // Request processing code here
    response.getWriter().println("Request handled by _jspService() method");
}

Note: The _jspService() method should not be overridden by JSP developers. It’s generated by the JSP container and includes the code corresponding to the JSP scriptlet and expressions.

3. jspDestroy()

  • Purpose : The jspDestroy() method is called once when the JSP is about to be destroyed. This is similar to the destroy() method in servlets. It’s used for cleanup tasks, such as releasing resources.
  • When Called : It is called by the JSP container just before the JSP is taken out of service.

Example
java
public void jspDestroy() {
    // Cleanup code here
    System.out.println("jspDestroy() method called");
}

Complete Life-Cycle methods

Let’s see a complete example to illustrate these life-cycle methods. We will define a JSP file and include the jspInit() and jspDestroy() methods.

Example: JSP Life-Cycle Example

  Java Class: MyJsp.java (Generated by JSP Container)
java
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;

public class MyJsp extends HttpServlet {

    // Initialization method
    public void jspInit() {
        System.out.println("jspInit() method called");
    }

    // Service method for handling requests
    public void _jspService(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        response.getWriter().println("Request handled by _jspService() method");
    }

    // Cleanup method
    public void jspDestroy() {
        System.out.println("jspDestroy() method called");
    }
}

  JSP File: myJsp.jsp
jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%! 
    // Initialization method
    public void jspInit() {
        System.out.println("jspInit() method called");
    }

    // Cleanup method
    public void jspDestroy() {
        System.out.println("jspDestroy() method called");
    }
%>
<!DOCTYPE html>
<html>
<head>
    <title>JSP Life-Cycle Example</title>
</head>
<body>
    <h1>JSP Life-Cycle Example</h1>
    <%
        // Request handling code
        out.println("Request handled by _jspService() method");
    %>
</body>
</html>

Explanation Life-Cycle methods

  • Initialization : The jspInit() method is defined within the JSP file using <%! … %>. It prints a message when the JSP is initialized.
  • Request Handling : The request handling code is within <% … %> and prints a message for each request.
  • Cleanup : The jspDestroy() method is also defined within the JSP file using <%! … %>. It prints a message when the JSP is destroyed.

Summary

  • jspInit() : Called once during initialization, used for setup tasks.
  • jspService() : Called for each request, used for processing requests and generating responses.
  • jspDestroy() : Called once during destruction, used for cleanup tasks.

Homepage

Readmore