SlideShare a Scribd company logo
Java Server Pages (JSP Basic)
BCA -302
JSP
2
Learning
Objectives
Introduction to Dynamic Web Pages
Overview of JSP
3
Content
• Introduction to Web Page
• Static Web Page
• Dynamic Web Page
• Difference Between Static and Dynamic Webpages
• Introduction to JSP
• JSP Life Cycle
• JSP Directory Structure
• JSP Elements/ JSP Tags
4
Web Page
• web page is a document available on world wide web. Web Pages
are stored on web server and can be viewed using a web browser.
• A web page can contain huge information including text,
graphics, audio, video and hyper links. These hyper links are the
link to other web pages.
• Collection of linked web pages on a web server is known as
website. There is unique Uniform Resource Locator (URL) is
associated with each web page.
5
Static Web Page
• Static web pages are also known as flat or
stationary web page.
• They are loaded on the client’s browser as exactly
they are stored on the web server.
• Such web pages contain only static information.
• User can only read the information but can’t do any
modification or interact with the information.
• Static web pages are created using only HTML.
• Static web pages are only used when the
information is no more required to be modified.
6
Dynamic Web Page
• Dynamic web page shows different information at different point of time. It is
possible to change a portaion of a web page without loading the entire web page. It
has been made possible using Ajax technology.
• Server-side dynamic web page: It is created by using server-side scripting. There
are server-side scripting parameters that determine how to assemble a new web
page which also include setting up of more client-side processing.
• Client-side dynamic web page: It is processed using client side scripting such as
JavaScript. And then passed in to Document Object Model (DOM).
7
Dynamic Web Page
8
Dynamic Web Page
SL.NO STATIC WEB PAGE DYNAMIC WEB PAGE
1.
In static web pages, Pages will remain same
until someone changes it manually.
In dynamic web pages, Content of pages
are different for different visitors.
2.
Static Web Pages are simple in terms of
complexity.
Dynamic web pages are complicated.
3.
In static web pages, Information are change
rarely.
In dynamic web page, Information are
change frequently.
4.
Static Web Page takes less time for loading
than dynamic web page.
Dynamic web page takes more time for
loading.
5. In Static Web Pages, database is not used. In dynamic web pages, database is used.
6.
Static web pages are written in languages such
as: HTML, JavaScript, CSS, etc.
Dynamic web pages are written in
languages such as: CGI, AJAX, ASP,
ASP.NET, etc.
7.
Static web pages does not contain any
application program .
Dynamic web pages contains application
program for different services.
8.
Static web pages require less work and cost in
designing them.
Dynamic web pages require comparatively
more work and cost in designing them.
9
Introduction to JSP
• Java Server Pages (JSP) is a server side technology for developing dynamic web pages.
• This is mainly used for implementing presentation layer (GUI Part) of an application.
• A complete JSP code is more like a HTML with bits of java code in it.
• JSP is an extension of servlets and every JSP page first gets converted into servlet by
JSP container before processing the client’s request.
• JSP technology is used to create web application just like Servlet technology.
• It can be thought of as an extension to Servlet because it provides more functionality than
servlet such as expression language, JSTL, etc.
• A JSP page consists of HTML tags and JSP tags.
• The JSP pages are easier to maintain than Servlet because we can separate designing
and development. It provides some additional features such as Expression Language,
Custom Tags, etc
• JSP have access to the entire family of Java APIs, including the JDBC API to access
enterprise databases
• JSP runs on tomcat server.
• JSP pages are saved with .jsp extension which lets the server know that this is a JSP
page and needs to go through JSP life cycle stages.
10
Architecture of a JSP Application
• Based on the location where request
processing happens (Servlet OR
JSP(java server pages)) there are two
architectures for JSP.
• They are – Model1 Architecture &
Model2 Architecture.
• Model1 Architecture: In this Model,
JSP plays a key role and it is
responsible for of processing the
request made by client. Client (Web
browser) makes a request, JSP then
creates a bean object which
then fulfils the request and pass the
response to JSP. JSP then sends the
response back to client. Unlike Model2
architecture, in this Model most of the
processing is done by JSP itself.
11
Architecture of a JSP Application Contd…
• Model2 Architecture: In this Model,
Servlet plays a major role and it
is responsible for processing the
client’s(web browser) request.
Presentation part (GUI part) will be
handled by JSP and it is done with the
help of bean as shown in image. The
servlet acts as controller and in charge
of request processing. It creates the
bean objects if required by the jsp
page and calls the respective jsp page.
The jsp handles the presentation part
by using the bean object. In this Model,
JSP doesn’t do any processing,
Servlet creates the bean Object and
calls the JSP program as per the
request made by client..
12
JSP Life Cycle
• A JSP life cycle is defined as the
process from its creation till the
destruction.
• The following are the paths
followed by a JSP −
1. Compilation
2. Initialization
3. Execution
4. Cleanup
13
JSP Life Cycle Contd…
JSP Initialization
When a container loads a JSP it invokes the jspInit() method before servicing any
requests. If there is need to perform JSP-specific initialization, override the jspInit()
method −
public void jspInit(){
// Initialization code...
}
Typically, initialization is performed only once and as with the servlet init method,
you generally initialize database connections, open files, and create lookup tables
in the jspInit method.
14
JSP Life Cycle Contd…
JSP Compilation
When a browser asks for a JSP, the JSP engine first checks to see whether it
needs to compile the page. If the page has never been compiled, or if the JSP has
been modified since it was last compiled, the JSP engine compiles the page.
The compilation process involves three steps −
Parsing the JSP.
Turning the JSP into a servlet.
Compiling the servlet.
15
JSP Life Cycle Contd…
JSP Execution
• This phase of the JSP life cycle represents all interactions with requests until the JSP is
destroyed.
• Whenever a browser requests a JSP and the page has been loaded and initialized, the
JSP engine invokes the _jspService() method in the JSP.
• The _jspService() method takes an HttpServletRequest and an HttpServletResponse as its
parameters as follows −
void _jspService(HttpServletRequest request, HttpServletResponse response) {
// Service handling code...
}
• The _jspService() method of a JSP is invoked on request basis. This is responsible for
generating the response for that request and this method is also responsible for generating
responses to all seven of the HTTP methods, i.e, GET, POST, DELETE, etc..
16
JSP Life Cycle Contd…
JSP Cleanup
• The destruction phase of the JSP life cycle represents when a JSP is being removed from
use by a container.
• The jspDestroy() method is the JSP equivalent of the destroy method for servlets. Override
jspDestroy when you need to perform any cleanup, such as releasing database
connections or closing open files.
• The jspDestroy() method has the following form −
public void jspDestroy() {
// Your cleanup code goes here.
}
17
JSP Directory Structure
18
JSP Elements
• The Scriptlet
• JSP Declaration
• JSP Expression
• JSP Comments
• JSP Directives
• JSP Actions
• JSP Implicit Objects
19
JSP Scriptlet
• A scriptlet can contain any number of
JAVA language statements, variable or
method declarations, or expressions that
are valid in the page scripting language.
• Following is the syntax of Scriptlet −
<% code fragment %>
• For XML the syntax is
<jsp:scriptlet>
code fragment
</jsp:scriptlet>
<html>
<head>JSP Scriptlet Demo</title></head>
<body>
JSP Scriptlet Demo <br/>
<%
out.println("Your IP address is " +
request.getRemoteAddr());
%>
</body>
</html>
20
JSP Declaration
• Declaration tag is a block of java code for
declaring class wide variables, methods and
classes.
• Whatever placed inside these tags gets
initialized during JSP initialization phase and
has class scope.
• JSP container keeps this code outside of the
service method (_jspService()) to make them
class level variables and methods.
• Following is the syntax for JSP Declarations −
<%! declaration; [ declaration; ]+ ... %>
• For XML The syntax is:
<jsp:declaration>
code fragment
</jsp:declaration>
<html>
<head><title>JSP Declaration Demo
</title></head>
<body>
JSP Declaration Demo<br/>
// Variable Declaration
<%! int i = 0; %>
<%! int a, b, c; %>
<%! Circle a = new Circle(2.0); %>
// function Declaration
<%!
int sum(int num1, int num2, int num3){
return num1+num2+num3;
}
%>
</body>
</html>
21
JSP Expression
• Expression tag evaluates the expression placed
in it, converts the result into String and send the
result back to the client through response
object. Basically it writes the result to the
client(browser).
• Syntax of expression tag in JSP:
• <%= expression %>
<html>
<head>
<title>JSP expression tag example</title>
</head>
<body>
//Expression of values
<%= 2+4*5 %>
//Expression of variable
<%
int a=10;
int b=20;
int c=30;
%>
<%= a+b+c %>
</body>
</html>
22
JSP Comment
• JSP comment marks text or statements that the
JSP container should ignore. A JSP comment
is useful when you want to hide or "comment
out", a part of the JSP page.
• Following is the syntax of the JSP comments −
• <%-- This is JSP comment --%>
<html>
<head>
<title>JSP Comment</title>
</head>
<body>
<h2>A Test of Comments</h2>
<%-- This comment will not be visible in the
page source --%>
</body>
</html>
23
JSP Directive
• Directives control the processing of an entire JSP page. It gives directions to the server
regarding processing of a page.
• Directives can have a number of attributes which programmer can list down as key-value pairs
and separated by commas.
• The blanks between the @ symbol and the directive name, and between the last attribute and
the closing %>, are optional.
• Syntax of Directives:
<%@ directive name [attribute name=“value” attribute name=“value” ........]%>
• There are three types of Directives in JSP:
1) Page Directive <%@ page ... %>
2) Include Directive <%@ include ... %>
3) TagLib Directive <%@ taglib ... %>
24
Page Directive
• The page directive is used to provide instructions to the container. These instructions pertain to
the current JSP page.
• One may code page directives anywhere in JSP page. By convention, page directives are
coded at the top of the JSP page.
• Basic syntax is as:
<%@ page attribute = "value" %>
• There are several attributes, which are used along with Page Directives and these are –
1. import
2. session
3. isErrorPage
4. errorPage
5. ContentType
6. isThreadSafe
7. buffer
8. extends
9. info
10. language
11. Autoflush, etc
25
Page Directive (import attribute)
1. Import: This attribute is used to import
packages. While doing coding there is
a need to include more than one
packages, In such scenarios this page
directive’s attribute is very useful as it
allows to mention more than one
packages at the same place separated
by commas (,). Alternatively multiple
instances of page element each one
with different package can be coded.
Syntax of import attribute –
<%@page import="value"%>
<%@page import="java.io.*%>
<%@page import="java.lang.*%>
<%--Comment: OR Below Statement: Both are
Same--%>
<%@page import="java.io.*, java.lang.*"%>
26
Page Directive (attribute)
2. session:
• This attribute is to handle HTTP sessions for JSP pages. It can have two values: true or
false.
• Default value for this attribute: true
• Syntax of session attribute:
<%@ page session="value"%>
here value is either true OR false
3. isErrorPage:
• This attribute is used to specify whether the current JSP page can be used as an error
page for another JSP page.
• If value of isErrorPage is true it means that the page can be used for exception handling
for another page. Generally these pages has error/warning messages OR exception
handling codes and being called by another JSP page when there is an exception
occurred there.
• Default value for this attribute: false
• Syntax:
• <%@ page isErrorPage="value"%>
<%@ page session="true"%>
<%@ page session=“false"%>
<%@ page isErrorPage=“false"%>
<%@ page isErrorPage=“true"%>
27
Page Directive (attribute)
4. ErrorPage:
• errorPage attribute is used to specify the URL of a JSP page which has isErrorPage
attrbute set to true. It handles the un-handled exceptions in the page.
• Syntax of errorPage attribute:
<%@ page errorPage="value"%>
• Here value is a JSP page name which has exception handling code (and isErrorPage
set to true).
5. ContentType
• This attribute is used to set the content type of a JSP page.
• Default value: text/html
• Syntax of contentType attribute:
<%@ page contentType="value"%>
<%@ page errorPage="ExceptionHandling.jsp"%>
<%@ page contentType="text/html"%>
28
Page Directive (attribute)
6. isThreadSafe:
• Setting isThreadSafe as true, it means that the JSP page supports multithreading (more
than one thread can execute the JSP page simultaneously). On the other hand if it is set
to false then JSP engine won’t allow multithreading which means only single thread will
execute the page code.
• Default value for isThreadSafe attribute: true.
• Syntax of isThreadSafe attribute:
<%@ page isThreadSafe="value"%>
7. buffer
• This attribute is used to specify the buffer size. If you specify this to none during coding
then the output would directly written to Response object by JSPWriter. And, if you specify
a buffer size then the output first written to buffer then it will be available for response
object.
• Syntax of buffer attribute:
• <%@ page buffer="value"%> // value is size in kb or none.
<%@ page isThreadSafe="false"%>
<%@ page buffer=“none"%>
<%@ page buffer=“5kb”%>
29
Page Directive (attribute)
8. extends:
• This attribute is used to inherit class.
• Syntax of extends attribute:
<%@ page extends="value"%>
9. info
• It provides a description to a JSP page. The string specified in info will return when we will
call getServletInfo() method.
• Syntax of info:
<%@ page info="value"%> // value is size in kb or none.
<%@ page extends="mypackage.SampleClass"%>
<%@ page info="This code is given by Chaitanya
Singh"%>
30
Page Directive (attribute)
10. language:
• It specifies the scripting language( underlying language) being used in the page..
• Syntax of language attribute:
<%@ page language="value"%>
Here value is any scripting language
11. autoFlush
• If it is true it means the buffer should be flushed whenever it is full. false will throw an
exception when buffer overflows.
• Default Value: True
• Syntax of info:
<%@ page autoFlush="value"%> // value can be true or false
<%@ page language=“java"%>
<%@ page autoFlush=“true”%>
31
include Directive
• The include directive is used to include a file
during the translation phase.
• This directive tells the container to merge the
content of other external files with the current JSP
during the translation phase.
• It can be used anywhere in the page.
• Syntax of Include Directive:
<%@include file ="value"%>, here value is the
JSP file name which needs to be included.
• Example: <%@include file="myJSP.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>
32
taglib Directive
• The JavaServer Pages API allow to define
custom JSP tags that look like HTML or XML
tags and a tag library is a set of user-defined
tags that implement custom behavior.
• The taglib directive declares that the JSP page
uses a set of custom tags, identifies the
location of the library, and provides means for
identifying the custom tags in the JSP page.
• The taglib directive follows the syntax given
below −
<%@ taglib uri="uri" prefix = "prefixOfTag" >,
Here, the uri attribute value resolves to a location
the container understands and the prefix attribute
informs a container what bits of markup are custom
actions
<%@ taglib uri =
"http://www.example.com/custlib" prefix
= "mytag" %>
<html>
<body>
<mytag:hello/>
</body>
</html>
33
JSP Actions
• JSP actions use constructs in XML syntax to control the behavior of the servlet engine.
• Syntax:
<jsp:action_name attribute = "value" />
• There are many JSP action tags or elements. Each JSP action tag is used to perform some
specific tasks.
• The action tags are used to control the flow between pages and to use Java Bean.
34
JSP Actions
JSP Action Tags Description
jsp:forward forwards the request and response to another resource.
jsp:include includes another resource.
jsp:useBean creates or locates bean object.
jsp:setProperty sets the value of property in bean object.
jsp:getProperty prints the value of property of the bean.
jsp:plugin embeds another components such as applet.
jsp:param sets the parameter value. It is used in forward and include mostly.
jsp:fallback can be used to print the message if plugin is working. It is used in
jsp:plugin.
35
JSP implicit objects
• JSP implicit Objects are the Java objects
that the JSP Container makes available to
the developers in each page and the
developer can call them directly without
being explicitly declared. JSP Implicit
Objects are also called pre-defined
variables.
• These objects are created by JSP Engine
during translation phase (while translating
JSP to Servlet). They are being created
inside service method so can be directly
used within Scriptlet without initializing
and declaring them.
• There are total 9 implicit objects available
in JSP.
Object Corresponding Class
out javax.servlet.jsp.JspWriter
request javax.servlet.http.HttpServletRequest
response javax.servlet.http.HttpServletResponse
session javax.servlet.http.HttpSession
applicationjavax.servlet.ServletContext
exception javax.servlet.jsp.JspException
page java.lang.Object
pageConte
xt
javax.servlet.jsp.PageContext
config javax.servlet.ServletConfig

