SlideShare uma empresa Scribd logo
1 de 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.

Mais conteúdo relacionado

Mais procurados

Mais procurados (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
 

Semelhante a Introduction to jsp

Semelhante a 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
 

Mais 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
 

Mais 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
 

Último

ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdfMr Bounab Samir
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17Celine George
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxkarenfajardo43
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptxmary850239
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
Multi Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleMulti Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleCeline George
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxSayali Powar
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Developmentchesterberbo7
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...DhatriParmar
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQuiz Club NITW
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseCeline George
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataBabyAnnMotar
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWQuiz Club NITW
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 

Último (20)

ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
prashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Professionprashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Profession
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdf
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
Multi Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleMulti Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP Module
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Development
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
 
Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 Database
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped data
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITW
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 

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.