beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Java Server Pages (JSP) Life Cycle

By Chaitanya Singh | Filed Under: JSP tutorial

« Previous Tutorial: JSP Introduction

Next Tutorial: JSP Directives »

JSP pages are saved with .jsp extension which lets the server know that this is a JSP page and needs to go through JSP life cycle stages.
In my previous post about JSP introduction, I explained that JSP is not processed as such, they first gets converted into Servelts and then the corresponding servlet gets processed by Server.

When client makes a request to Server, it first goes to container. Then container checks whether the servlet class is older than jsp page(  To ensure that the JSP file got modified). If this is the case then container does the translation again (converts JSP to Servlet) otherwise it skips the translation phase (i.e. if JSP webpage is not modified then it doesn’t do the translation to improve the performance as this phase takes time and to repeat this step every time is not time feasible)

The steps in the life cycle of jsp page are:

  1. Translation
  2. Compilation
  3. Loading
  4. Instantiation
  5. Initialization
  6. RequestProcessing
  7. Destruction

JSP-life-cycle

Let see the Life cycle of JSP in more detail –
1) As stated above whenever container receives request from client, it does translation only when servlet class is older than JSP page otherwsie it skips this phase (reason I explained above).
2) Then the container –

  • compiles the corresponding servlet program
  • Loads the corresponding servlet class
  • Instantiates the servlet class
  • Calls the jspInit() method to initialize the servlet instance( Jsp container will do this job only when the instance of servlet file is not running or if it is older than the jsp file.)

[code language=”java”]
public void jspInit()
{
//code to intialize Servlet instances
}[/code]

3) A new thread is then gets created, which invokes the_jspService() method, with a request (HttpServletRequest) and response (HttpServletRespnse) objects as parameters -shown below.

[code language=”java”]
void _jspService( HttpServletRequest req, HttpServletResponse res)
{
//code goes here
}[/code]

4) Invokes the jspDestroy() method to destroy the instance of the servlet class. code will look like below –

[code language=”java”]
public void jspDestory()
{
//code to remove the instances of servlet class
}[/code]

« Previous Tutorial: JSP Introduction

Next Tutorial: JSP Directives »

Enjoyed this post? Try these related posts

  1. How to validate and invalidate session in JSP
  2. How to access body of Custom tags in JSP tutorial
  3. Solution – The superclass “javax.servlet.http.HttpServlet” was not found on the Java Build Path
  4. JSP Expression Language (EL) – JSP Tutorial
  5. Difference between include directive and include tag in JSP
  6. Config Implicit Object in JSP with examples

Comments

  1. Poornima says

    November 5, 2014 at 4:55 PM

    Thank you sir,

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

JSP Tutorial

  • Basics of JSP
  • Life cycle of JSP
  • JSP in Eclipse IDE
  • Declaration tag
  • Expression tag
  • Scriptlets
  • Directives
  • Include Directive
  • Param Directive
  • Exception handling
  • Action tags
  • Include action
  • Forward action
  • useBean, setProperty & getProperty
  • Implicit Objects
  • Session implicit object
  • Validate session
  • Request implicit object
  • Response implicit object
  • Out implicit object
  • Application implicit object
  • Config implicit object
  • pageContext implicit object
  • Exception implicit object
  • Expression language
  • Custom Tags
  • Custom tag example
  • JSP Interview Q

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap