Which lifecycle method is called first when a JSP page is requested?

Java MCQ: Which lifecycle method is called first when a JSP page is requested?

a) init()
b) service()
c) jspInit()
d) jspDestroy()

Answer:

c) jspInit()

Explanation:

The jspInit() method is called first when a JSP page is requested. This method is part of the JSP lifecycle and is called once when the JSP page is initialized, typically when it is accessed for the first time. The jspInit() method is analogous to the init() method in Servlets and is used to perform any initialization tasks required by the JSP page.

Here’s a typical implementation of jspInit():

public void jspInit() {
    // Initialization code
}

After jspInit() is called, the service() method (or its JSP equivalent) is invoked to handle the client request. Finally, when the JSP page is no longer needed, the jspDestroy() method is called to clean up resources.

The jspInit() method is important for setting up resources or configurations that the JSP page might need during its lifecycle.

Reference links:

https://www.rameshfadatare.com/learn-java-programming/
https://www.javaguides.net/p/java-tutorial-learn-java-programming.html

Scroll to Top