0% found this document useful (0 votes)
2 views32 pages

Chapter 5 JSP

Java Server Pages (JSP) is a technology for creating dynamic web content by embedding Java code within HTML or XML. The JSP lifecycle includes stages such as translation, compilation, initialization, request processing, and destruction, allowing for efficient handling of web requests. JSP elements include declarations, scriptlets, expressions, comments, directives, and implicit objects, which facilitate the development and management of web applications.

Uploaded by

rahatigu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views32 pages

Chapter 5 JSP

Java Server Pages (JSP) is a technology for creating dynamic web content by embedding Java code within HTML or XML. The JSP lifecycle includes stages such as translation, compilation, initialization, request processing, and destruction, allowing for efficient handling of web requests. JSP elements include declarations, scriptlets, expressions, comments, directives, and implicit objects, which facilitate the development and management of web applications.

Uploaded by

rahatigu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Advanced Programming

Chapter 5: JSP(Java Server


Page)

1
INTRODUCTION

o Java Server Pages (JSP) is a technology for developing Webpages


that supports dynamic content.
o Dynamic content includes some fields like dropdown, checkboxes,
etc. whose value will be fetched from the database.
o This can also be used to access JavaBeans objects.
o We can share information across pages using request and response
objects.
o JSP can be used for separation of the view layer with the business
logic in the web application.

2
CONT..
o It can consist of either HTML or XML (combination of both is also
possible) with JSP actions and commands.
Difference between Servlet and JSP

3
CONT..

o A JSP is just another servlet, and like HTTP servlets, a JSP is a


serverside Web component that can be used to generate dynamic
Web pages.
o The fundamental difference between servlets and JSPs is:
o Servlets generate HTML from Java code.
o JSPs embed Java code in static HTML.
o A JavaServer Pages (JSP) component is a type of Java servlet that
is designed to fulfill the role of a user interface for a Java web
application.
o Web developers write JSPs as text files that combine HTML or
XHTML code, XML elements, and embedded JSP actions and
commands
4
JSP ─ LIFECYCLE

o It is the process of translating JSP Page into servlet as a JSP Page


needs to be converted into servlet first in order to process the
service requests.
o The Life Cycle starts with the creation of JSP and ends with the
disintegration of that.
o When the browser asks for a JSP, JSP engine first checks whether
it needs to compile the page.
o If the JSP is last compiled or the recent modification is done in
JSP, then the JSP engine compiles the page.
o Compilation process of JSP page involves three steps:
o Parsing of JSP
o Turning JSP into servlet
o Compiling the servlet 5
Cont..

➢SP Lifecycle is depicted in the below diagram.

JSP Life Cycle = Convert → Compile → Load → Init →


Service → Destroy 6
Cont..

Following steps explain the JSP life cycle:


o Translation of JSP page
o Compilation of JSP page(Compilation of JSP page into ...jsp.java).
o Classloading (...jsp.java is converted to class file ...jsp.class)
o Instantiation(Object of generated servlet is created).
o Initialisation(...jspinit() method is invoked by container).
o Request Processing(...jspservice() method is invoked by the container).
o Destroy (...jspDestroy() method invoked by the container.
Let us have more detailed summary on the above points:
1) Translation of the JSP Page:
A Java servlet file is generated from a JSP source file. In translation phase,
container validates the syntactic correctness of JSP page and tag files.
7
Cont..

o The JSP container interprets the standard directives and actions, and
the custom actions referencing tag libraries (they are all part of JSP
page and will be discussed in the later section) used in this JSP page.
o In the above pictorial description, demo.jsp is translated to demojsp.java
in the first step Let’s take an example of “demo.jsp” as shown below:

8
Cont..

2) Compilation of the JSP Page:


o The generated java servlet file is compiled into java servlet class
o The translation of java source page to its implementation class can happen
at any time between the deployment of JSP page into the container and
processing of the JSP page.
o In the above pictorial description demojsp.java is compiled to a class
file demojsp.class
3) Classloading
o Servlet class that has been loaded from JSP source is now loaded into
the container.

9
Cont..

4) Instantiation
o In this step the object i.e. the instance of the class is generated.
o The container manages one or more instances of this class in the response
to requests and other events.
o Typically, a JSP container is built using a servlet container
o A JSP container is an extension of servlet container as both the container
support JSP and servlet.
o A JSPPage interface which is provided by container provides init() and
destroy () methods.
o There is an interface HttpJSPPage which serves HTTP requests, and it also
contains the service method

10
Cont..

5) Initialization

o jspinit() method will initiate the servlet instance which was


generatedfrom JSP and will be invoked by the container in this
phase.
o Once the instance gets created, init method will be invoked
immediately after that
o It is only called once during a JSP life cycle, the method for
initialization is declared as shown above

11
Cont..

6. Request processing

o ...jspservice() method is invoked by the container for all the requests


raised by the JSP page during its life cycle
o For this phase, it has to go through all the above phases and then only
service method can be invoked.
o It passes request and response objects
o This method cannot be overridden
o The method is shown above: It is responsible for generating of all HTTP
methods i.e. GET, POST, etc.
12
Cont..

7) Destroy

o ...jspdestroy() method is also invoked by the container


