SlideShare a Scribd company logo
Java JSP Training
JSP Part 1
Page 1Classification: Restricted
Copyright @ 2000 Jordan Anastasiade. All rights reserved. 1
Agenda
• JSP (Java Server Pages Technology)
• JSP vs Servlet
• MVC Architecture
• Scriplet
Page 2Classification: Restricted
Copyright @ 2000 Jordan Anastasiade. All rights reserved. 2
JavaServer Pages Technology
• JavaServer Pages (JSP) technology provides a simplified, fast way to create
web pages that display dynamically-generated content.
• Server Page technology = the web pages are dynamically built on the server
side. E.g. JSP, ASP, BSP
Page 3Classification: Restricted
Copyright @ 2000 Jordan Anastasiade. All rights reserved. 3
JSP vs Servlet
Servlet = HTML in a Java class
JSP = Java in an HTML page
out.println(“<h1> Hello World! </h1>”);
<%= request.getParameter("title") %>
MVC Architecture
Page 5Classification: Restricted
Model 1 Architecture
Page 6Classification: Restricted
Model 2 Architecture
(also known as MVC or Model-View-Controller)
Single Responsibility Principle,
Separation of Concerns.
Page 7Classification: Restricted
Copyright @ 2000 Jordan Anastasiade. All rights reserved. 7
JSP Page
• A JSP page is a page created by the web developer that includes JSP
technology-specific tags, declarations, and possibly scriptlets, in
combination with other static HTML or XML tags.
• A JSP page has the extension .jsp; this signals to the web server that the JSP
engine will process elements on this page.
• Pages built using JSP technology are typically implemented using a
translation phase that is performed once, the first time the page is called.
The page is compiled into a Java Servlet class and remains in server
memory, so subsequent calls to the page have very fast response times.
Page 8Classification: Restricted
Copyright @ 2000 Jordan Anastasiade. All rights reserved. 8
Overview
• JavaServer Pages (JSP) lets you separate the dynamic part of your pages
from the static HTML.
HTML tags and text
<% some JSP code here %>
HTML tags and text
<I>
<%= request.getParameter("title") %>
</I>
You normally give your file a .jsp extension, and typically install it in any
place you could place a normal Web page
Page 9Classification: Restricted
Copyright @ 2000 Jordan Anastasiade. All rights reserved. 9
Client and Server with JSP
Page 10Classification: Restricted
Copyright @ 2000 Jordan Anastasiade. All rights reserved. 10
Translation Time
• A JSP application is usually a collection of JSP files, HTML files, graphics
and other resources.
• A JSP page is compiled when your user loads it into a Web browser
1. When the user loads the page for the first time, the files that make up the
application are all translated together, without any dynamic data, into
one Java source file (a .java file)
2. The .java file is compiled to a .class file. In most implementations, the
.java file is a Java servlet that complies with the Java Servlet API.
Page 11Classification: Restricted
Copyright @ 2000 Jordan Anastasiade. All rights reserved. 11
Simple JSP Page
<%@ page info=“A Simple JSP Sample” %>
<HTML>
<H1> First JSP Page </H1>
<BODY>
<% out.println(“Welcome to JSP world”); %>
</BODY>
</HTML>
Page 12Classification: Restricted
Copyright @ 2000 Jordan Anastasiade. All rights reserved. 12
How JSP Works?
User Request – JSP File Requested
Server
File
ChangedCreate Source from JSP
Compile Execute Servlet
Page 13Classification: Restricted
Review of Life Cycle of Servlet
Page 14Classification: Restricted
JSP Lifecycle
1.Translation (JSP
 Servlet)
