Introduction to JSP

JSP Chapters

Introduction to JSP

Introduction to Java Server Pages (JSP)

Java Server Pages is a server side technology that helps software developers to create dynamic web pages.

JSP technology is built on servlet technology by separating business logic (java code) and presentation logic (HTML code)

JSP is not meant to replace servlet technology , however JSP is an extension of the servlet technology, and it is good practice to use both servlets and JSP pages in the same web applications for development.

JSP’s is run on web container in which JSP page is translated into standard java class or servlet & processed.

Advantages of using JSP technology:

  • If there are changes in web pages , pages are automatically recompiled
  • Business logic and presentation logic is separated which minimizes the compilation time and increases the performance of web application
  • JSP technology provides writing user defined custom tags
  • Addressing JSP pages is much simpler than calling servlets ( servlets use deployment descriptor web.xml for addressing)


For any further quires do write us to


Difference between HTML and JSP

JSP is used to develop both static and dynamic web pages & process dynamic data. HTML  is mainly used to develop static web pages & process static data.
JSP is Server side technology HTML is Client side technology
JSP page needs web container to get processed HTML needs simple web browser to get processed
JSP supports html tags & also custom tags or tag libraries HTML only supports html anchor tags

JSP Basics

JSP Fundamentals

Java Server Pages (JSP) is a technology used to build web pages that support dynamic content processing with the help of special tags called JSP tags or elements.

Simple HTML pages can be turned into JSP pages by changing its extension to .jsp and adding tag which start with <% and end with %>.

 out.println("Hello, I am new to JSP Technology"); 


The character sequences <%  and %> are called or enclosed JSP Scriplets ( which you will learn later in this tutorial) , which are evaluated at run time by web container to generate dynamic pages that change in response to user requests.

Displaying the Server Time Example:

 <TITLE>Displaying the server time</TITLE> 
Welcome. The server time is now 
 java.util.Calendar now = java.util.Calendar.getInstance(); 
 int hour = now.get(java.util.Calendar.HOUR_OF_DAY); 
 int minute = now.get(java.util.Calendar.MINUTE);

if (hour<10) 
 out.println("0" + hour); 


if (minute<10) 
 out.println("0" + minute); 


How JSP Works

Processing of Execution of JSP Request

Step1: Request from Client

A JSP page has the extension as .jsp

Client request is made by tying the page name with extension .jsp on the web browser. Example index.jsp

Step2: Request Sent To Server

The Request received is from clients browser is sent to the web server

Step3: JSP Container

The web server passes the request to JSP Container for processing.

Step4: Process of generating servlet

JSP files are compiled by the JSP Container into a servlet. This step creates the .jsp file as a Java servlet source file (.java file)

Step5: Process of generating servlet to class file

.java files are converted into .class file and to execute the business logic

Step6:- Instantiation of Servlet:

The instantiation of servlet is performed using the init () and service () methods.

Step7:- Generating Output:

The request received from client is executed and the output is generated as HTML.

Step8:- Client receives the Output:

The web server sends the response back to client in the form of HTML displayed in browser

Life Cycle of a JSP

Life Cycle of a JSP

Life cycle of a JSP can be divided into following phases

  • Translation
  • Initialization
  • Servicing
  • Destruction



When a request is made by client for JSP page, the JSP container will check that JSP page is correctly formed and syntax is correct. If the syntax check is successful then the JSP container will translate the JSP into standard Java servlet. Servlet will be compiled into a class file by the JSP container and will be ready for use.

Whenever container receives a request, it first checks if the JSP file has be modified or since it was last translated. If it is not translated, it retranslated so that the response generated will have the most up-to-date information of the JSP file.


Once the translation phase has been completed, the JSP container will load the generated class file and creates an instance of the servlet in order to continue processing of the initial request.


Once the JSP container has completed all the preliminary work, the initial request will be serviced. The important methods that are generated in this phase are

This method is used to initialize the servlet for further processing.

This method provides all of the functionality for handling a request and returning a response to the client.


After processing the clients request the servlet has to be destroyed. The servlet container will call the jspdestroy() method to destroy the servlet. Once this method is called the servlet will no longer will respond to client request.

JSP Syntax

JSP Syntax

To write an effective JSP page, we have to understand all these elements or tags used in JSP.

