SlideShare une entreprise Scribd logo
1  sur  23
Introduction to JSP
JSP and Servlets / Session 5 / 2 of 24
Objectives
Explain JSP
Identify the advantages of using JSP
Describe various elements of JSP
Describe the JSP Life Cycle
Develop JSP using Java Studio Enterprise 8
JSP and Servlets / Session 5 / 3 of 24
Introduction to JSP
Java Server Pages (JSP) are saved with the
extension .jsp.
Efficiently controls dynamic content generation.
Uses Java programming language and class
libraries.
Uses HTML for presentation of pages and Java
code to access dynamic content.
JSP page
JAVA
Server
JSP uses JAVA
to access
dynamic content
JSP and Servlets / Session 5 / 4 of 24
Benefits of JSP 3-1
Separates content from presentation
Request
Response
JSP page
Static
Content
Dynamic
Content
Server
Client
Web Designer JSP Programmer
JSP and Servlets / Session 5 / 5 of 24
Benefits of JSP 3-2
Emphasizes reusable components
JSP page 1
Static
Content
Dynamic
Content
JSP page 2
Static
Content
Dynamic
Content
JSP page 3
Static
Content
Dynamic
Content
JavaBean
Multiple JSP
pages use the
same JavaBean
JSP and Servlets / Session 5 / 6 of 24
Benefits of JSP 3-3
Simplified page development - Web designer
and Web programmer use Web development
tools to develop a JSP page.
JSP page
Static
Content
Dynamic
Content
Web Designer JSP Programmer
Web Development Tools
Macromedia
Dreamweaver
Java Studio
Enterprise 8
.....
JSP and Servlets / Session 5 / 7 of 24
Elements of JSP 2-1
Elements of a JSP page
Static Content
Directives
Expressions
Scriptlets
JSP Page
Taglib:
<%@ taglib uri= "tagLibraryURI"
prefix= "tagPrefix" %>
<%valid Java code block%>
<%= Java Expression %>
Page :
<%@ page ATTRIBUTES %>
Include:
<%@ include file = " Filename" %>
Declarations
Actions
<%! declaration(s) %>
Forward
Include
Plug-ins
Bean tags
JSP and Servlets / Session 5 / 8 of 24
Elements of JSP 2-2
<%@ page language="java" %>
<html>
…
…
<h1>Elements of JSP</h1>
<p>Today is </p>
<page id="clock" class=
"calendar.jspCalendar" />
<ul>
<li>Day: <%=clock.getDayOfMonth() %>
<li>Year: <%=clock.getYear() %>
</ul>
Page Directive
JSP ExpressionStatic Content
<%
if (Calendar.getInstance().get(Calendar.AM_PM)
== Calendar.AM)
{
%>
Good Morning
<%
}
else
{
%>
Good Afternoon
<%
}
%>
<%@ include file="copyrightfile.html" %>
…
…
</html>
JSP Scriptlet
Include Directive
JSP and Servlets / Session 5 / 9 of 24
Static Content
Static content is the text written on a Web page.
Any text-based format can be used to write static
content.
A page directive is used to specify the format of
content.
<html>
<%@ page contentType = "text/html" %>
<head>
<title>Example for static content</title>
</head>
…
//Contains text in specified content type.
…
</body>
</html>
HTML tags contain
static content
JSP and Servlets / Session 5 / 10 of 24
JSP Directives
JSP container uses directives for processing of
JSP page.
Controls the structure of the Servlet
Provides global information about a JSP page
Scope of directives is the entire JSP file
<html>
...
<%@ page language="Java" import=
“java.rmi.*, java.util.*"
session="true" buffer="12kb" autoFlush="true"
info="PageDirective" errorPage="error.jsp"
isErrorPage="false"
isThreadSafe="true" %>
<head>
<title>Testing Page Directive</title>
</head>
<body>
<h1>Testing Page Directive</h1>
This page is testing Page Directive.
</body >
…
…
</html>
page Directive
Demonstration: Example 1
<html>
<head>
<title>Testing include
directive</title>
</head>
<body>
<h1> Example of directives</h1>
<%@ include file ="testFile.html" %>
</body>
</html>
include Directive
// testFile.html
<html>
<body>
The JSP <b>"include"</b> directive example.
</body>
</html>
Demonstration: Example 2
JSP and Servlets / Session 5 / 11 of 24
JSP Expression
Contains a Java statement
Value of Java Statement will be evaluated and
inserted into generated Web page.
Displays individual variables, or the result of
some calculation.
<html>
<head>
<title>Testing Expression directive</title>
</head>
<body>
<h1> Testing Expression directive </h1>
<% int i = 2, j=3;%>
<% i++;%>
<% j=j+i; %>
…
</body>
</html>
JSP Expression
JSP and Servlets / Session 5 / 12 of 24
JSP Scriptlet
Block of Java code that performs functions
which are not supported by tags.
Executed during run time
<html>
<head>
<title>Scriptlet of a JSP </title>
</head>
<body>
<h1>Scriptlet of a JSP</h1>
<%
int k=0;
for(int i=0;i<5;i++)
{
k=k+i;
System.out.println(k);
}
%>
...
</body>
</html>
JSP Scriptlet
JSP and Servlets / Session 5 / 13 of 24
JSP Declaration
Used to define variables and methods in a JSP
page.
Declared variables and methods can then be
referenced by other scripting elements on the
same page.
Used to define single or multiple variables
<%!
int x = 0, y = 0;
private String units = "ft";
%>
Declares x and y as
integer variables
Declares units as
a String variable
<%!
public long fact (long x)
{
if (x == 0) return 1;
else return x * fact(x-1);
}
%>
Declares
fact method
JSP and Servlets / Session 5 / 14 of 24
JSP Actions
Allows the transfer of control between pages
Allows JSP pages to access JavaBeans
component objects stored on the server.
JSP Actions
Forward Include Plug-ins Bean tags
JSP and Servlets / Session 5 / 15 of 24
Forward Action
Permanently transfers control from a JSP page
to another location.
JSP Page 1
Forward
Action
JSP Page 2
Transfers control
to another page
JSP and Servlets / Session 5 / 16 of 24
Include Action 2-1
Inserts the content generated by remote
resource in the output of the current JSP page.
Temporarily transfers the control to a new page
JSP Page 1
Include
Action
JSP Page 2
Transfers control
to another page
Transfers control
back to the original
page
JSP and Servlets / Session 5 / 17 of 24
Include Action 2-2
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<title>jspactionexample</title>
</head>
<body bgcolor="#ffffff">
<h1>Jsp actions and declarations</h1>
<jsp:include flush="true"
page="JspExample.jsp">
<jsp:param name="stringeg" value="String
passed using forward directive"/>
</jsp:include>
</body>
</html>
Include Action
Demonstration: Example 3
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page language="java" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title>Declaration, Expression and Actions</title>
<%=request.getParameter("stringeg")%>
<%! static private char[] vowels ={ 'a', 'e', 'i', 'o',
'u', 'A', 'E', 'I', 'O', 'U'};
String word;
public boolean startsWithVowel(String word) {
this.word=word;
char first = word.charAt(0);
for (int i = 0; i < vowels.length; ++i) {
if (first == vowels[i])
return true;
}
return false;
}
JSP Declarations
static private String[] articles = { "a ", "an " };
public String withArticle(String noun) {
if (startsWithVowel(noun)) return
articles[1] + noun;
else return articles[0] + noun;
}
%>
</head>
<body bgcolor="#ffffff">
<br />
<b>Starts with a vowel: </b>
<%=startsWithVowel("I am a very good Programmer")%>
<br/>
<b>String entered is: </b>
<%=word%>
<br />
<b>Article:</b>
<%=withArticle("apple")%> <br />
</body>
</html>
JSP Declarations
Demonstration: Example 4
JSP and Servlets / Session 5 / 18 of 24
JSP Plug-ins and Bean Tags
JSP Plug-ins - Used to generate browser-specific
HTML.
JSP Bean Tags – Used to interact with
JavaBeans stored on the server.
JSP and Servlets / Session 5 / 19 of 24
JSP Life Cycle 3-1
Client
JSP page
3 Execution
2
Translation
Compilation
1
Servlet
Request
Server
Request
ResponseResponse
JSP Life Cycle
JSP and Servlets / Session 5 / 20 of 24
JSP Life Cycle 3-2
Translation and Compilation
Translation
Compilation
Servlet
JSP
Determines
Errors in JSP
Extracts data
from JSP element
Generates a Servlet
for JSP
JSP and Servlets / Session 5 / 21 of 24
JSP Life Cycle 3-3
Execution – Actions performed during execution
are:
<%@ page buffer = "none|20kb" %>
<%@ page errorPage = "errorJSP.html" %>
Handling Errors
Sets buffer size
Specifies error page
Buffering Output
JSP and Servlets / Session 5 / 22 of 24
Demonstration: Example 5
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%!
double radius=6.0;
private double getRadius(){
return radius;
}
private double getDiameter(){
return (radius * 2);
}
private double getArea(){
return (3.1415 * radius);
}
private double getCircumference(){
return(3.1415 *(radius * 2));
}
%>
JSP declarations
JSP Application in Java Studio Enterprise 8
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title>Declaration Tag - Methods</title>
</head>
<h3>Calculating area and circumference of a
Circle</h3>
<hr/>
<b>Radius of circle:</b> <%=radius%> cm<br/>
<b>Diameter:</b> <%=getDiameter()%> cm<br/>
<b>Area of Circle is:</b> <%=getArea()%>
cm<sup>2</sup><br/>
<b>Circumference of a circle is:</b>
<%=getCircumference()%><br/>
<hr/>
<body></body>
</html>
JSP scriptlets
JSP and Servlets / Session 5 / 23 of 24
Summary
 JSP uses Java programming language and class