More Related Content

PDF
Java Server Pages
PPTX
INTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE AND JSP PROCESSING
PDF
Jsp tutorial (1)
PPT
Deploying java beans in jsp
PPTX
Java Server Pages(jsp)
PPT
Jsp(java server pages)
PPTX
Java Server Pages
PPT
Jsp Comparison
Java Server Pages
INTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE AND JSP PROCESSING
Jsp tutorial (1)
Deploying java beans in jsp
Java Server Pages(jsp)
Jsp(java server pages)
Java Server Pages
Jsp Comparison

What's hot (19)

PDF
PPT
JSP Processing
PPT
3.jsp tutorial
PPTX
Jsp (java server page)
DOC
JSP Scope variable And Data Sharing
PPT
Jsp slides
PPTX
Java server pages
PPTX
PPTX
Jsp elements
PPS
Jsp chapter 1
PPSX
JSP - Part 2 (Final)
PPT
Jsp Slides
PDF
Jeetrainers.com coursejspservlets00
PDF
Java Web Programming [4/9] : JSP Basic
PPTX
Session 37 - JSP - Part 2 (final)
PPTX
Enterprise java unit-2_chapter-3
PPT
Introduction to jsp
JSP Processing
3.jsp tutorial
Jsp (java server page)
JSP Scope variable And Data Sharing
Jsp slides
Java server pages
Jsp elements
Jsp chapter 1
JSP - Part 2 (Final)
Jsp Slides
Jeetrainers.com coursejspservlets00
Java Web Programming [4/9] : JSP Basic
Session 37 - JSP - Part 2 (final)
Enterprise java unit-2_chapter-3
Introduction to jsp
Ad