o This method is called when container decides it no longer needs
the servlet instance to service requests.
o When the call to destroy method is made then, the servlet is
ready for a garbage collection
o This is the end of the life cycle.
o We can override jspdestroy() method when we perform any
cleanup such as releasing database connections or closing open
13
files.
JSP Elements

JSP Declaration
➢ A declaration tag is a piece of Java code for declaring variables,
methods and classes.
➢ If we declare a variable or method inside declaration tag it means
that the declaration is made inside the servlet class but outside the
service method.
➢ We can declare a static member, an instance variable (can declare a
number or string) and methods inside the declaration tag.
➢ Syntax of Declaration tag <%! Declare variable %>

14
The out put is : Java Server program
wel come to Jsp first program

15
JSP Scriptlet
o Scriptlet tag allows to write Java code into JSP file.
o JSP container moves statements in _jspservice() method while
generating servlet from jsp.
o For each request of the client, service method of the JSP gets
invoked hence the code inside the Scriptlet executes for every
request.
o A Scriptlet contains java code that is executed every time JSP is
invoked.
o Syntax of Scriptlet tag: <% java code %>
o Here <% %> tags are scriplets tag and within it, we can place java
code.

16
JSP Expression
o Expression tag evaluates the expression placed in it.
o It accesses the data stored in stored application.
o It allows create expressions like arithmetic and logical.
o It produces scriptless JSP page.
o Syntax: <% = expression %>

The expression number is 55913


17
JSP Comments

➢ Comments are the one when JSP container wants to ignore certain
texts and statements.
➢ When we want to hide certain content, then we can add that to the
comments section.
➢ Syntax: <% -- JSP Comments --%>
➢ This tags are used to comment in JSP and ignored by the JSP
container

18
Creating a simple JSP Page
➢ A JSP page has an HTML body incorporated with Java code into it.
➢ We are creating a simple JSP page which includes declarations,
scriplets, expressions, comments tags in it.

Example:

OUT PUT
19
Directory Structure of JSP

➢ In directory structure, there is a root folder which has folder


WEB-INF, which has all configuration files and library files.
➢ JSP files are outside WEB-INF folder

20
JSP Directives
➢ JSP directives are the messages to JSP container.
➢ They provide global information about an entire JSP page.
➢ JSP directives are used to give special instruction to a container for
translation of JSP to servlet code.
➢ In JSP life cycle phase, JSP has to be converted to a servlet which is the
translation phase.
➢ They give instructions to the container on how to handle certain aspects
of JSP processing
➢ Directives can have many attributes by comma separated as key-value
pairs.
➢ In JSP, directive is described in <% %> tags.
➢ Syntax: <% directive attribute="" %>
21
JSP Implicit Objects
o JSP implicit objects are created during the translation phase of
JSP to the servlet.
o These objects can be directly used in scriplets that goes in the
service method.
o They are created by the container automatically, and they can be
accessed using objects
Four scopes
1) Application scope:
o Objects owned by the container application
o Any servlet or JSP can manipulate these objects
2) Page scope
o Objects that exist only in page in which they are defined.
o Each page has its own instance of these objects

22
CONT..

3)Request scope
o Objects exist for duration of client request
o Objects go out of scope after response sent to client
4)Session scope
o Objects exist for duration of client’s browsing
session
o Objects go out of scope when client terminates
session or when session timeout occurs

23
CONT..

Figure: JSP Implicit Objects


24
JSP Standard Actions Tags

o JSP standard actions (most common tasks).


o Provide access to common tasks performed in a JSP
o Including content from other resources
o Forwarding requests to other resources
o Interacting with JavaBeans
o JSP containers process actions at request time.
o Delimited by <jsp:action> and </jsp:action>.

25
Cont..

Figure: JSP Standard Actions


26
<jsp:include> A

<jsp:include> action
o Enables dynamic content to be included in a JSP at request
time
o More flexible than include directive (included at translation
time)
o Requires more overhead when page contents change
frequently
<jsp:include page = "toc.html" flush = "true" />
o page is the resource to include
o flush must be true to say to flush buffer after including

27
<jsp:forward> Action

o <jsp:forward> action
o Enables JSP to forward request to different resources
o Can forward requests only to resources in same context
o Original JSP terminates
o <jsp:param> action
o Specifies name/value pairs of information
o Name/Value pairs are passed to other actions

28
<jsp:useBean> Actio

o Enables JSP to manipulate Java object


Creates Java object or locates an existing object for use in JSP
o <jsp:getProperty name = "rotator" property = "link“ /> same as
o <%= rotator.getLink() %>

29
JSP Exception Handling

o Exceptions in JSP occur when there is an error in the code either by the
developer or internal error from the system.
o Exception handling in JSP is the same as in Java where we manage exceptions
using Try Catch blocks.
o Unlike Java, there are exceptions in JSP also when there is no error in the
code.
o Exceptions in JSP are of three types:
o 1) Checked Exception
o FileNotFoundException
o IO Exception
o SQLException
o 2) Runtime Exception
o ArrayIndexOutOfBoundsException
o ArithmeticException and NullPointer Exception
30
CONT…

o Error
o Error
o Instantiation Error
o Internal Error

31
Thank You!

32

You might also like