KEMBAR78
Java Server Pages(jsp) | PPTX
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.
HTTP request
HTTP response
WEB
SERVER
JSP page
JSP container
compiles to
a servlet
URL
request
JavaBean
Library
DB
properties,
call methods
HTTP page
response
BROWSE
R
DATABASE
SERVER
JSP Architecture
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
Instantiation
Initialization
RequestDestroy
Translation
Compilation
JSP
LIFECYCLE
Web Server
hello.jsp hello_jsp.java
Step: 1
Step: 2
hello_jsp.class
Step: 3Step: 4Step: 5
jspInit() Create
Step: 6
jspService()
Step: 7
jspDestroy()
Web Container
JSP Life Cycle
 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.
Web Container
 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 Life Cycle Phases
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
• EXPRESSION enclosed in <%= and %> markers
A expression is used to insert the result of a Java expression directly into the
output.
• 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);
%>
The time is : <%= new java.util.Date() %>
JSP Elements
• 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" %>
<%
Date x = new java.util.Date();
out.println (x);
%>
Page is used in importing
external classes and providing
information about the page
<%@include file="externalContent.jsp" %>
include is used to
include the code
written in another file.
taglib is used to allow users use tags they
defined themselves using "custom tags"
• DECLARATION enclosed in <%! and %> markers
This tag allows the developer to declare variables or methods.
• Code placed in this must end in a semicolon(;).
• Declarations do not generate output, so are used with JSP expressions or
scriptlets.
<%! private int counter = 0 ;
private String getAccount (int accountNo); %>
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
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 1. Which JSP Lifecycle step is out of order?
A. <% page import=“java.util.Date” %>
B. <%@ page import=“java.util.Date” @%>
C. <%@ page import=“java.util.Date” %>
D. <% import java.util.Date %>
Qus 2. Which is an example of the syntax used to
import a class in a JSP?
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
Qus 3.
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
S
THANK YOU ☺

Java Server Pages(jsp)

  • 1.
  • 2.
    Introduction  JSP (JavaServer 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.
  • 3.
    HTTP request HTTP response WEB SERVER JSPpage JSP container compiles to a servlet URL request JavaBean Library DB properties, call methods HTTP page response BROWSE R DATABASE SERVER JSP Architecture Java Server Pages are part of a 3-tier architecture. A server will act as a mediator between client browser and database.
  • 4.
    JSP Architecture  Thefollowing 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.
  • 5.
  • 6.
    Web Server hello.jsp hello_jsp.java Step:1 Step: 2 hello_jsp.class Step: 3Step: 4Step: 5 jspInit() Create Step: 6 jspService() Step: 7 jspDestroy() Web Container JSP Life Cycle
  • 7.
     Web Containertranslates 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. Web Container
  • 8.
     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 Life Cycle Phases
  • 9.
    JSP Lifecycle Methods Oncea 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.
  • 10.
    What happens toa JSP page when it is translated into Servlet
  • 11.
    • EXPRESSION enclosedin <%= and %> markers A expression is used to insert the result of a Java expression directly into the output. • 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); %> The time is : <%= new java.util.Date() %> JSP Elements
  • 12.
    • 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" %> <% Date x = new java.util.Date(); out.println (x); %> Page is used in importing external classes and providing information about the page <%@include file="externalContent.jsp" %> include is used to include the code written in another file. taglib is used to allow users use tags they defined themselves using "custom tags"
  • 13.
    • DECLARATION enclosedin <%! and %> markers This tag allows the developer to declare variables or methods. • Code placed in this must end in a semicolon(;). • Declarations do not generate output, so are used with JSP expressions or scriptlets. <%! private int counter = 0 ; private String getAccount (int accountNo); %>
  • 14.
  • 15.
  • 17.
    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.
  • 23.
  • 26.
  • 27.
    A. Translate theJSP into a servlet. B. Compile servlet source code. C. Call _jspService() D. Instantiate the servlet class. E. Call jspInit() F. Call jspDestroy() Qus 1. Which JSP Lifecycle step is out of order?
  • 28.
    A. <% pageimport=“java.util.Date” %> B. <%@ page import=“java.util.Date” @%> C. <%@ page import=“java.util.Date” %> D. <% import java.util.Date %> Qus 2. Which is an example of the syntax used to import a class in a JSP?
  • 29.
    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 Qus 3.
  • 30.
    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
  • 31.

Editor's Notes

  • #15 When you develop a webpage, you use HTML to describe what the page should look like and how it should behave.
  • #28 The _jspService() method can never be called before jspInit().