You are on page 1of 31

Java Server Pages

(JSP)

PRESENTED BY:
Manisha Keim
Introduction
 JSP (Java Server Pages) is server side technology to create
dynamic java web application.
 Allows Java programming code to be embedded in the HTML
pages.
 JSP contains an extension of .jsp
 After execution of a JSP page a plain HTML is produced and
displayed in the client's Web browser.
 JSP can be thought as an extension to servlet technology
because it provides features to easily create user views.
JSP Architecture
URL
JSP page
request

HTTP request
JSP container
compiles to properties, JavaBean
a servlet call methods Library

response
HTTP response
HTTP page
DB

BROWSE WEB DATABASE


R SERVER SERVER
Java Server Pages are part of a 3-tier architecture. A server will act as a mediator between
client browser and database.
JSP Architecture

 The following steps explain how the web server creates the web page using JSP:
 As with a normal page, your browser sends an HTTP request to the web server.
 The web server recognizes that the HTTP request is for a JSP page and forwards it to a JSP engine. This
is done by using the URL or JSP page which ends with .jsp instead of .html.
 The JSP engine loads the JSP page from disk and converts it into a servlet content. This conversion is very
simple in which all template text is converted to println( ) statements and all JSP elements are converted to
Java code that implements the corresponding dynamic behavior of the page.
 The JSP engine compiles the servlet into an executable class and forwards the original request to a servlet
engine.
 A part of the web server called the servlet engine loads the Servlet class and executes it. During execution,
the servlet produces an output in HTML format, which the servlet engine passes to the web server inside
an HTTP response.
 The web server forwards the HTTP response to your browser in terms of static HTML content.
 Finally web browser handles the dynamically generated HTML page inside the HTTP response exactly as if
it were a static page.
Loading

Compilation Instantiation

JSP
Translation
LIFECYCLE Initialization

Destroy Request
JSP Life Cycle

Web Server
Step: 1

hello.jsp hello_jsp.java
Web Container
Step: 7
Step: 6 jspDestroy() Step: 2
jspService()

Step: 5 Step: 4 Step: 3


jspInit() Create
hello_jsp.class
Web Container

 Web Container translates JSP code into a servlet class


source(.java) file, then compiles that into a java servlet class.
In the third step, the servlet class bytecode is loaded using
classloader. The Container then creates an instance of that
servlet class.
 The initialized servlet can now service request. For each
request the Web Container call the jspService() method.
 When the Container removes the servlet instance from
service, it calls the jspDestroy() method to perform any
required clean up.
JSP Life Cycle Phases
 Translation – JSP pages doesn’t look like normal java classes, actually JSP container parse the
JSP pages and translate them to generate corresponding servlet source code. If JSP file name is
hello.jsp, usually its named as hello_jsp.java.
 Compilation – If the translation is successful, then container compiles the generated servlet
source file to generate class file.
 Class Loading – Once JSP is compiled as servlet class, its lifecycle is similar to servlet and it
gets loaded into memory.
 Instance Creation – After JSP class is loaded into memory, its object is instantiated by the
container.
 Initialization – The JSP class is then initialized and it transforms from a normal class to servlet.
 Request Processing – For every client request, a new thread is spawned with ServletRequest
and ServletResponse to process and generate the HTML response.
 Destroy – Last phase of JSP life cycle where it’s unloaded into memory.
JSP Lifecycle Methods

Once a JSP page is translated to a servlet, the container invokes the following life
cycle methods on the servlet :
• jspInit() : This method is invoked at the time when the servlet is initialized.
• jspService() : This method is invoked when request for the JSP page is received.
• jspDestroy() : This method is invoked before the servlet is removes from the
service.
What happens to a JSP page when it is
translated into Servlet
JSP Elements
• EXPRESSION enclosed in <%= and %> markers
A expression is used to insert the result of a Java expression directly into the
output.
The time is : <%= new java.util.Date() %>

• SCRIPTLET enclosed in <% and %> markers:


A scriptlet can contain any number of JAVA language statements, variable or
method declarations, or expressions that are valid in the page scripting language.
<%
String message = “Hello World”;
out.println (message);
%>
• DIRECTIVES syntax - <%@ directive attribute = "value" %>
A JSP directive gives special information about the JSP page to the JSP Engine.
• There are three main types of directive :-
 page : processing information for this page
 include : files to be included
 taglib : tag library to be used in the page

<%@page import="java.util.Date" %> <%@include file="externalContent.jsp" %>


<%
Date x = new java.util.Date();
include is used to
out.println (x); include the code
%> written in another file.

Page is used in importing


external classes and providing taglib is used to allow users use tags they
information about the page defined themselves using "custom tags"
• DECLARATION enclosed in <%! and %> markers
This tag allows the developer to declare variables or methods.

<%! private int counter = 0 ;


private String getAccount (int accountNo); %>

• Code placed in this must end in a semicolon(;).


• Declarations do not generate output, so are used with JSP expressions or
scriptlets.
HTML
JSP – ENVIRONMENT
SETUP
What is JDK? JRE?

 JRE: Java Runtime Environment. It is basically the Java Virtual


Machine where your Java programs run on.
 JDK: It's the full featured Software Development Kit for Java,
including JRE, and the compilers and tools to create and compile
programs.
 The JDK is a superset of the JRE, and contains everything that is in
the JRE.
 Sometimes, even though you are not planning to do any Java
Development on a computer, you still need the JDK installed.
Because application server will convert JSP into Servlets and
use JDK to compile the servlets.
Java – Environment Setup
QUESTIONS
Qus 1. Which JSP Lifecycle step is out of order?

A. Translate the JSP into a servlet.


B. Compile servlet source code.
C. Call _jspService()
D. Instantiate the servlet class.
E. Call jspInit()
F. Call jspDestroy()
Qus 2. Which is an example of the syntax used to
import a class in a JSP?

A. <% page import=“java.util.Date” %>


B. <%@ page import=“java.util.Date” @%>
C. <%@ page import=“java.util.Date” %>
D. <% import java.util.Date %>
Qus 3.

To Display:
"Good Morning", if it is between 3:00am and 12:00pm
"Good Afternoon", if it is between 12:00pm and 6:00pm
"Good Evening", if it is after 6:00 pm
References
 http://www.studytonight.com/jsp/lifecycle-of-jsp.php
 https://www.ntu.edu.sg/home/ehchua/programming/java/JSPB
yExample.html
 http://met.guc.edu.eg/OnlineTutorials/JSP%20-
%20Servlets/A%20simple%20JSP%20example.aspx
 Head First JavaScript Programming: A Brain-Friendly Guide
 https://www.tutorialspoint.com/java/java_environment_setup.h
tm
THANK YOU ☺
S

You might also like