libraries.
 JSP page uses HTML to display static text and Java
code to generate dynamic content.
 Elements of JSP page are static content, JSP directives,
JSP expressions, and JSP scriptlets.
 A JSP page can be created using standard development
tools.
 JSP uses reusable and cross-platform components,
such as JavaBeans.
 JSP allows the creation of user-defined tags and makes
the JSP development process easy.
 Different phases in JSP life cycle are translation,
compilation, and execution.

Contenu connexe

Tendances (20)

Jsp sasidhar
Jsp sasidharJsp sasidhar
Jsp sasidhar
 
JSP Scope variable And Data Sharing
JSP Scope variable And Data SharingJSP Scope variable And Data Sharing
JSP Scope variable And Data Sharing
 
Jsp element
Jsp elementJsp element
Jsp element
 
Jsp
JspJsp
Jsp
 
Jsp chapter 1
Jsp chapter 1Jsp chapter 1
Jsp chapter 1
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
Java Server Pages(jsp)
Java Server Pages(jsp)Java Server Pages(jsp)
Java Server Pages(jsp)
 
Servlet and jsp interview questions
Servlet and jsp interview questionsServlet and jsp interview questions
Servlet and jsp interview questions
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
JSP
JSPJSP
JSP
 
Jsp
JspJsp
Jsp
 
