Chapter 5 JSP
Chapter 5 JSP
1
INTRODUCTION
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 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..
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
11
Cont..
6. Request processing
7) Destroy
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 %>
➢ 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
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..
25
Cont..
<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
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