Similar to Jsp basic (20)

DOCX
Java server pages
DOCX
Unit 4 1 web technology uptu
DOCX
Unit 4 web technology uptu
PPT
JSP 1.pptdfdfdfdsfdsfdsfdsfdsgdgdgdgdgdd
PPTX
JavaScript, often abbreviated as JS, is a programming language and core techn...
PPTX
JAVA SERVER PAGES
PPTX
Module 3.pptx.............................
PPTX
JSP- JAVA SERVER PAGES
PPTX
JSP - Java Server Page
PPTX
JSP overview
PDF
Enterprise java unit-3_chapter-1-jsp
PPTX
JSP.pptx
PPTX
Platform for Enterprise Solution - Java EE5
PPT
Atul & shubha goswami jsp
PPTX
WT Unit-Vuufvmjn dissimilating Dunkirk k
PPTX
Introduction - Java Server Programming (JSP)
ODP
servlets and jsp
PPTX
Java server pages
Unit 4 1 web technology uptu
Unit 4 web technology uptu
JSP 1.pptdfdfdfdsfdsfdsfdsfdsgdgdgdgdgdd
JavaScript, often abbreviated as JS, is a programming language and core techn...
JAVA SERVER PAGES
Module 3.pptx.............................
JSP- JAVA SERVER PAGES
JSP - Java Server Page
JSP overview
Enterprise java unit-3_chapter-1-jsp
JSP.pptx
Platform for Enterprise Solution - Java EE5
Atul & shubha goswami jsp
WT Unit-Vuufvmjn dissimilating Dunkirk k
Introduction - Java Server Programming (JSP)
servlets and jsp
Ad