JSP Directives
JSP DirectivesJSP Directives
JSP Directives
 
Jsp(java server pages)
Jsp(java server pages)Jsp(java server pages)
Jsp(java server pages)
 
Jsp
JspJsp
Jsp
 
Java server pages
Java server pagesJava server pages
Java server pages
 
Implicit objects advance Java
Implicit objects advance JavaImplicit objects advance Java
Implicit objects advance Java
 
Introduction to the Servlet / JSP course
Introduction to the Servlet / JSP course Introduction to the Servlet / JSP course
Introduction to the Servlet / JSP course
 
Jsp ppt
Jsp pptJsp ppt
Jsp ppt
 
JSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGESJSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGES
 
Jsp interview questions by java training center
Jsp interview questions by java training centerJsp interview questions by java training center
Jsp interview questions by java training center
 

Similaire à Introduction to jsp

Similaire à Introduction to jsp (20)

JSP.pptx
JSP.pptxJSP.pptx
JSP.pptx
 
JSP Components and Directives.pdf
JSP Components and Directives.pdfJSP Components and Directives.pdf
JSP Components and Directives.pdf
 
Atul & shubha goswami jsp
Atul & shubha goswami jspAtul & shubha goswami jsp
Atul & shubha goswami jsp
 
Jsp in Servlet by Rj
Jsp in Servlet by RjJsp in Servlet by Rj
Jsp in Servlet by Rj
 
