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

What's hot

Session 25 - Introduction to JEE, Servlets
Session 25 - Introduction to JEE, ServletsSession 25 - Introduction to JEE, Servlets
Session 25 - Introduction to JEE, Servlets
PawanMM
 
Session 28 - Servlets - Part 4
Session 28 - Servlets - Part 4Session 28 - Servlets - Part 4
Session 28 - Servlets - Part 4
PawanMM
 
Session 29 - Servlets - Part 5
Session 29 - Servlets - Part 5Session 29 - Servlets - Part 5
Session 29 - Servlets - Part 5
PawanMM
 
Session 26 - Servlets Part 2
Session 26 - Servlets Part 2Session 26 - Servlets Part 2
Session 26 - Servlets Part 2
PawanMM
 
Session 40 - Hibernate - Part 2
Session 40 - Hibernate - Part 2Session 40 - Hibernate - Part 2
Session 40 - Hibernate - Part 2
PawanMM
 
JSP - Part 2 (Final)
JSP - Part 2 (Final) JSP - Part 2 (Final)
JSP - Part 2 (Final)
Hitesh-Java
 
Spring - Part 4 - Spring MVC
Spring - Part 4 - Spring MVCSpring - Part 4 - Spring MVC
Spring - Part 4 - Spring MVC
Hitesh-Java
 
Struts 2 - Introduction
Struts 2 - Introduction Struts 2 - Introduction
Struts 2 - Introduction
Hitesh-Java
 
JSP - Part 1
JSP - Part 1JSP - Part 1
JSP - Part 1
Hitesh-Java
 
Hibernate - Part 2
Hibernate - Part 2 Hibernate - Part 2
Hibernate - Part 2
Hitesh-Java
 
Session 43 - Spring - Part 1 - IoC DI Beans
Session 43 - Spring - Part 1 - IoC DI BeansSession 43 - Spring - Part 1 - IoC DI Beans
Session 43 - Spring - Part 1 - IoC DI Beans
PawanMM
 
Session 38 - Core Java (New Features) - Part 1
Session 38 - Core Java (New Features) - Part 1Session 38 - Core Java (New Features) - Part 1
Session 38 - Core Java (New Features) - Part 1
PawanMM
 
Jsp & Ajax
Jsp & AjaxJsp & Ajax
Jsp & Ajax
Ang Chen
 
Lecture 2: Servlets
Lecture 2:  ServletsLecture 2:  Servlets
Lecture 2: Servlets
Fahad Golra
 
Java Web Programming [4/9] : JSP Basic
Java Web Programming [4/9] : JSP BasicJava Web Programming [4/9] : JSP Basic
Java Web Programming [4/9] : JSP Basic
IMC Institute
 
jsf2 Notes
jsf2 Notesjsf2 Notes
jsf2 Notes
Rajiv Gupta
 
Java Web Programming [3/9] : Servlet Advanced
Java Web Programming [3/9] : Servlet AdvancedJava Web Programming [3/9] : Servlet Advanced
Java Web Programming [3/9] : Servlet Advanced
IMC Institute
 
Enterprise java unit-3_chapter-1-jsp
Enterprise  java unit-3_chapter-1-jspEnterprise  java unit-3_chapter-1-jsp
Enterprise java unit-3_chapter-1-jsp
sandeep54552
 
Struts 2 - Hibernate Integration
Struts 2 - Hibernate Integration Struts 2 - Hibernate Integration
Struts 2 - Hibernate Integration
Hitesh-Java
 

What's hot (19)

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

Similar to Session 36 - JSP - Part 1

Java serverpages
Java serverpagesJava serverpages
Java serverpages
Amit Kumar
 
10 jsp-scripting-elements
10 jsp-scripting-elements10 jsp-scripting-elements
10 jsp-scripting-elements
Phạm Thu Thủy
 
Jsp
JspJsp
Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology
Ayes Chinmay
 
Jsp sasidhar
Jsp sasidharJsp sasidhar
Jsp sasidhar
Sasidhar Kothuru
 
Jsp
JspJsp
Java .ppt
Java .pptJava .ppt
Java .ppt
MuhammedYaseenMc
 
JSP.pptx
JSP.pptxJSP.pptx
JSP.pptx
NishaRohit6
 
Jsp and jstl
Jsp and jstlJsp and jstl
Jsp and jstl
vishal choudhary
 
JSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGESJSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGES
Yoga Raja
 
Jsp in Servlet by Rj
Jsp in Servlet by RjJsp in Servlet by Rj
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
Shah Nawaz Bhurt
 
JSP - Java Server Page
JSP - Java Server PageJSP - Java Server Page
JSP - Java Server Page
Vipin Yadav
 
Introduction to JSP.pptx
Introduction to JSP.pptxIntroduction to JSP.pptx
Introduction to JSP.pptx
ManishaPatil932723
 
Chap4 4 2
Chap4 4 2Chap4 4 2
Chap4 4 2
Hemo Chella
 
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 7 ...
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 7 ...Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 7 ...
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 7 ...
WebStackAcademy
 
JSP Directives
JSP DirectivesJSP Directives
JSP Directives
ShahDhruv21
 

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

Java serverpages
Java serverpagesJava serverpages
Java serverpages
 
10 jsp-scripting-elements
10 jsp-scripting-elements10 jsp-scripting-elements
10 jsp-scripting-elements
 
Jsp
JspJsp
Jsp
 
Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology
 
Jsp sasidhar
Jsp sasidharJsp sasidhar
Jsp sasidhar
 
Jsp
JspJsp
Jsp
 
JSP Technology I
JSP Technology IJSP Technology I
JSP Technology I
 
Java .ppt
Java .pptJava .ppt
Java .ppt
 
25.ppt
25.ppt25.ppt
25.ppt
 
JSP.pptx
JSP.pptxJSP.pptx
JSP.pptx
 
Jsp and jstl
Jsp and jstlJsp and jstl
Jsp and jstl
 
JSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGESJSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGES
 
Jsp in Servlet by Rj
Jsp in Servlet by RjJsp in Servlet by Rj
Jsp in Servlet by Rj
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
JSP - Java Server Page
JSP - Java Server PageJSP - Java Server Page
JSP - Java Server Page
 
Introduction to JSP.pptx
Introduction to JSP.pptxIntroduction to JSP.pptx
Introduction to JSP.pptx
 
Chap4 4 2
Chap4 4 2Chap4 4 2
Chap4 4 2
 
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 7 ...
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 7 ...Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 7 ...
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 7 ...
 
20jsp
20jsp20jsp
20jsp
 
JSP Directives
JSP DirectivesJSP Directives
JSP Directives
 

More from PawanMM

Session 48 - JS, JSON and AJAX
Session 48 - JS, JSON and AJAXSession 48 - JS, JSON and AJAX
Session 48 - JS, JSON and AJAX
PawanMM
 
Session 46 - Spring - Part 4 - Spring MVC
Session 46 - Spring - Part 4 - Spring MVCSession 46 - Spring - Part 4 - Spring MVC
Session 46 - Spring - Part 4 - Spring MVC
PawanMM
 
Session 45 - Spring - Part 3 - AOP
Session 45 - Spring - Part 3 - AOPSession 45 - Spring - Part 3 - AOP
Session 45 - Spring - Part 3 - AOP
PawanMM
 
Session 44 - Spring - Part 2 - Autowiring, Annotations, Java based Configuration
Session 44 - Spring - Part 2 - Autowiring, Annotations, Java based ConfigurationSession 44 - Spring - Part 2 - Autowiring, Annotations, Java based Configuration
Session 44 - Spring - Part 2 - Autowiring, Annotations, Java based Configuration
PawanMM
 
Session 42 - Struts 2 Hibernate Integration
Session 42 - Struts 2 Hibernate IntegrationSession 42 - Struts 2 Hibernate Integration
Session 42 - Struts 2 Hibernate Integration
PawanMM
 
Session 41 - Struts 2 Introduction
Session 41 - Struts 2 IntroductionSession 41 - Struts 2 Introduction
Session 41 - Struts 2 Introduction
PawanMM
 
Session 24 - JDBC, Intro to Enterprise Java
Session 24 - JDBC, Intro to Enterprise JavaSession 24 - JDBC, Intro to Enterprise Java
Session 24 - JDBC, Intro to Enterprise Java
PawanMM
 
Session 23 - JDBC
Session 23 - JDBCSession 23 - JDBC
Session 23 - JDBC
PawanMM
 
Session 22 - Java IO, Serialization
Session 22 - Java IO, SerializationSession 22 - Java IO, Serialization
Session 22 - Java IO, Serialization
PawanMM
 