2.Compilation
(Servlet
compiled)
3.Loading
4.Instantiation
5.Initialization
6.Request
Processing
7.Destruction
Page 15Classification: Restricted
Copyright @ 2000 Jordan Anastasiade. All rights reserved. 15
JSP Elements
• Declarations <%! code %>
• Expressions <%= expression %>
• Scriptlets <% code %>
Page 16Classification: Restricted
Copyright @ 2000 Jordan Anastasiade. All rights reserved. 16
HTML Comment
• Generates a comment that is sent to the client.
• Syntax
<!-- comment [ <%= expression %> ] -->
• Example:
<!-- This page was loaded on
<%= (new java.util.Date()).toLocaleString() %>
-->
Page 17Classification: Restricted
Copyright @ 2000 Jordan Anastasiade. All rights reserved. 17
Declaration
• Declares a variable or method valid in the scripting language used in the JSP page.
• Syntax
<%! declaration; [ declaration; ]+ ... %>
• Examples
<%! String destin; %>
<%! public String getDestination()
{return destin;}%>
<%! Circle a = new Circle(2.0); %>
• You can declare any number of variables or methods within one declaration
element, as long as you end each declaration with a semicolon.
• The declaration must be valid in the Java programming language.
Page 18Classification: Restricted
Copyright @ 2000 Jordan Anastasiade. All rights reserved. 18
Declaration Example: Variables declaration
<HTML>
<HEAD><TITLE>JSP Declarations</TITLE></HEAD>
<BODY><H1>JSP Declarations</H1>
<%! private int keepCount = 0; %>
<H2>
Page accessed:
<%= ++keepCount %>
times
</H2>
</BODY>
</HTML>
Best Practice: Never use instance
variables in Servlets. (Multithreading)
Page 19Classification: Restricted
Declaration example: Methods declaration
<html>
<head>
<title>Methods Declaration</title>
</head>
<body>
<%!
int sum(int num1, int num2, int num3){
return num1+num2+num3;
}
%>
<%= "Result is: " + sum(10,40,50) %>
</body>
</html>
Page 20Classification: Restricted
Copyright @ 2000 Jordan Anastasiade. All rights reserved. 20
Predefined Variable – Implicit Objects
• request – Object of HttpServletRequest (request parameters, HTTP headers, cookies
• response – Object of HttpServletResponse
• out - Object of PrintWriter buffered version JspWriter
• session - Object of HttpSession associated with the request
• application - Object of ServletContext shared by all servlets in the engine
• config - Object of ServletConfig
• pageContext - Object of PageContext in JSP for a single point of access
e.g. pageContext.getSession()
• page – variable synonym for this object
e.g. <%= ((Servlet)page).getServletInfo () %>
Page 21Classification: Restricted
Copyright @ 2000 Jordan Anastasiade. All rights reserved. 21
Expression
• Contains an expression valid in the scripting language used in the JSP page. Syntax:
<%= expression %>
<%! String name = new String(“JSP World”); %>
<%! public String getName() { return name; } %>
<B><%= getName() %></B>
• Description:
An expression element contains a scripting language expression that is
evaluated, converted to a String, and inserted where the expression appears in
the JSP file.
• Because the value of an expression is converted to a String, you can use an
expression within a line of text, whether or not it is tagged with HTML, in a
JSPfile. Expressions are evaluated from left to right.
Page 22Classification: Restricted
Copyright @ 2000 Jordan Anastasiade. All rights reserved. 22
Expression Example
<HTML>
<HEAD>
<TITLE>JSP Expressions</TITLE>
</HEAD>
<BODY>
<H2>JSP Expressions</H2>
<UL>
<LI>Current time: <%= new java.util.Date() %>
<LI>Your hostname: <%= request.getRemoteHost()%>
<LI>Your session ID: <%= session.getId() %>
</UL>
</BODY>
</HTML>
Page 23Classification: Restricted
Expression Example 2
<html>
<head>
<title>JSP expression tag example1</title>
</head>
<body>
<%= 2+4*5 %>
</body>
</html>
Page 24Classification: Restricted
Expression Example 3
<html>
<head>
<title>JSP expression tag example2</title>
</head>
<body>
<%
int a=10;
int b=20;
int c=30;
%>
<%= a+b+c %>
</body>
</html>
Page 25Classification: Restricted
Expression Example 4
index.jsp
<html>
<head>
<title> JSP expression tag example3 </title>
</head>
<body>
<% application.setAttribute("MyName", "Chaitanya"); %>
<a href="display.jsp">Click here for display</a>
</body>
</html>
display.jsp
<html>
<head>
<title>Display Page</title>
</head>
<body>
<%="This is a String" %><br>
<%= application.getAttribute("MyName") %>
</body>
</html>
Page 26Classification: Restricted
Copyright @ 2000 Jordan Anastasiade. All rights reserved. 26
Scriptlet
• Contains a code fragment valid in the page scripting language.
• Syntax
<% code fragment %>
<%
String var1 = request.getParameter("name");
out.println(var1);
%>
• This code will be placed in the generated servlet method:
_jspService()
Page 27Classification: Restricted
Copyright @ 2000 Jordan Anastasiade. All rights reserved. 27
Scriplet Example
<HTML>
<HEAD><TITLE>Weather</TITLE></HEAD>
<BODY>
<H2>Today's weather</H2>
<% if (Math.random() < 0.5) { %>
Today will be a <B>sunny</B> day!
<% } else { %>
Today will be a <B>windy</B> day!
<% } %>
</BODY>
</HTML>
Page 28Classification: Restricted
Copyright @ 2000 Jordan Anastasiade. All rights reserved. 28
Topics to be covered in next session
• JSP vs Servlet
• LifeCycle of Servlet
• JSP Elements
• JSP Page directive
• Directives vs Action tags
Page 29Classification: Restricted
Copyright @ 2000 Jordan Anastasiade. All rights reserved. 29
Thank you!

More Related Content

PPSX
Hibernate - Part 2
Hitesh-Java
 
PPSX
Spring - Part 3 - AOP
Hitesh-Java
 
PPSX
JSP - Part 2 (Final)
Hitesh-Java
 
PPSX
Java IO, Serialization
Hitesh-Java
 
PPSX
Struts 2 - Introduction
Hitesh-Java
 
PPSX
Hibernate - Part 1
Hitesh-Java
 
PPSX
Struts 2 - Hibernate Integration
Hitesh-Java
 
PPTX
Session 40 - Hibernate - Part 2
PawanMM
 
Hibernate - Part 2
Hitesh-Java
 
Spring - Part 3 - AOP
Hitesh-Java
 
JSP - Part 2 (Final)
Hitesh-Java
 
Java IO, Serialization
Hitesh-Java
 
Struts 2 - Introduction
Hitesh-Java
 
Hibernate - Part 1
Hitesh-Java
 
Struts 2 - Hibernate Integration
Hitesh-Java
 
Session 40 - Hibernate - Part 2
PawanMM
 

What's hot (18)

PDF
Hibernate Interview Questions
Syed Shahul
 
PPTX
Session 38 - Core Java (New Features) - Part 1
PawanMM
 
PPSX
JDBC Part - 2
Hitesh-Java
 
PPTX
Hibernate in Action
Akshay Ballarpure
 
DOC
24 collections framework interview questions
Arun Vasanth
 
PDF
Hibernate Presentation
guest11106b
 
PPTX
Session 43 - Spring - Part 1 - IoC DI Beans
PawanMM
 
ODP
Hibernate Developer Reference
Muthuselvam RS
 
PDF
Bea weblogic job_interview_preparation_guide
Pankaj Singh
 
PPSX
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slides
Hitesh-Java
 
DOCX
Hibernate3 q&a
Faruk Molla
 
PPSX
Elements of Java Language
Hitesh-Java
 
PPTX
Hibernate ppt
Aneega
 
PPTX
Spring & hibernate
Santosh Kumar Kar
 
PPS
Java Hibernate Programming with Architecture Diagram and Example
kamal kotecha
 
PPT
Hibernate presentation
Manav Prasad
 
PPS
Jdbc api
kamal kotecha
 
PPTX
Hibernate tutorial
Mumbai Academisc
 
Hibernate Interview Questions
Syed Shahul
 
Session 38 - Core Java (New Features) - Part 1
PawanMM
 
JDBC Part - 2
Hitesh-Java
 
Hibernate in Action
Akshay Ballarpure
 
24 collections framework interview questions
Arun Vasanth
 
Hibernate Presentation
guest11106b
 
Session 43 - Spring - Part 1 - IoC DI Beans
PawanMM
 
Hibernate Developer Reference
Muthuselvam RS
 
Bea weblogic job_interview_preparation_guide
Pankaj Singh
 
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slides
Hitesh-Java
 
Hibernate3 q&a
Faruk Molla
 
Elements of Java Language
Hitesh-Java
 
Hibernate ppt
Aneega
 
Spring & hibernate
Santosh Kumar Kar
 
Java Hibernate Programming with Architecture Diagram and Example
kamal kotecha
 
Hibernate presentation
Manav Prasad
 
Jdbc api
kamal kotecha
 
Hibernate tutorial
Mumbai Academisc
 
Ad

Similar to JSP - Part 1 (20)

PPTX
Session 36 - JSP - Part 1
PawanMM
 
PPT
Java serverpages
Amit Kumar
 
PPT
JSP Part 1
DeeptiJava
 
PPTX
Java Server Pages
Shah Nawaz Bhurt
 
PPT
Jsp sasidhar
Sasidhar Kothuru
 
PPTX
JSP.pptx
NishaRohit6
 
PPTX
Web programming-Introduction to JSP.pptx
mcjaya2024
 
PPTX
Jsp
Pooja Verma
 
PPTX
JSP - Java Server Page
Vipin Yadav
 
PPTX
JSP- JAVA SERVER PAGES
Yoga Raja
 
PPS
Jsp element
kamal kotecha
 
PPT
Atul & shubha goswami jsp
Atul Giri
 
PPTX
JSP AND XML USING JAVA WITH GET AND POST METHODS
bharathiv53
 
PPTX
Introduction - Java Server Programming (JSP)
PadmavathiKPSGCAS
 
PPT
Jsp intro
husnara mohammad
 
PPT
Jsp ppt
Vikas Jagtap
 
PPTX
JAVA SERVER PAGES
Kalpana T
 
PPTX
WT Unit-Vuufvmjn dissimilating Dunkirk k
asta9578
 
Session 36 - JSP - Part 1
PawanMM
 
Java serverpages
Amit Kumar
 
JSP Part 1
DeeptiJava
 
Java Server Pages
Shah Nawaz Bhurt
 
Jsp sasidhar
Sasidhar Kothuru
 
JSP.pptx
NishaRohit6
 
Web programming-Introduction to JSP.pptx
mcjaya2024
 
JSP - Java Server Page
Vipin Yadav
 
JSP- JAVA SERVER PAGES
Yoga Raja
 
Jsp element
kamal kotecha
 
Atul & shubha goswami jsp
Atul Giri
 
JSP AND XML USING JAVA WITH GET AND POST METHODS
bharathiv53
 
Introduction - Java Server Programming (JSP)
PadmavathiKPSGCAS
 
Jsp intro
husnara mohammad
 
Jsp ppt
Vikas Jagtap
 
JAVA SERVER PAGES
Kalpana T
 
WT Unit-Vuufvmjn dissimilating Dunkirk k
asta9578
 
Ad

More from Hitesh-Java (20)

PPSX
Spring - Part 4 - Spring MVC
Hitesh-Java
 
PPSX
Spring - Part 1 - IoC, Di and Beans
Hitesh-Java
 
PPSX
JDBC
Hitesh-Java
 
PPSX
Inner Classes
Hitesh-Java
 
PPSX
Collections - Maps
Hitesh-Java
 
PPSX
Review Session - Part -2
Hitesh-Java
 
PPSX
Review Session and Attending Java Interviews
Hitesh-Java
 
PPSX
Collections - Lists, Sets
Hitesh-Java
 
PPSX
Collections - Sorting, Comparing Basics
Hitesh-Java
 
PPSX
Collections - Array List
Hitesh-Java
 
PPSX
Object Class
Hitesh-Java
 
PPSX
Exception Handling - Continued
Hitesh-Java
 
PPSX
Exception Handling - Part 1
Hitesh-Java
 
PPSX
OOPs with Java - Packaging and Access Modifiers
Hitesh-Java
 
PPSX
OOP with Java - Abstract Classes and Interfaces
Hitesh-Java
 
PPSX
OOP with Java - Part 3
Hitesh-Java
 
PPSX
OOP with Java - Continued
Hitesh-Java
 
PPSX
Intro to Object Oriented Programming with Java
Hitesh-Java
 
PPSX
Practice Session
Hitesh-Java
 
PPSX
Strings in Java
Hitesh-Java
 
Spring - Part 4 - Spring MVC
Hitesh-Java
 
Spring - Part 1 - IoC, Di and Beans
Hitesh-Java
 
Inner Classes
Hitesh-Java
 
Collections - Maps
Hitesh-Java
 
Review Session - Part -2
Hitesh-Java
 
Review Session and Attending Java Interviews
Hitesh-Java
 
Collections - Lists, Sets
Hitesh-Java
 
Collections - Sorting, Comparing Basics
Hitesh-Java
 
Collections - Array List
Hitesh-Java
 
Object Class
Hitesh-Java
 
Exception Handling - Continued
Hitesh-Java
 
Exception Handling - Part 1
Hitesh-Java
 
OOPs with Java - Packaging and Access Modifiers
Hitesh-Java
 
OOP with Java - Abstract Classes and Interfaces
Hitesh-Java
 
OOP with Java - Part 3
Hitesh-Java
 
OOP with Java - Continued
Hitesh-Java
 
Intro to Object Oriented Programming with Java
Hitesh-Java
 
Practice Session
Hitesh-Java
 
Strings in Java
Hitesh-Java
 

Recently uploaded (20)

PPTX
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
PDF
Software Development Methodologies in 2025
KodekX
 
PDF
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PDF
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
PDF
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
PPTX
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PDF
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
PPTX
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
PPTX
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PDF
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
Software Development Methodologies in 2025
KodekX
 
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 

JSP - Part 1

  • 2. Page 1Classification: Restricted Copyright @ 2000 Jordan Anastasiade. All rights reserved. 1 Agenda • JSP (Java Server Pages Technology) • JSP vs Servlet • MVC Architecture • Scriplet
  • 3. Page 2Classification: Restricted Copyright @ 2000 Jordan Anastasiade. All rights reserved. 2 JavaServer Pages Technology • JavaServer Pages (JSP) technology provides a simplified, fast way to create web pages that display dynamically-generated content. • Server Page technology = the web pages are dynamically built on the server side. E.g. JSP, ASP, BSP
  • 4. Page 3Classification: Restricted Copyright @ 2000 Jordan Anastasiade. All rights reserved. 3 JSP vs Servlet Servlet = HTML in a Java class JSP = Java in an HTML page out.println(“<h1> Hello World! </h1>”); <%= request.getParameter("title") %>
  • 7. Page 6Classification: Restricted Model 2 Architecture (also known as MVC or Model-View-Controller) Single Responsibility Principle, Separation of Concerns.
  • 8. Page 7Classification: Restricted Copyright @ 2000 Jordan Anastasiade. All rights reserved. 7 JSP Page • A JSP page is a page created by the web developer that includes JSP technology-specific tags, declarations, and possibly scriptlets, in combination with other static HTML or XML tags. • A JSP page has the extension .jsp; this signals to the web server that the JSP engine will process elements on this page. • Pages built using JSP technology are typically implemented using a translation phase that is performed once, the first time the page is called. The page is compiled into a Java Servlet class and remains in server memory, so subsequent calls to the page have very fast response times.
  • 9. Page 8Classification: Restricted Copyright @ 2000 Jordan Anastasiade. All rights reserved. 8 Overview • JavaServer Pages (JSP) lets you separate the dynamic part of your pages from the static HTML. HTML tags and text <% some JSP code here %> HTML tags and text <I> <%= request.getParameter("title") %> </I> You normally give your file a .jsp extension, and typically install it in any place you could place a normal Web page
  • 10. Page 9Classification: Restricted Copyright @ 2000 Jordan Anastasiade. All rights reserved. 9 Client and Server with JSP
  • 11. Page 10Classification: Restricted Copyright @ 2000 Jordan Anastasiade. All rights reserved. 10 Translation Time • A JSP application is usually a collection of JSP files, HTML files, graphics and other resources. • A JSP page is compiled when your user loads it into a Web browser 1. When the user loads the page for the first time, the files that make up the application are all translated together, without any dynamic data, into one Java source file (a .java file) 2. The .java file is compiled to a .class file. In most implementations, the .java file is a Java servlet that complies with the Java Servlet API.
  • 12. Page 11Classification: Restricted Copyright @ 2000 Jordan Anastasiade. All rights reserved. 11 Simple JSP Page <%@ page info=“A Simple JSP Sample” %> <HTML> <H1> First JSP Page </H1> <BODY> <% out.println(“Welcome to JSP world”); %> </BODY> </HTML>
  • 13. Page 12Classification: Restricted Copyright @ 2000 Jordan Anastasiade. All rights reserved. 12 How JSP Works? User Request – JSP File Requested Server File ChangedCreate Source from JSP Compile Execute Servlet
  • 14. Page 13Classification: Restricted Review of Life Cycle of Servlet
  • 15. Page 14Classification: Restricted JSP Lifecycle 1.Translation (JSP  Servlet) 2.Compilation (Servlet compiled) 3.Loading 4.Instantiation 5.Initialization 6.Request Processing 7.Destruction
  • 16. Page 15Classification: Restricted Copyright @ 2000 Jordan Anastasiade. All rights reserved. 15 JSP Elements • Declarations <%! code %> • Expressions <%= expression %> • Scriptlets <% code %>
  • 17. Page 16Classification: Restricted Copyright @ 2000 Jordan Anastasiade. All rights reserved. 16 HTML Comment • Generates a comment that is sent to the client. • Syntax <!-- comment [ <%= expression %> ] --> • Example: <!-- This page was loaded on <%= (new java.util.Date()).toLocaleString() %> -->
  • 18. Page 17Classification: Restricted Copyright @ 2000 Jordan Anastasiade. All rights reserved. 17 Declaration • Declares a variable or method valid in the scripting language used in the JSP page. • Syntax <%! declaration; [ declaration; ]+ ... %> • Examples <%! String destin; %> <%! public String getDestination() {return destin;}%> <%! Circle a = new Circle(2.0); %> • You can declare any number of variables or methods within one declaration element, as long as you end each declaration with a semicolon. • The declaration must be valid in the Java programming language.
  • 19. Page 18Classification: Restricted Copyright @ 2000 Jordan Anastasiade. All rights reserved. 18 Declaration Example: Variables declaration <HTML> <HEAD><TITLE>JSP Declarations</TITLE></HEAD> <BODY><H1>JSP Declarations</H1> <%! private int keepCount = 0; %> <H2> Page accessed: <%= ++keepCount %> times </H2> </BODY> </HTML> Best Practice: Never use instance variables in Servlets. (Multithreading)
  • 20. Page 19Classification: Restricted Declaration example: Methods declaration <html> <head> <title>Methods Declaration</title> </head> <body> <%! int sum(int num1, int num2, int num3){ return num1+num2+num3; } %> <%= "Result is: " + sum(10,40,50) %> </body> </html>
  • 21. Page 20Classification: Restricted Copyright @ 2000 Jordan Anastasiade. All rights reserved. 20 Predefined Variable – Implicit Objects • request – Object of HttpServletRequest (request parameters, HTTP headers, cookies • response – Object of HttpServletResponse • out - Object of PrintWriter buffered version JspWriter • session - Object of HttpSession associated with the request • application - Object of ServletContext shared by all servlets in the engine • config - Object of ServletConfig • pageContext - Object of PageContext in JSP for a single point of access e.g. pageContext.getSession() • page – variable synonym for this object e.g. <%= ((Servlet)page).getServletInfo () %>
  • 22. Page 21Classification: Restricted Copyright @ 2000 Jordan Anastasiade. All rights reserved. 21 Expression • Contains an expression valid in the scripting language used in the JSP page. Syntax: <%= expression %> <%! String name = new String(“JSP World”); %> <%! public String getName() { return name; } %> <B><%= getName() %></B> • Description: An expression element contains a scripting language expression that is evaluated, converted to a String, and inserted where the expression appears in the JSP file. • Because the value of an expression is converted to a String, you can use an expression within a line of text, whether or not it is tagged with HTML, in a JSPfile. Expressions are evaluated from left to right.
  • 23. Page 22Classification: Restricted Copyright @ 2000 Jordan Anastasiade. All rights reserved. 22 Expression Example <HTML> <HEAD> <TITLE>JSP Expressions</TITLE> </HEAD> <BODY> <H2>JSP Expressions</H2> <UL> <LI>Current time: <%= new java.util.Date() %> <LI>Your hostname: <%= request.getRemoteHost()%> <LI>Your session ID: <%= session.getId() %> </UL> </BODY> </HTML>
  • 24. Page 23Classification: Restricted Expression Example 2 <html> <head> <title>JSP expression tag example1</title> </head> <body> <%= 2+4*5 %> </body> </html>
  • 25. Page 24Classification: Restricted Expression Example 3 <html> <head> <title>JSP expression tag example2</title> </head> <body> <% int a=10; int b=20; int c=30; %> <%= a+b+c %> </body> </html>
  • 26. Page 25Classification: Restricted Expression Example 4 index.jsp <html> <head> <title> JSP expression tag example3 </title> </head> <body> <% application.setAttribute("MyName", "Chaitanya"); %> <a href="display.jsp">Click here for display</a> </body> </html> display.jsp <html> <head> <title>Display Page</title> </head> <body> <%="This is a String" %><br> <%= application.getAttribute("MyName") %> </body> </html>
  • 27. Page 26Classification: Restricted Copyright @ 2000 Jordan Anastasiade. All rights reserved. 26 Scriptlet • Contains a code fragment valid in the page scripting language. • Syntax <% code fragment %> <% String var1 = request.getParameter("name"); out.println(var1); %> • This code will be placed in the generated servlet method: _jspService()
  • 28. Page 27Classification: Restricted Copyright @ 2000 Jordan Anastasiade. All rights reserved. 27 Scriplet Example <HTML> <HEAD><TITLE>Weather</TITLE></HEAD> <BODY> <H2>Today's weather</H2> <% if (Math.random() < 0.5) { %> Today will be a <B>sunny</B> day! <% } else { %> Today will be a <B>windy</B> day! <% } %> </BODY> </HTML>
  • 29. Page 28Classification: Restricted Copyright @ 2000 Jordan Anastasiade. All rights reserved. 28 Topics to be covered in next session • JSP vs Servlet • LifeCycle of Servlet • JSP Elements • JSP Page directive • Directives vs Action tags
  • 30. Page 29Classification: Restricted Copyright @ 2000 Jordan Anastasiade. All rights reserved. 29 Thank you!