Session 5 : intro to jsp - Giáo trình Bách Khoa Aptech
Session 5 : intro to jsp  - Giáo trình Bách Khoa AptechSession 5 : intro to jsp  - Giáo trình Bách Khoa Aptech
Session 5 : intro to jsp - Giáo trình Bách Khoa Aptech
 
Java .ppt
Java .pptJava .ppt
Java .ppt
 
25.ppt
25.ppt25.ppt
25.ppt
 
J2EE - JSP-Servlet- Container - Components
J2EE - JSP-Servlet- Container - ComponentsJ2EE - JSP-Servlet- Container - Components
J2EE - JSP-Servlet- Container - Components
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
J2EE jsp_01
J2EE jsp_01J2EE jsp_01
J2EE jsp_01
 
Unit 4 web technology uptu
Unit 4 web technology uptuUnit 4 web technology uptu
Unit 4 web technology uptu
 
Unit 4 1 web technology uptu
Unit 4 1 web technology uptuUnit 4 1 web technology uptu
Unit 4 1 web technology uptu
 
Introduction to JSP.pptx
Introduction to JSP.pptxIntroduction to JSP.pptx
Introduction to JSP.pptx
 
JSP Technology I
JSP Technology IJSP Technology I
JSP Technology I
 
JavaScript, often abbreviated as JS, is a programming language and core techn...
JavaScript, often abbreviated as JS, is a programming language and core techn...JavaScript, often abbreviated as JS, is a programming language and core techn...
JavaScript, often abbreviated as JS, is a programming language and core techn...
 
Jsp1
Jsp1Jsp1
Jsp1
 
3.jsp tutorial
3.jsp tutorial3.jsp tutorial
3.jsp tutorial
 
Lap trinh web [Slide jsp]
Lap trinh web [Slide jsp]Lap trinh web [Slide jsp]
Lap trinh web [Slide jsp]
 
Jsp basic
Jsp basicJsp basic
Jsp basic
 
JSP - Part 1
JSP - Part 1JSP - Part 1
JSP - Part 1
 

Plus de Jafar Nesargi

Network adpater,cabel,cards ,types, network devices
Network adpater,cabel,cards ,types, network devicesNetwork adpater,cabel,cards ,types, network devices
Network adpater,cabel,cards ,types, network devicesJafar Nesargi
 
An introduction to networking
An introduction to networkingAn introduction to networking
An introduction to networkingJafar Nesargi
 
Computer basics Intro
Computer basics IntroComputer basics Intro
Computer basics IntroJafar Nesargi
 
Chapter 7 relation database language
Chapter 7 relation database languageChapter 7 relation database language
Chapter 7 relation database languageJafar Nesargi
 
Chapter 6 relational data model and relational
Chapter  6  relational data model and relationalChapter  6  relational data model and relational
Chapter 6 relational data model and relationalJafar Nesargi
 
Chapter 4 record storage and primary file organization
Chapter 4 record storage and primary file organizationChapter 4 record storage and primary file organization
Chapter 4 record storage and primary file organizationJafar Nesargi
 
Introduction to-oracle
Introduction to-oracleIntroduction to-oracle
Introduction to-oracleJafar Nesargi
 
Cascading style sheets
Cascading style sheetsCascading style sheets
Cascading style sheetsJafar Nesargi
 
Session1 gateway to web page development
Session1   gateway to web page developmentSession1   gateway to web page development
Session1 gateway to web page developmentJafar Nesargi
 
Chapter 3 servlet & jsp
Chapter 3 servlet & jspChapter 3 servlet & jsp
Chapter 3 servlet & jspJafar Nesargi
 