Session 21 - Inner Classes
Session 21 - Inner ClassesSession 21 - Inner Classes
Session 21 - Inner Classes
PawanMM
 
Session 20 - Collections - Maps
Session 20 - Collections - MapsSession 20 - Collections - Maps
Session 20 - Collections - Maps
PawanMM
 
Session 19 - Review Session
Session 19 - Review SessionSession 19 - Review Session
Session 19 - Review Session
PawanMM
 
Session 18 - Review Session and Attending Java Interviews
Session 18 - Review Session and Attending Java InterviewsSession 18 - Review Session and Attending Java Interviews
Session 18 - Review Session and Attending Java Interviews
PawanMM
 
Session 17 - Collections - Lists, Sets
Session 17 - Collections - Lists, SetsSession 17 - Collections - Lists, Sets
Session 17 - Collections - Lists, Sets
PawanMM
 
Session 16 - Collections - Sorting, Comparing Basics
Session 16 - Collections - Sorting, Comparing BasicsSession 16 - Collections - Sorting, Comparing Basics
Session 16 - Collections - Sorting, Comparing Basics
PawanMM
 

More from PawanMM (15)

Session 48 - JS, JSON and AJAX
Session 48 - JS, JSON and AJAXSession 48 - JS, JSON and AJAX
Session 48 - JS, JSON and AJAX
 
Session 46 - Spring - Part 4 - Spring MVC
Session 46 - Spring - Part 4 - Spring MVCSession 46 - Spring - Part 4 - Spring MVC
Session 46 - Spring - Part 4 - Spring MVC
 
Session 45 - Spring - Part 3 - AOP
Session 45 - Spring - Part 3 - AOPSession 45 - Spring - Part 3 - AOP
Session 45 - Spring - Part 3 - AOP
 
Session 44 - Spring - Part 2 - Autowiring, Annotations, Java based Configuration
Session 44 - Spring - Part 2 - Autowiring, Annotations, Java based ConfigurationSession 44 - Spring - Part 2 - Autowiring, Annotations, Java based Configuration
Session 44 - Spring - Part 2 - Autowiring, Annotations, Java based Configuration
 
Session 42 - Struts 2 Hibernate Integration
Session 42 - Struts 2 Hibernate IntegrationSession 42 - Struts 2 Hibernate Integration
Session 42 - Struts 2 Hibernate Integration
 
Session 41 - Struts 2 Introduction
Session 41 - Struts 2 IntroductionSession 41 - Struts 2 Introduction
Session 41 - Struts 2 Introduction
 
Session 24 - JDBC, Intro to Enterprise Java
Session 24 - JDBC, Intro to Enterprise JavaSession 24 - JDBC, Intro to Enterprise Java
Session 24 - JDBC, Intro to Enterprise Java
 
Session 23 - JDBC
Session 23 - JDBCSession 23 - JDBC
Session 23 - JDBC
 
Session 22 - Java IO, Serialization
Session 22 - Java IO, SerializationSession 22 - Java IO, Serialization
Session 22 - Java IO, Serialization
 
Session 21 - Inner Classes
Session 21 - Inner ClassesSession 21 - Inner Classes
Session 21 - Inner Classes
 
Session 20 - Collections - Maps
Session 20 - Collections - MapsSession 20 - Collections - Maps
Session 20 - Collections - Maps
 
Session 19 - Review Session
Session 19 - Review SessionSession 19 - Review Session
Session 19 - Review Session
 
Session 18 - Review Session and Attending Java Interviews
Session 18 - Review Session and Attending Java InterviewsSession 18 - Review Session and Attending Java Interviews
Session 18 - Review Session and Attending Java Interviews
 
Session 17 - Collections - Lists, Sets
Session 17 - Collections - Lists, SetsSession 17 - Collections - Lists, Sets
Session 17 - Collections - Lists, Sets
 
Session 16 - Collections - Sorting, Comparing Basics
Session 16 - Collections - Sorting, Comparing BasicsSession 16 - Collections - Sorting, Comparing Basics
Session 16 - Collections - Sorting, Comparing Basics
 

Recently uploaded

Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
Bhaskar Mitra
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Tobias Schneck
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
Frank van Harmelen
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Inflectra
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
CatarinaPereira64715
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 

Recently uploaded (20)

Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 

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!