More from Jaya Kumari (20)

PPTX
Python data type
PPTX
Introduction to python
PPTX
Variables in python
PPTX
Basic syntax supported by python
PPTX
PPTX
Inheritance
PPTX
Overloading
PPTX
Decision statements
PPTX
Loop control statements
PPTX
Looping statements
PPTX
Operators used in vb.net
PPTX
Variable and constants in Vb.NET
PPTX
Keywords, identifiers and data type of vb.net
PPTX
Introduction to vb.net
PPTX
Frame class library and namespace
PPTX
Introduction to .net
PPTX
Java script Basic
PPTX
Sgml and xml
PPTX
Java script Advance
PPTX
Html form
Python data type
Introduction to python
Variables in python
Basic syntax supported by python
Inheritance
Overloading
Decision statements
Loop control statements
Looping statements
Operators used in vb.net
Variable and constants in Vb.NET
Keywords, identifiers and data type of vb.net
Introduction to vb.net
Frame class library and namespace
Introduction to .net
Java script Basic
Sgml and xml
Java script Advance
Html form

Recently uploaded (20)

PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
A Presentation on Artificial Intelligence
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPT
Teaching material agriculture food technology
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Approach and Philosophy of On baking technology
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Electronic commerce courselecture one. Pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Empathic Computing: Creating Shared Understanding
Chapter 3 Spatial Domain Image Processing.pdf
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
A Presentation on Artificial Intelligence
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
Teaching material agriculture food technology
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Unlocking AI with Model Context Protocol (MCP)
20250228 LYD VKU AI Blended-Learning.pptx
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Understanding_Digital_Forensics_Presentation.pptx
Spectral efficient network and resource selection model in 5G networks
Dropbox Q2 2025 Financial Results & Investor Presentation
Approach and Philosophy of On baking technology
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Electronic commerce courselecture one. Pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Empathic Computing: Creating Shared Understanding

