SlideShare ist ein Scribd-Unternehmen logo
1 von 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.

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (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
 

Ähnlich wie Introduction to jsp

Ähnlich wie 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
 

Mehr von 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
 

Mehr von 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
 

Kürzlich hochgeladen

Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 

Kürzlich hochgeladen (20)

INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 

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.