Record storage and primary file organization
Record storage and primary file organizationRecord storage and primary file organization
Record storage and primary file organizationJafar Nesargi
 

Plus de Jafar Nesargi (20)

Network adpater,cabel,cards ,types, network devices
Network adpater,cabel,cards ,types, network devicesNetwork adpater,cabel,cards ,types, network devices
Network adpater,cabel,cards ,types, network devices
 
An introduction to networking
An introduction to networkingAn introduction to networking
An introduction to networking
 
Computer basics Intro
Computer basics IntroComputer basics Intro
Computer basics Intro
 
Css
CssCss
Css
 
Chapter 7 relation database language
Chapter 7 relation database languageChapter 7 relation database language
Chapter 7 relation database language
 
Chapter 6 relational data model and relational
Chapter  6  relational data model and relationalChapter  6  relational data model and relational
Chapter 6 relational data model and relational
 
Chapter 4 record storage and primary file organization
Chapter 4 record storage and primary file organizationChapter 4 record storage and primary file organization
Chapter 4 record storage and primary file organization
 
Chapter3
Chapter3Chapter3
Chapter3
 
Introduction to-oracle
Introduction to-oracleIntroduction to-oracle
Introduction to-oracle
 
Chapter2
Chapter2Chapter2
Chapter2
 
Cascading style sheets
Cascading style sheetsCascading style sheets
Cascading style sheets
 
Session1 gateway to web page development
Session1   gateway to web page developmentSession1   gateway to web page development
Session1 gateway to web page development
 
Introduction to jsp
Introduction to jspIntroduction to jsp
Introduction to jsp
 
Chapter 3 servlet & jsp
Chapter 3 servlet & jspChapter 3 servlet & jsp
Chapter 3 servlet & jsp
 
Rmi
RmiRmi
Rmi
 
Java bean
Java beanJava bean
Java bean
 
Networking
NetworkingNetworking
Networking
 
Chapter2 j2ee
Chapter2 j2eeChapter2 j2ee
Chapter2 j2ee
 
Chapter 1 swings
Chapter 1 swingsChapter 1 swings
Chapter 1 swings
 
Record storage and primary file organization
Record storage and primary file organizationRecord storage and primary file organization
Record storage and primary file organization
 

Dernier

Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxPooja Bhuva
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17Celine George
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 

Dernier (20)

Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 

