SlideShare a Scribd company logo
Java & JEE Training
Session 36 – 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

PPTX
Session 33 - Session Management using other Techniques
PPTX
Session 34 - JDBC Best Practices, Introduction to Design Patterns
PPTX
Session 31 - Session Management, Best Practices, Design Patterns in Web Apps
PPTX
Session 37 - JSP - Part 2 (final)
PPTX
Session 35 - Design Patterns
PPTX
Session 30 - Servlets - Part 6
PPTX
Session 39 - Hibernate - Part 1
PPTX
Session 32 - Session Management using Cookies
Session 33 - Session Management using other Techniques
Session 34 - JDBC Best Practices, Introduction to Design Patterns
Session 31 - Session Management, Best Practices, Design Patterns in Web Apps
Session 37 - JSP - Part 2 (final)
Session 35 - Design Patterns
Session 30 - Servlets - Part 6
Session 39 - Hibernate - Part 1
Session 32 - Session Management using Cookies

What's hot (19)

PPTX
Session 25 - Introduction to JEE, Servlets
PPTX
Session 28 - Servlets - Part 4
PPTX
Session 29 - Servlets - Part 5
PPTX
Session 26 - Servlets Part 2
PPTX
Session 40 - Hibernate - Part 2
PPSX
JSP - Part 2 (Final)
PPSX
Spring - Part 4 - Spring MVC
PPSX
Struts 2 - Introduction
PPSX
JSP - Part 1
PPSX
Hibernate - Part 2
PPTX
Session 43 - Spring - Part 1 - IoC DI Beans
PPTX
Session 38 - Core Java (New Features) - Part 1
PDF
Jsp & Ajax
PDF
Lecture 2: Servlets
PDF
Java Web Programming [4/9] : JSP Basic
PDF
jsf2 Notes
PDF
Java Web Programming [3/9] : Servlet Advanced
PDF
Enterprise java unit-3_chapter-1-jsp
PPSX
Struts 2 - Hibernate Integration
Session 25 - Introduction to JEE, Servlets
Session 28 - Servlets - Part 4
Session 29 - Servlets - Part 5
Session 26 - Servlets Part 2
Session 40 - Hibernate - Part 2
JSP - Part 2 (Final)
Spring - Part 4 - Spring MVC
Struts 2 - Introduction
JSP - Part 1
Hibernate - Part 2
Session 43 - Spring - Part 1 - IoC DI Beans
Session 38 - Core Java (New Features) - Part 1
Jsp & Ajax
Lecture 2: Servlets
Java Web Programming [4/9] : JSP Basic
jsf2 Notes
Java Web Programming [3/9] : Servlet Advanced
Enterprise java unit-3_chapter-1-jsp
Struts 2 - Hibernate Integration
Ad

Similar to Session 36 - JSP - Part 1 (20)

PPT
Java serverpages
PPT
JSP Part 1
PPT
Jsp sasidhar
PPTX
Java Server Pages
PPTX
Web programming-Introduction to JSP.pptx
PPTX
JSP.pptx
PPTX
PPTX
JSP - Java Server Page
PPTX
JSP- JAVA SERVER PAGES
PPTX
JSP AND XML USING JAVA WITH GET AND POST METHODS
PPTX
Introduction - Java Server Programming (JSP)
PPS
Jsp element
PPT
Atul & shubha goswami jsp
PPT
Jsp intro
PPTX
WT Unit-Vuufvmjn dissimilating Dunkirk k
PPTX
JAVA SERVER PAGES
PPTX
SCWCD : Java server pages CHAP : 9
PDF
Lap trinh web [Slide jsp]
Java serverpages
JSP Part 1
Jsp sasidhar
Java Server Pages
Web programming-Introduction to JSP.pptx
JSP.pptx
JSP - Java Server Page
JSP- JAVA SERVER PAGES
JSP AND XML USING JAVA WITH GET AND POST METHODS
Introduction - Java Server Programming (JSP)
Jsp element
Atul & shubha goswami jsp
Jsp intro
WT Unit-Vuufvmjn dissimilating Dunkirk k
JAVA SERVER PAGES
SCWCD : Java server pages CHAP : 9
Lap trinh web [Slide jsp]
Ad

More from PawanMM (15)

PPTX
Session 48 - JS, JSON and AJAX
PPTX
Session 46 - Spring - Part 4 - Spring MVC
PPTX
Session 45 - Spring - Part 3 - AOP
PPTX
Session 44 - Spring - Part 2 - Autowiring, Annotations, Java based Configuration
PPTX
Session 42 - Struts 2 Hibernate Integration
PPTX
Session 41 - Struts 2 Introduction
PPTX
Session 24 - JDBC, Intro to Enterprise Java
PPTX
Session 23 - JDBC
PPTX
Session 22 - Java IO, Serialization
PPTX
Session 21 - Inner Classes
PPTX
Session 20 - Collections - Maps
PPTX
Session 19 - Review Session
PPTX
Session 18 - Review Session and Attending Java Interviews
PPTX
Session 17 - Collections - Lists, Sets
PPTX
Session 16 - Collections - Sorting, Comparing Basics
Session 48 - JS, JSON and AJAX
Session 46 - Spring - Part 4 - Spring MVC
Session 45 - Spring - Part 3 - AOP
Session 44 - Spring - Part 2 - Autowiring, Annotations, Java based Configuration
Session 42 - Struts 2 Hibernate Integration
Session 41 - Struts 2 Introduction
Session 24 - JDBC, Intro to Enterprise Java
Session 23 - JDBC
Session 22 - Java IO, Serialization
Session 21 - Inner Classes
Session 20 - Collections - Maps
Session 19 - Review Session
Session 18 - Review Session and Attending Java Interviews
Session 17 - Collections - Lists, Sets
Session 16 - Collections - Sorting, Comparing Basics

Recently uploaded (20)

PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
Big Data Technologies - Introduction.pptx
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
cuic standard and advanced reporting.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Electronic commerce courselecture one. Pdf
PDF
Encapsulation theory and applications.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
MYSQL Presentation for SQL database connectivity
DOCX
The AUB Centre for AI in Media Proposal.docx
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Per capita expenditure prediction using model stacking based on satellite ima...
Dropbox Q2 2025 Financial Results & Investor Presentation
NewMind AI Weekly Chronicles - August'25 Week I
Building Integrated photovoltaic BIPV_UPV.pdf
Big Data Technologies - Introduction.pptx
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
cuic standard and advanced reporting.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Chapter 3 Spatial Domain Image Processing.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Understanding_Digital_Forensics_Presentation.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Electronic commerce courselecture one. Pdf
Encapsulation theory and applications.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
MYSQL Presentation for SQL database connectivity
The AUB Centre for AI in Media Proposal.docx

Session 36 - JSP - Part 1

  • 1. Java & JEE Training Session 36 – 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!