JSP Introduction and Life Cycle

0
634

JSP (Java Server Pages) basically separates the work of web designers and developers i.e. it separates ‘User Interfaces’ from ‘Content Generation’.
In this post, I am going to cover the basics of JSP, Hello World code sample, JSP Life Cycle, Components and exception/error handling.

What is a JSP

A JSP is simply an HTML page containing additional code to execute application logic which generates dynamic content. A JSP may contain the code for: JDBC, EJB, RMI etc.

<html>
<head><title>My First Hello World JSP</title></head>
<body>
<font size="20"><%="Hello World JSP !!!" %></font>
</body>
</html>

How JSP Works

    • The source code (JSP files) is copied to document directory of the web server. When a request is made for this page, the server recognizes the *.jsp extension and realizes that special handling is required.
    • First time the file is requested, it is compiled into a servlet object and stored in memory and the output is sent back to the requesting client.
    • After the first request, the server checks to see whether *.jsp file has changed. If not changed, server invokes the previously compiled servlet object.

JSP Life Cycle

To understand the JSP life cycle, let’s have a look at the below diagram which depicts the story of a JSP:

JSP Life Cycle
JSP Life Cycle

JSP Page Translation

First of all JSP page gets translated or parsed into a servlet class file. So putting simply, JSP turns into a servlet. Note that if there would be errors, you can get page translation errors too. To know more about JSP errors read here.

JSP Page Compilation and Loading

So now the parsed JSP file gets compiled and turns into a .class file and get loaded.

Instance Creation and Initialization

This phase instantiates the Servlet instance by calling jspInit() method.

JSP Service

This is the phase where JSP spends most of its life, i.e. serving the requests. When the request comes from any of the clients, _jspService() method gets called to get the response.

JSP Destroy

This is where the JSP gets ready for garbage collection. jspDestroy() method gets called in this phase and if needed all the DB connection, n/w connections get closed here.

We need to note that the generated servlet class from JSP, implements HttpJspPage interface. I would add more details around this and extend this post with that later.

There are three components of JSP which are required to understand to work with JSP:

A) Directives
B) Actions
C) Implicit Objects

We will cover all of these gradually starting Directives. So in my next post I would detail out JSP Directives. Till then happy learning JSP and share what you need to know more about the basics of JSP.

 

Previous articleSingleton Bean Vs. Pattern
Next articleHandling JSP Errors
I have spent almost 10 years playing around Java and related technologies. I love to write on different topics and would be very much willing to hear back your feedback/suggestions on them. This site is a medium to share my knowledge with the Java folks and grow further. My other interests include traveling, driving, swimming and dance. But yes, my web site has become my passion over the time :) I live in Scotland and travel to India often, my roots being there.

NO COMMENTS

LEAVE A REPLY