SlideShare a Scribd company logo
Java & JEE Training
Session 37 – JSP Part 2 (Final)
Page 1Classification: Restricted
Agenda
• JSP vs Servlet
• LifeCycle of Servlet
• JSP Elements
• JSP Page directive
• Directives vs Action tags
Page 2Classification: Restricted
Review of last session on JSP
• JSP vs Servlet
• Model1 vs Model 2 (MVC) architecture
• JSP Elements
• Declaration
• Expression
• Scriplets
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") %>
Page 4Classification: Restricted
Model 1 Architecture
Page 5Classification: Restricted
Model 2 Architecture
Page 6Classification: Restricted
LifeCycle of Servlet
Page 7Classification: Restricted
Copyright @ 2000 Jordan Anastasiade. All rights reserved. 7
JSP Elements
• Declarations <%! code %>
• Expressions <%= expression %>
• Scriptlets <% code %>
JSP Part 2
Directives -
page, include, taglib
Page 10Classification: Restricted
Copyright @ 2000 Jordan Anastasiade. All rights reserved. 10
JSP Page Directive
• Directives are messages to the JSP container and do not produce output
into the current output stream
• Syntax:
<%@ directive attribute=“value” %>
<%@ directive attribute1=“value1”
attribute1 =“value2” … %>
There are three types of directives:
1. page
2. include
3. taglib
XML form:
<jsp:directive.directiveType attribute=“value” />
Page 11Classification: Restricted
Copyright @ 2000 Jordan Anastasiade. All rights reserved. 11
Page Directive
• Defines attributes that apply to an entire JSP page.
<%@ page
[ language="java" ]
[ extends="package.class" ]
[ import="{package.class | package.*}, ..." ]
[ session="true|false" ]
[ buffer="none|8kb|sizekb" ]
[ autoFlush="true|false" ]
[ isThreadSafe="true|false" ]
[ info="text" ]
[ errorPage="relativeURL" ]
[ contentType="mimeType [ ;charset=characterSet ]" [
isErrorPage="true|false" ]
%>
Page 12Classification: Restricted
Page directive example..
<%@ page import="java.util.*" %>
<HTML>
<BODY>
<%
System.out.println( "Evaluating date now" );
Date date = new Date();
%>
Hello! The time is now <%= date %>
</BODY>
</HTML>
Page 13Classification: Restricted
Copyright @ 2000 Jordan Anastasiade. All rights reserved. 13
Include Directive
• Includes a static file in a JSP file, parsing the file's JSP elements.
• Syntax
<%@ include file="relativeURL" %>
The <%@ include %> directive inserts a file of text or code in a JSP file at
translation time, when the JSP file is compiled.
<%@ include %> process is static. A static include means that the text
of the included file is added to the JSP file.
You may include a JSP or HTML file like this. E.g. for headers and
footers.
Page 14Classification: Restricted
Include directive example…
• index.jsp
<html>
<head>
<title>Main JSP Page</title>
</head>
<body>
<%@ include file="file1.jsp" %>
Main JSP Page: Content between two include directives.
<%@ include file="file2.jsp" %>
</body>
</html>
• file1.jsp
<p align="center">
This is my File1.jsp and I will include it in index.jsp using include directive
</p>
• file2.jsp
<p align="center">
This is File2.jsp
</p>
Page 15Classification: Restricted
Copyright @ 2000 Jordan Anastasiade. All rights reserved. 15
Taglib Directive
• Defines a tag library and prefix for the custom tags used in the JSP page.
<%@ taglib uri="URIToTagLibrary" prefix="tagPrefix" %>
<%@ taglib uri="http://thathost/tags" prefix="public" %>
<public:loop>
</public:loop>
The <%@ taglib %> directive declares that the JSP file uses custom tags,
names the tag library that defines them, and specifies their tag prefix.
We will look into this further when we look at frameworks like Struts
and Spring.
JSP Action Tags:
include, forward, useBean
Page 17Classification: Restricted
Directives vs Action tags
• Directives are used during translation phase while actions are used during
request processing phase.
• Unlike Directives, Actions are re-evaluated each time the page is accessed.
Page 18Classification: Restricted
<jsp:include>
<html>
<head>
<title>Demo of JSP include Action Tag</title>
</head>
<body>
<h3>JSP page: Demo Include</h3>
<jsp:include page="sample.jsp" />
</body>
</html>
Page 19Classification: Restricted
<jsp:include> with parameters
index.jsp
<html>
<head>
<title>JSP Include example with parameters</title>
</head>
<body>
<h2>This is index.jsp Page</h2>
<jsp:include page="display.jsp">
<jsp:param name="userid" value=“Pawan" />
<jsp:param name="password" value=“Pawan" />
<jsp:param name="name" value=“Pawan Kumar" />
<jsp:param name="age" value="27" />
</jsp:include>
</body>
</html>
display.jsp
<html>
<head>
<title>Display Page</title>
</head>
<body>
<h2>Hello this is a display.jsp Page</h2>
UserID: <%=request.getParameter("userid") %><br>
Password is: <%=request.getParameter("password")
%><br>
User Name: <%=request.getParameter("name")
%><br>
Age: <%=request.getParameter("age") %>
</body>
</html>
Page 20Classification: Restricted
<jsp:forward> without parameters
• first.jsp
<html>
<head>
<title>Demo of JSP Forward Action Tag</title>
</head>
<body>
<h3>JSP page: Demo forward</h3>
<jsp:forward page="second.jsp" />
</body>
</html>
Page 21Classification: Restricted
<jsp:forward> with parameters
index.jsp
<html>
<head>
<title>JSP Include example with parameters</title>
</head>
<body>
<h2>This is index.jsp Page</h2>
<jsp:forward page="display.jsp">
<jsp:param name="userid" value=“Pawan" />
<jsp:param name="password" value=“Pawan" />
<jsp:param name="name" value=“Pawan Kumar" />
<jsp:param name="age" value="27" />
</jsp:forward>
</body>
</html>
display.jsp
<html>
<head>
<title>Display Page</title>
</head>
<body>
<h2>Hello this is a display.jsp Page</h2>
UserID: <%=request.getParameter("userid") %><br>
Password is: <%=request.getParameter("password")
%><br>
User Name: <%=request.getParameter("name")
%><br>
Age: <%=request.getParameter("age") %>
</body>
</html>
Page 22Classification: Restricted
<jsp:useBean> : Bean class
• First write a bean Details.java with username, age, password as properties.
public class Details {
public Details() {
}
private String username;
private int age;
private String password;
// Write getter and setter methods here…
}
Page 23Classification: Restricted
<jsp:useBean> : index.jsp
<html>
<head><title>
useBean, getProperty and setProperty example
</title></head>
<form action="userdetails.jsp" method="post">
User Name: <input type="text" name="username"><br>
User Password: <input type="password" name="password"><br>
User Age: <input type="text" name="age"><br>
<input type="submit" value="register">
</form>
</html>
Page 24Classification: Restricted
<jsp:useBean>: userDetails.jsp
<jsp:useBean id="userinfo“ class=“mypackage.Details">
</jsp:useBean>
<jsp:setProperty property="*" name="userinfo"/>
You have enterted below details:<br>
<jsp:getProperty property="username" name="userinfo"/><br>
<jsp:getProperty property="password" name="userinfo"/><br>
<jsp:getProperty property="age" name="userinfo" /><br>
Page 25Classification: Restricted
Copyright @ 2000 Jordan Anastasiade. All rights reserved. 25
Conclusion
JavaServer Pages (JSP) lets you separate the dynamic part of your pages
from the static HTML.
1. One can simply write the regular HTML in the normal manner, using
whatever Web-page-building tools you normally use.
2. One can enclose then the code for the dynamic parts in special tags,
most of which
start with "<%"
and end with "%>"
Page 26Classification: Restricted
Copyright @ 2000 Jordan Anastasiade. All rights reserved. 26
Topics to be covered in next session
• Core Java features with Examples
• Assertions
• Varargs
• Static import
• Autoboxing and Unboxing
• Enum
• Covariant
• Annotations
• Generics
• Instrumentation
• Catch Multiple Exceptions
Page 27Classification: Restricted
Copyright @ 2000 Jordan Anastasiade. All rights reserved. 27
Thank you!

More Related Content

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

What's hot (19)

PPTX
Session 25 - Introduction to JEE, Servlets
PPTX
Session 26 - Servlets Part 2
PPTX
Session 29 - Servlets - Part 5
PPTX
Session 40 - Hibernate - Part 2
PPSX
JSP - Part 2 (Final)
PPTX
Session 28 - Servlets - Part 4
PPSX
JSP - Part 1
PPSX
Hibernate - Part 2
PPSX
Spring - Part 4 - Spring MVC
PPSX
Struts 2 - Introduction
PDF
Jsp & Ajax
PDF
Java Web Programming [4/9] : JSP Basic
PPSX
Java IO, Serialization
PDF
Lecture 2: Servlets
PPTX
Session 43 - Spring - Part 1 - IoC DI Beans
PPTX
WebLogic Developer Webcast 1: JPA 2.0
PDF
Java Web Programming [3/9] : Servlet Advanced
PDF
jsf2 Notes
PDF
Session 25 - Introduction to JEE, Servlets
Session 26 - Servlets Part 2
Session 29 - Servlets - Part 5
Session 40 - Hibernate - Part 2
JSP - Part 2 (Final)
Session 28 - Servlets - Part 4
JSP - Part 1
Hibernate - Part 2
Spring - Part 4 - Spring MVC
Struts 2 - Introduction
Jsp & Ajax
Java Web Programming [4/9] : JSP Basic
Java IO, Serialization
Lecture 2: Servlets
Session 43 - Spring - Part 1 - IoC DI Beans
WebLogic Developer Webcast 1: JPA 2.0
Java Web Programming [3/9] : Servlet Advanced
jsf2 Notes
Ad

Similar to Session 37 - JSP - Part 2 (final) (20)

PPTX
JSP Directives
PPTX
SCWCD : Java server pages CHAP : 9
PPTX
JSP Directives IMPLICIT ACTIONS and HACKING.pptx
PPTX
JSP AND XML USING JAVA WITH GET AND POST METHODS
PPTX
Jsp elements
PDF
PPT
JSP Part 1
PPTX
Introduction to JSP.pptx
PPTX
Jsp session 4
PPTX
JSP- JAVA SERVER PAGES
PPT
Jsp intro
PPTX
JSP.pptx programming guide for beginners and experts
PPT
PDF
Lap trinh web [Slide jsp]
PPT
Session 5 : intro to jsp - Giáo trình Bách Khoa Aptech
PPTX
Java server pages
PPTX
JSP_Complete_Guide_With_Step_By_Step_solution
JSP Directives
SCWCD : Java server pages CHAP : 9
JSP Directives IMPLICIT ACTIONS and HACKING.pptx
JSP AND XML USING JAVA WITH GET AND POST METHODS
Jsp elements
JSP Part 1
Introduction to JSP.pptx
Jsp session 4
JSP- JAVA SERVER PAGES
Jsp intro
JSP.pptx programming guide for beginners and experts
Lap trinh web [Slide jsp]
Session 5 : intro to jsp - Giáo trình Bách Khoa Aptech
Java server pages
JSP_Complete_Guide_With_Step_By_Step_solution
Ad

More from PawanMM (16)

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 38 - Core Java (New Features) - Part 1
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 38 - Core Java (New Features) - Part 1
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
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PPTX
Cloud computing and distributed systems.
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
A Presentation on Artificial Intelligence
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Approach and Philosophy of On baking technology
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Modernizing your data center with Dell and AMD
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Big Data Technologies - Introduction.pptx
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Cloud computing and distributed systems.
Dropbox Q2 2025 Financial Results & Investor Presentation
A Presentation on Artificial Intelligence
Diabetes mellitus diagnosis method based random forest with bat algorithm
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Approach and Philosophy of On baking technology
Building Integrated photovoltaic BIPV_UPV.pdf
Review of recent advances in non-invasive hemoglobin estimation
Modernizing your data center with Dell and AMD
The Rise and Fall of 3GPP – Time for a Sabbatical?
Understanding_Digital_Forensics_Presentation.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Big Data Technologies - Introduction.pptx

Session 37 - JSP - Part 2 (final)

  • 1. Java & JEE Training Session 37 – JSP Part 2 (Final)
  • 2. Page 1Classification: Restricted Agenda • JSP vs Servlet • LifeCycle of Servlet • JSP Elements • JSP Page directive • Directives vs Action tags
  • 3. Page 2Classification: Restricted Review of last session on JSP • JSP vs Servlet • Model1 vs Model 2 (MVC) architecture • JSP Elements • Declaration • Expression • Scriplets
  • 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") %>
  • 8. Page 7Classification: Restricted Copyright @ 2000 Jordan Anastasiade. All rights reserved. 7 JSP Elements • Declarations <%! code %> • Expressions <%= expression %> • Scriptlets <% code %>
  • 11. Page 10Classification: Restricted Copyright @ 2000 Jordan Anastasiade. All rights reserved. 10 JSP Page Directive • Directives are messages to the JSP container and do not produce output into the current output stream • Syntax: <%@ directive attribute=“value” %> <%@ directive attribute1=“value1” attribute1 =“value2” … %> There are three types of directives: 1. page 2. include 3. taglib XML form: <jsp:directive.directiveType attribute=“value” />
  • 12. Page 11Classification: Restricted Copyright @ 2000 Jordan Anastasiade. All rights reserved. 11 Page Directive • Defines attributes that apply to an entire JSP page. <%@ page [ language="java" ] [ extends="package.class" ] [ import="{package.class | package.*}, ..." ] [ session="true|false" ] [ buffer="none|8kb|sizekb" ] [ autoFlush="true|false" ] [ isThreadSafe="true|false" ] [ info="text" ] [ errorPage="relativeURL" ] [ contentType="mimeType [ ;charset=characterSet ]" [ isErrorPage="true|false" ] %>
  • 13. Page 12Classification: Restricted Page directive example.. <%@ page import="java.util.*" %> <HTML> <BODY> <% System.out.println( "Evaluating date now" ); Date date = new Date(); %> Hello! The time is now <%= date %> </BODY> </HTML>
  • 14. Page 13Classification: Restricted Copyright @ 2000 Jordan Anastasiade. All rights reserved. 13 Include Directive • Includes a static file in a JSP file, parsing the file's JSP elements. • Syntax <%@ include file="relativeURL" %> The <%@ include %> directive inserts a file of text or code in a JSP file at translation time, when the JSP file is compiled. <%@ include %> process is static. A static include means that the text of the included file is added to the JSP file. You may include a JSP or HTML file like this. E.g. for headers and footers.
  • 15. Page 14Classification: Restricted Include directive example… • index.jsp <html> <head> <title>Main JSP Page</title> </head> <body> <%@ include file="file1.jsp" %> Main JSP Page: Content between two include directives. <%@ include file="file2.jsp" %> </body> </html> • file1.jsp <p align="center"> This is my File1.jsp and I will include it in index.jsp using include directive </p> • file2.jsp <p align="center"> This is File2.jsp </p>
  • 16. Page 15Classification: Restricted Copyright @ 2000 Jordan Anastasiade. All rights reserved. 15 Taglib Directive • Defines a tag library and prefix for the custom tags used in the JSP page. <%@ taglib uri="URIToTagLibrary" prefix="tagPrefix" %> <%@ taglib uri="http://thathost/tags" prefix="public" %> <public:loop> </public:loop> The <%@ taglib %> directive declares that the JSP file uses custom tags, names the tag library that defines them, and specifies their tag prefix. We will look into this further when we look at frameworks like Struts and Spring.
  • 17. JSP Action Tags: include, forward, useBean
  • 18. Page 17Classification: Restricted Directives vs Action tags • Directives are used during translation phase while actions are used during request processing phase. • Unlike Directives, Actions are re-evaluated each time the page is accessed.
  • 19. Page 18Classification: Restricted <jsp:include> <html> <head> <title>Demo of JSP include Action Tag</title> </head> <body> <h3>JSP page: Demo Include</h3> <jsp:include page="sample.jsp" /> </body> </html>
  • 20. Page 19Classification: Restricted <jsp:include> with parameters index.jsp <html> <head> <title>JSP Include example with parameters</title> </head> <body> <h2>This is index.jsp Page</h2> <jsp:include page="display.jsp"> <jsp:param name="userid" value=“Pawan" /> <jsp:param name="password" value=“Pawan" /> <jsp:param name="name" value=“Pawan Kumar" /> <jsp:param name="age" value="27" /> </jsp:include> </body> </html> display.jsp <html> <head> <title>Display Page</title> </head> <body> <h2>Hello this is a display.jsp Page</h2> UserID: <%=request.getParameter("userid") %><br> Password is: <%=request.getParameter("password") %><br> User Name: <%=request.getParameter("name") %><br> Age: <%=request.getParameter("age") %> </body> </html>
  • 21. Page 20Classification: Restricted <jsp:forward> without parameters • first.jsp <html> <head> <title>Demo of JSP Forward Action Tag</title> </head> <body> <h3>JSP page: Demo forward</h3> <jsp:forward page="second.jsp" /> </body> </html>
  • 22. Page 21Classification: Restricted <jsp:forward> with parameters index.jsp <html> <head> <title>JSP Include example with parameters</title> </head> <body> <h2>This is index.jsp Page</h2> <jsp:forward page="display.jsp"> <jsp:param name="userid" value=“Pawan" /> <jsp:param name="password" value=“Pawan" /> <jsp:param name="name" value=“Pawan Kumar" /> <jsp:param name="age" value="27" /> </jsp:forward> </body> </html> display.jsp <html> <head> <title>Display Page</title> </head> <body> <h2>Hello this is a display.jsp Page</h2> UserID: <%=request.getParameter("userid") %><br> Password is: <%=request.getParameter("password") %><br> User Name: <%=request.getParameter("name") %><br> Age: <%=request.getParameter("age") %> </body> </html>
  • 23. Page 22Classification: Restricted <jsp:useBean> : Bean class • First write a bean Details.java with username, age, password as properties. public class Details { public Details() { } private String username; private int age; private String password; // Write getter and setter methods here… }
  • 24. Page 23Classification: Restricted <jsp:useBean> : index.jsp <html> <head><title> useBean, getProperty and setProperty example </title></head> <form action="userdetails.jsp" method="post"> User Name: <input type="text" name="username"><br> User Password: <input type="password" name="password"><br> User Age: <input type="text" name="age"><br> <input type="submit" value="register"> </form> </html>
  • 25. Page 24Classification: Restricted <jsp:useBean>: userDetails.jsp <jsp:useBean id="userinfo“ class=“mypackage.Details"> </jsp:useBean> <jsp:setProperty property="*" name="userinfo"/> You have enterted below details:<br> <jsp:getProperty property="username" name="userinfo"/><br> <jsp:getProperty property="password" name="userinfo"/><br> <jsp:getProperty property="age" name="userinfo" /><br>
  • 26. Page 25Classification: Restricted Copyright @ 2000 Jordan Anastasiade. All rights reserved. 25 Conclusion JavaServer Pages (JSP) lets you separate the dynamic part of your pages from the static HTML. 1. One can simply write the regular HTML in the normal manner, using whatever Web-page-building tools you normally use. 2. One can enclose then the code for the dynamic parts in special tags, most of which start with "<%" and end with "%>"
  • 27. Page 26Classification: Restricted Copyright @ 2000 Jordan Anastasiade. All rights reserved. 26 Topics to be covered in next session • Core Java features with Examples • Assertions • Varargs • Static import • Autoboxing and Unboxing • Enum • Covariant • Annotations • Generics • Instrumentation • Catch Multiple Exceptions
  • 28. Page 27Classification: Restricted Copyright @ 2000 Jordan Anastasiade. All rights reserved. 27 Thank you!