Jsp basic

  • 1. Java Server Pages (JSP Basic) BCA -302
  • 3. 3 Content • Introduction to Web Page • Static Web Page • Dynamic Web Page • Difference Between Static and Dynamic Webpages • Introduction to JSP • JSP Life Cycle • JSP Directory Structure • JSP Elements/ JSP Tags
  • 4. 4 Web Page • web page is a document available on world wide web. Web Pages are stored on web server and can be viewed using a web browser. • A web page can contain huge information including text, graphics, audio, video and hyper links. These hyper links are the link to other web pages. • Collection of linked web pages on a web server is known as website. There is unique Uniform Resource Locator (URL) is associated with each web page.
  • 5. 5 Static Web Page • Static web pages are also known as flat or stationary web page. • They are loaded on the client’s browser as exactly they are stored on the web server. • Such web pages contain only static information. • User can only read the information but can’t do any modification or interact with the information. • Static web pages are created using only HTML. • Static web pages are only used when the information is no more required to be modified.
  • 6. 6 Dynamic Web Page • Dynamic web page shows different information at different point of time. It is possible to change a portaion of a web page without loading the entire web page. It has been made possible using Ajax technology. • Server-side dynamic web page: It is created by using server-side scripting. There are server-side scripting parameters that determine how to assemble a new web page which also include setting up of more client-side processing. • Client-side dynamic web page: It is processed using client side scripting such as JavaScript. And then passed in to Document Object Model (DOM).
  • 8. 8 Dynamic Web Page SL.NO STATIC WEB PAGE DYNAMIC WEB PAGE 1. In static web pages, Pages will remain same until someone changes it manually. In dynamic web pages, Content of pages are different for different visitors. 2. Static Web Pages are simple in terms of complexity. Dynamic web pages are complicated. 3. In static web pages, Information are change rarely. In dynamic web page, Information are change frequently. 4. Static Web Page takes less time for loading than dynamic web page. Dynamic web page takes more time for loading. 5. In Static Web Pages, database is not used. In dynamic web pages, database is used. 6. Static web pages are written in languages such as: HTML, JavaScript, CSS, etc. Dynamic web pages are written in languages such as: CGI, AJAX, ASP, ASP.NET, etc. 7. Static web pages does not contain any application program . Dynamic web pages contains application program for different services. 8. Static web pages require less work and cost in designing them. Dynamic web pages require comparatively more work and cost in designing them.
  • 9. 9 Introduction to JSP • Java Server Pages (JSP) is a server side technology for developing dynamic web pages. • This is mainly used for implementing presentation layer (GUI Part) of an application. • A complete JSP code is more like a HTML with bits of java code in it. • JSP is an extension of servlets and every JSP page first gets converted into servlet by JSP container before processing the client’s request. • JSP technology is used to create web application just like Servlet technology. • It can be thought of as an extension to Servlet because it provides more functionality than servlet such as expression language, JSTL, etc. • A JSP page consists of HTML tags and JSP tags. • The JSP pages are easier to maintain than Servlet because we can separate designing and development. It provides some additional features such as Expression Language, Custom Tags, etc • JSP have access to the entire family of Java APIs, including the JDBC API to access enterprise databases • JSP runs on tomcat server. • JSP pages are saved with .jsp extension which lets the server know that this is a JSP page and needs to go through JSP life cycle stages.
  • 10. 10 Architecture of a JSP Application • Based on the location where request processing happens (Servlet OR JSP(java server pages)) there are two architectures for JSP. • They are – Model1 Architecture & Model2 Architecture. • Model1 Architecture: In this Model, JSP plays a key role and it is responsible for of processing the request made by client. Client (Web browser) makes a request, JSP then creates a bean object which then fulfils the request and pass the response to JSP. JSP then sends the response back to client. Unlike Model2 architecture, in this Model most of the processing is done by JSP itself.
  • 11. 11 Architecture of a JSP Application Contd… • Model2 Architecture: In this Model, Servlet plays a major role and it is responsible for processing the client’s(web browser) request. Presentation part (GUI part) will be handled by JSP and it is done with the help of bean as shown in image. The servlet acts as controller and in charge of request processing. It creates the bean objects if required by the jsp page and calls the respective jsp page. The jsp handles the presentation part by using the bean object. In this Model, JSP doesn’t do any processing, Servlet creates the bean Object and calls the JSP program as per the request made by client..
  • 12. 12 JSP Life Cycle • A JSP life cycle is defined as the process from its creation till the destruction. • The following are the paths followed by a JSP − 1. Compilation 2. Initialization 3. Execution 4. Cleanup
  • 13. 13 JSP Life Cycle Contd… JSP Initialization When a container loads a JSP it invokes the jspInit() method before servicing any requests. If there is need to perform JSP-specific initialization, override the jspInit() method − public void jspInit(){ // Initialization code... } Typically, initialization is performed only once and as with the servlet init method, you generally initialize database connections, open files, and create lookup tables in the jspInit method.
  • 14. 14 JSP Life Cycle Contd… JSP Compilation When a browser asks for a JSP, the JSP engine first checks to see whether it needs to compile the page. If the page has never been compiled, or if the JSP has been modified since it was last compiled, the JSP engine compiles the page. The compilation process involves three steps − Parsing the JSP. Turning the JSP into a servlet. Compiling the servlet.
  • 15. 15 JSP Life Cycle Contd… JSP Execution • This phase of the JSP life cycle represents all interactions with requests until the JSP is destroyed. • Whenever a browser requests a JSP and the page has been loaded and initialized, the JSP engine invokes the _jspService() method in the JSP. • The _jspService() method takes an HttpServletRequest and an HttpServletResponse as its parameters as follows − void _jspService(HttpServletRequest request, HttpServletResponse response) { // Service handling code... } • The _jspService() method of a JSP is invoked on request basis. This is responsible for generating the response for that request and this method is also responsible for generating responses to all seven of the HTTP methods, i.e, GET, POST, DELETE, etc..
  • 16. 16 JSP Life Cycle Contd… JSP Cleanup • The destruction phase of the JSP life cycle represents when a JSP is being removed from use by a container. • The jspDestroy() method is the JSP equivalent of the destroy method for servlets. Override jspDestroy when you need to perform any cleanup, such as releasing database connections or closing open files. • The jspDestroy() method has the following form − public void jspDestroy() { // Your cleanup code goes here. }
  • 18. 18 JSP Elements • The Scriptlet • JSP Declaration • JSP Expression • JSP Comments • JSP Directives • JSP Actions • JSP Implicit Objects
  • 19. 19 JSP Scriptlet • A scriptlet can contain any number of JAVA language statements, variable or method declarations, or expressions that are valid in the page scripting language. • Following is the syntax of Scriptlet − <% code fragment %> • For XML the syntax is <jsp:scriptlet> code fragment </jsp:scriptlet> <html> <head>JSP Scriptlet Demo</title></head> <body> JSP Scriptlet Demo <br/> <% out.println("Your IP address is " + request.getRemoteAddr()); %> </body> </html>
  • 20. 20 JSP Declaration • Declaration tag is a block of java code for declaring class wide variables, methods and classes. • Whatever placed inside these tags gets initialized during JSP initialization phase and has class scope. • JSP container keeps this code outside of the service method (_jspService()) to make them class level variables and methods. • Following is the syntax for JSP Declarations − <%! declaration; [ declaration; ]+ ... %> • For XML The syntax is: <jsp:declaration> code fragment </jsp:declaration> <html> <head><title>JSP Declaration Demo </title></head> <body> JSP Declaration Demo<br/> // Variable Declaration <%! int i = 0; %> <%! int a, b, c; %> <%! Circle a = new Circle(2.0); %> // function Declaration <%! int sum(int num1, int num2, int num3){ return num1+num2+num3; } %> </body> </html>
  • 21. 21 JSP Expression • Expression tag evaluates the expression placed in it, converts the result into String and send the result back to the client through response object. Basically it writes the result to the client(browser). • Syntax of expression tag in JSP: • <%= expression %> <html> <head> <title>JSP expression tag example</title> </head> <body> //Expression of values <%= 2+4*5 %> //Expression of variable <% int a=10; int b=20; int c=30; %> <%= a+b+c %> </body> </html>
  • 22. 22 JSP Comment • JSP comment marks text or statements that the JSP container should ignore. A JSP comment is useful when you want to hide or "comment out", a part of the JSP page. • Following is the syntax of the JSP comments − • <%-- This is JSP comment --%> <html> <head> <title>JSP Comment</title> </head> <body> <h2>A Test of Comments</h2> <%-- This comment will not be visible in the page source --%> </body> </html>
  • 23. 23 JSP Directive • Directives control the processing of an entire JSP page. It gives directions to the server regarding processing of a page. • Directives can have a number of attributes which programmer can list down as key-value pairs and separated by commas. • The blanks between the @ symbol and the directive name, and between the last attribute and the closing %>, are optional. • Syntax of Directives: <%@ directive name [attribute name=“value” attribute name=“value” ........]%> • There are three types of Directives in JSP: 1) Page Directive <%@ page ... %> 2) Include Directive <%@ include ... %> 3) TagLib Directive <%@ taglib ... %>
  • 24. 24 Page Directive • The page directive is used to provide instructions to the container. These instructions pertain to the current JSP page. • One may code page directives anywhere in JSP page. By convention, page directives are coded at the top of the JSP page. • Basic syntax is as: <%@ page attribute = "value" %> • There are several attributes, which are used along with Page Directives and these are – 1. import 2. session 3. isErrorPage 4. errorPage 5. ContentType 6. isThreadSafe 7. buffer 8. extends 9. info 10. language 11. Autoflush, etc
  • 25. 25 Page Directive (import attribute) 1. Import: This attribute is used to import packages. While doing coding there is a need to include more than one packages, In such scenarios this page directive’s attribute is very useful as it allows to mention more than one packages at the same place separated by commas (,). Alternatively multiple instances of page element each one with different package can be coded. Syntax of import attribute – <%@page import="value"%> <%@page import="java.io.*%> <%@page import="java.lang.*%> <%--Comment: OR Below Statement: Both are Same--%> <%@page import="java.io.*, java.lang.*"%>
  • 26. 26 Page Directive (attribute) 2. session: • This attribute is to handle HTTP sessions for JSP pages. It can have two values: true or false. • Default value for this attribute: true • Syntax of session attribute: <%@ page session="value"%> here value is either true OR false 3. isErrorPage: • This attribute is used to specify whether the current JSP page can be used as an error page for another JSP page. • If value of isErrorPage is true it means that the page can be used for exception handling for another page. Generally these pages has error/warning messages OR exception handling codes and being called by another JSP page when there is an exception occurred there. • Default value for this attribute: false • Syntax: • <%@ page isErrorPage="value"%> <%@ page session="true"%> <%@ page session=“false"%> <%@ page isErrorPage=“false"%> <%@ page isErrorPage=“true"%>
  • 27. 27 Page Directive (attribute) 4. ErrorPage: • errorPage attribute is used to specify the URL of a JSP page which has isErrorPage attrbute set to true. It handles the un-handled exceptions in the page. • Syntax of errorPage attribute: <%@ page errorPage="value"%> • Here value is a JSP page name which has exception handling code (and isErrorPage set to true). 5. ContentType • This attribute is used to set the content type of a JSP page. • Default value: text/html • Syntax of contentType attribute: <%@ page contentType="value"%> <%@ page errorPage="ExceptionHandling.jsp"%> <%@ page contentType="text/html"%>
  • 28. 28 Page Directive (attribute) 6. isThreadSafe: • Setting isThreadSafe as true, it means that the JSP page supports multithreading (more than one thread can execute the JSP page simultaneously). On the other hand if it is set to false then JSP engine won’t allow multithreading which means only single thread will execute the page code. • Default value for isThreadSafe attribute: true. • Syntax of isThreadSafe attribute: <%@ page isThreadSafe="value"%> 7. buffer • This attribute is used to specify the buffer size. If you specify this to none during coding then the output would directly written to Response object by JSPWriter. And, if you specify a buffer size then the output first written to buffer then it will be available for response object. • Syntax of buffer attribute: • <%@ page buffer="value"%> // value is size in kb or none. <%@ page isThreadSafe="false"%> <%@ page buffer=“none"%> <%@ page buffer=“5kb”%>
  • 29. 29 Page Directive (attribute) 8. extends: • This attribute is used to inherit class. • Syntax of extends attribute: <%@ page extends="value"%> 9. info • It provides a description to a JSP page. The string specified in info will return when we will call getServletInfo() method. • Syntax of info: <%@ page info="value"%> // value is size in kb or none. <%@ page extends="mypackage.SampleClass"%> <%@ page info="This code is given by Chaitanya Singh"%>
  • 30. 30 Page Directive (attribute) 10. language: • It specifies the scripting language( underlying language) being used in the page.. • Syntax of language attribute: <%@ page language="value"%> Here value is any scripting language 11. autoFlush • If it is true it means the buffer should be flushed whenever it is full. false will throw an exception when buffer overflows. • Default Value: True • Syntax of info: <%@ page autoFlush="value"%> // value can be true or false <%@ page language=“java"%> <%@ page autoFlush=“true”%>
  • 31. 31 include Directive • The include directive is used to include a file during the translation phase. • This directive tells the container to merge the content of other external files with the current JSP during the translation phase. • It can be used anywhere in the page. • Syntax of Include Directive: <%@include file ="value"%>, here value is the JSP file name which needs to be included. • Example: <%@include file="myJSP.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>
  • 32. 32 taglib Directive • The JavaServer Pages API allow to define custom JSP tags that look like HTML or XML tags and a tag library is a set of user-defined tags that implement custom behavior. • The taglib directive declares that the JSP page uses a set of custom tags, identifies the location of the library, and provides means for identifying the custom tags in the JSP page. • The taglib directive follows the syntax given below − <%@ taglib uri="uri" prefix = "prefixOfTag" >, Here, the uri attribute value resolves to a location the container understands and the prefix attribute informs a container what bits of markup are custom actions <%@ taglib uri = "http://www.example.com/custlib" prefix = "mytag" %> <html> <body> <mytag:hello/> </body> </html>
  • 33. 33 JSP Actions • JSP actions use constructs in XML syntax to control the behavior of the servlet engine. • Syntax: <jsp:action_name attribute = "value" /> • There are many JSP action tags or elements. Each JSP action tag is used to perform some specific tasks. • The action tags are used to control the flow between pages and to use Java Bean.
  • 34. 34 JSP Actions JSP Action Tags Description jsp:forward forwards the request and response to another resource. jsp:include includes another resource. jsp:useBean creates or locates bean object. jsp:setProperty sets the value of property in bean object. jsp:getProperty prints the value of property of the bean. jsp:plugin embeds another components such as applet. jsp:param sets the parameter value. It is used in forward and include mostly. jsp:fallback can be used to print the message if plugin is working. It is used in jsp:plugin.
  • 35. 35 JSP implicit objects • JSP implicit Objects are the Java objects that the JSP Container makes available to the developers in each page and the developer can call them directly without being explicitly declared. JSP Implicit Objects are also called pre-defined variables. • These objects are created by JSP Engine during translation phase (while translating JSP to Servlet). They are being created inside service method so can be directly used within Scriptlet without initializing and declaring them. • There are total 9 implicit objects available in JSP. Object Corresponding Class out javax.servlet.jsp.JspWriter request javax.servlet.http.HttpServletRequest response javax.servlet.http.HttpServletResponse session javax.servlet.http.HttpSession applicationjavax.servlet.ServletContext exception javax.servlet.jsp.JspException page java.lang.Object pageConte xt javax.servlet.jsp.PageContext config javax.servlet.ServletConfig