JSP Tags are divided into

  • Directive elements
  • Scripting elements
  • Action elements

We need to  learn how  to incorporate these elements in JSP which affects the JSP servlets that result from the translation of JSP pages for processing the request.

Directive elements

Directives are messages to the JSP container which contains information on how the container must translate a JSP page into a corresponding servlet.

General Syntax:

For single attribute

<%@ directive (attribute="value") %>

For Multiple attribute

<%@ directive attribute1="value1" attribute2="value2"%>

Note: Each attribute value must be quoted.

Directives are further divided into

  • Page directives
  • Include directives
  • Tag library directives

Page directives:

All the information pertaining to JSP Page such as language used, contentType, import and so on has to declared in page directive.

The following table will give more information about the page directives

Attribute Value Type Default Value
language Scripting language name “java”
info String Depends on the JSP container
contentType MIME type, character set “text/html; charset=ISO-8859-1”
extends Class name None
import Fully qualified class name or package name None
buffer Buffer size or false 8192
autoFlush Boolean “true”
session Boolean “true”
isThreadSafe Boolean “true”
errorPage URL None
isErrorPage Boolean “false”


Example for page directive:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>

Note: In JSP page we can write as many page directives as we wish.

Include directives:

Include directive enables JSP page owner to include the contents of other files in the current JSP page.

Example for Include directive

<%@include file="header.html"%>


Tag library directives

Tag library directives are used to include custom tags into JSP Page.

A custom tag is a user-defined JSP language element which can be used to create a JSP page.

We will discuss custom tags later in this chapter

Scripting Elements

Scripting elements

Scripting elements allows us to insert java code into the JSP page.

There are three types of scripting elements

  • Declarations
  • Scriplets
  • Expressions


Declarations allow us to declare methods and variables that can be used in the JSP page.

A declaration tag starts with a <%! and ends with a %> and can appear anywhere throughout the page

<%! Declarative element %>


Scriptlets are the code blocks of a JSP page where actual logic is written.

Scriptlets start with an opening <% tag and end with a closing %> tag.

<% Scriplet element %>


Expressions are used to print the result evaluated in a JSP page

When the JSP page is requested and their results are converted into a String and printed using Expression.

If the result cannot be converted into a String, an error will be raised at translation time or request processing time. ClassCastException will be raised.

An expression starts with a <%= and ends with a %>.

<%= Expression element %>


Comments are ignored by JSP container during page translation.

<%- – Comment should be written here- -%>

Implicit Objects

Implicit Objects

Implicit objects are predefined java objects or variables provided by JSP container for minimizing the use of Scriplets and Expressions used in JSP page.

Usage of Implicit objects minimizes the time required  for processing of JSP request.

Few of them are given below

request Implicit Object

This variable is of type HTTP request and implements interface javax.servlet.http.HttpServletRequest

It gives you access to the request parameters, the request type (e.g. GET or POST), and the incoming HTTP request headers (e.g. cookies etc).

<TITLE>Simple Page</TITLE> 
 String firstName = request.getParameter("firstName"); 
 out.println("First name: " + firstName); 

response Implicit Object

This variable is of type HTTP response and implements interface javax.servlet.http.HttpServletResponse

By using it, you can set HTTP status codes, content type and response headers etc.

out Implicit Object

This is the object of JspWriter.

It used to send output to the client.

session Implicit Object

This variable is of type HTTP session and implements interface javax.servlet.http.HttpServletResponse

It is used to work with Session Object

application Implicit Object

represents the servlet context for the Web application that implements the javax.servlet.ServletContext

Allows to store values in key-value pair form that are shared by all servlets in same web application

config Implicit Object

Represents the servlet configuration for a JSP page

This implements javax.servlet.ServletConfig interface

Represents the JSP configuration options e.g. init-parameters etc.

pageContext Implicit Object

This variable is instance of javax.servlet.jsp.PageContext, to give a single point of access to many of the page attributes.

This object is used to store the object values associated with the JSP Page.

page Implicit Object

This is an instance of the JSP page implementation class and is created when the page is translated.

The page implementation class implements the interface javax.servlet.jsp.HttpJspPage.

exception Implicit Object

This variable is of type java.lang.Throwable.

It represents the exception that is passed to JSP or error page.