Introduction to jsp

  • 2. JSP and Servlets / Session 5 / 2 of 24 Objectives Explain JSP Identify the advantages of using JSP Describe various elements of JSP Describe the JSP Life Cycle Develop JSP using Java Studio Enterprise 8
  • 3. JSP and Servlets / Session 5 / 3 of 24 Introduction to JSP Java Server Pages (JSP) are saved with the extension .jsp. Efficiently controls dynamic content generation. Uses Java programming language and class libraries. Uses HTML for presentation of pages and Java code to access dynamic content. JSP page JAVA Server JSP uses JAVA to access dynamic content
  • 4. JSP and Servlets / Session 5 / 4 of 24 Benefits of JSP 3-1 Separates content from presentation Request Response JSP page Static Content Dynamic Content Server Client Web Designer JSP Programmer
  • 5. JSP and Servlets / Session 5 / 5 of 24 Benefits of JSP 3-2 Emphasizes reusable components JSP page 1 Static Content Dynamic Content JSP page 2 Static Content Dynamic Content JSP page 3 Static Content Dynamic Content JavaBean Multiple JSP pages use the same JavaBean
  • 6. JSP and Servlets / Session 5 / 6 of 24 Benefits of JSP 3-3 Simplified page development - Web designer and Web programmer use Web development tools to develop a JSP page. JSP page Static Content Dynamic Content Web Designer JSP Programmer Web Development Tools Macromedia Dreamweaver Java Studio Enterprise 8 .....
  • 7. JSP and Servlets / Session 5 / 7 of 24 Elements of JSP 2-1 Elements of a JSP page Static Content Directives Expressions Scriptlets JSP Page Taglib: <%@ taglib uri= "tagLibraryURI" prefix= "tagPrefix" %> <%valid Java code block%> <%= Java Expression %> Page : <%@ page ATTRIBUTES %> Include: <%@ include file = " Filename" %> Declarations Actions <%! declaration(s) %> Forward Include Plug-ins Bean tags
  • 8. JSP and Servlets / Session 5 / 8 of 24 Elements of JSP 2-2 <%@ page language="java" %> <html> … … <h1>Elements of JSP</h1> <p>Today is </p> <page id="clock" class= "calendar.jspCalendar" /> <ul> <li>Day: <%=clock.getDayOfMonth() %> <li>Year: <%=clock.getYear() %> </ul> Page Directive JSP ExpressionStatic Content <% if (Calendar.getInstance().get(Calendar.AM_PM) == Calendar.AM) { %> Good Morning <% } else { %> Good Afternoon <% } %> <%@ include file="copyrightfile.html" %> … … </html> JSP Scriptlet Include Directive
  • 9. JSP and Servlets / Session 5 / 9 of 24 Static Content Static content is the text written on a Web page. Any text-based format can be used to write static content. A page directive is used to specify the format of content. <html> <%@ page contentType = "text/html" %> <head> <title>Example for static content</title> </head> … //Contains text in specified content type. … </body> </html> HTML tags contain static content
  • 10. JSP and Servlets / Session 5 / 10 of 24 JSP Directives JSP container uses directives for processing of JSP page. Controls the structure of the Servlet Provides global information about a JSP page Scope of directives is the entire JSP file <html> ... <%@ page language="Java" import= “java.rmi.*, java.util.*" session="true" buffer="12kb" autoFlush="true" info="PageDirective" errorPage="error.jsp" isErrorPage="false" isThreadSafe="true" %> <head> <title>Testing Page Directive</title> </head> <body> <h1>Testing Page Directive</h1> This page is testing Page Directive. </body > … … </html> page Directive Demonstration: Example 1 <html> <head> <title>Testing include directive</title> </head> <body> <h1> Example of directives</h1> <%@ include file ="testFile.html" %> </body> </html> include Directive // testFile.html <html> <body> The JSP <b>"include"</b> directive example. </body> </html> Demonstration: Example 2
  • 11. JSP and Servlets / Session 5 / 11 of 24 JSP Expression Contains a Java statement Value of Java Statement will be evaluated and inserted into generated Web page. Displays individual variables, or the result of some calculation. <html> <head> <title>Testing Expression directive</title> </head> <body> <h1> Testing Expression directive </h1> <% int i = 2, j=3;%> <% i++;%> <% j=j+i; %> … </body> </html> JSP Expression
  • 12. JSP and Servlets / Session 5 / 12 of 24 JSP Scriptlet Block of Java code that performs functions which are not supported by tags. Executed during run time <html> <head> <title>Scriptlet of a JSP </title> </head> <body> <h1>Scriptlet of a JSP</h1> <% int k=0; for(int i=0;i<5;i++) { k=k+i; System.out.println(k); } %> ... </body> </html> JSP Scriptlet
  • 13. JSP and Servlets / Session 5 / 13 of 24 JSP Declaration Used to define variables and methods in a JSP page. Declared variables and methods can then be referenced by other scripting elements on the same page. Used to define single or multiple variables <%! int x = 0, y = 0; private String units = "ft"; %> Declares x and y as integer variables Declares units as a String variable <%! public long fact (long x) { if (x == 0) return 1; else return x * fact(x-1); } %> Declares fact method
  • 14. JSP and Servlets / Session 5 / 14 of 24 JSP Actions Allows the transfer of control between pages Allows JSP pages to access JavaBeans component objects stored on the server. JSP Actions Forward Include Plug-ins Bean tags
  • 15. JSP and Servlets / Session 5 / 15 of 24 Forward Action Permanently transfers control from a JSP page to another location. JSP Page 1 Forward Action JSP Page 2 Transfers control to another page
  • 16. JSP and Servlets / Session 5 / 16 of 24 Include Action 2-1 Inserts the content generated by remote resource in the output of the current JSP page. Temporarily transfers the control to a new page JSP Page 1 Include Action JSP Page 2 Transfers control to another page Transfers control back to the original page
  • 17. JSP and Servlets / Session 5 / 17 of 24 Include Action 2-2 <%@page contentType="text/html"%> <%@page pageEncoding="UTF-8"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>jspactionexample</title> </head> <body bgcolor="#ffffff"> <h1>Jsp actions and declarations</h1> <jsp:include flush="true" page="JspExample.jsp"> <jsp:param name="stringeg" value="String passed using forward directive"/> </jsp:include> </body> </html> Include Action Demonstration: Example 3 <%@page contentType="text/html"%> <%@page pageEncoding="UTF-8"%> <%@page language="java" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Declaration, Expression and Actions</title> <%=request.getParameter("stringeg")%> <%! static private char[] vowels ={ 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}; String word; public boolean startsWithVowel(String word) { this.word=word; char first = word.charAt(0); for (int i = 0; i < vowels.length; ++i) { if (first == vowels[i]) return true; } return false; } JSP Declarations static private String[] articles = { "a ", "an " }; public String withArticle(String noun) { if (startsWithVowel(noun)) return articles[1] + noun; else return articles[0] + noun; } %> </head> <body bgcolor="#ffffff"> <br /> <b>Starts with a vowel: </b> <%=startsWithVowel("I am a very good Programmer")%> <br/> <b>String entered is: </b> <%=word%> <br /> <b>Article:</b> <%=withArticle("apple")%> <br /> </body> </html> JSP Declarations Demonstration: Example 4
  • 18. JSP and Servlets / Session 5 / 18 of 24 JSP Plug-ins and Bean Tags JSP Plug-ins - Used to generate browser-specific HTML. JSP Bean Tags – Used to interact with JavaBeans stored on the server.
  • 19. JSP and Servlets / Session 5 / 19 of 24 JSP Life Cycle 3-1 Client JSP page 3 Execution 2 Translation Compilation 1 Servlet Request Server Request ResponseResponse JSP Life Cycle
  • 20. JSP and Servlets / Session 5 / 20 of 24 JSP Life Cycle 3-2 Translation and Compilation Translation Compilation Servlet JSP Determines Errors in JSP Extracts data from JSP element Generates a Servlet for JSP
  • 21. JSP and Servlets / Session 5 / 21 of 24 JSP Life Cycle 3-3 Execution – Actions performed during execution are: <%@ page buffer = "none|20kb" %> <%@ page errorPage = "errorJSP.html" %> Handling Errors Sets buffer size Specifies error page Buffering Output
  • 22. JSP and Servlets / Session 5 / 22 of 24 Demonstration: Example 5 <%@page contentType="text/html"%> <%@page pageEncoding="UTF-8"%> <%! double radius=6.0; private double getRadius(){ return radius; } private double getDiameter(){ return (radius * 2); } private double getArea(){ return (3.1415 * radius); } private double getCircumference(){ return(3.1415 *(radius * 2)); } %> JSP declarations JSP Application in Java Studio Enterprise 8 <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Declaration Tag - Methods</title> </head> <h3>Calculating area and circumference of a Circle</h3> <hr/> <b>Radius of circle:</b> <%=radius%> cm<br/> <b>Diameter:</b> <%=getDiameter()%> cm<br/> <b>Area of Circle is:</b> <%=getArea()%> cm<sup>2</sup><br/> <b>Circumference of a circle is:</b> <%=getCircumference()%><br/> <hr/> <body></body> </html> JSP scriptlets
  • 23. JSP and Servlets / Session 5 / 23 of 24 Summary  JSP uses Java programming language and class libraries.  JSP page uses HTML to display static text and Java code to generate dynamic content.  Elements of JSP page are static content, JSP directives, JSP expressions, and JSP scriptlets.  A JSP page can be created using standard development tools.  JSP uses reusable and cross-platform components, such as JavaBeans.  JSP allows the creation of user-defined tags and makes the JSP development process easy.  Different phases in JSP life cycle are translation, compilation, and execution.