You are on page 1of 30

JAVA SERVER

PAGES
OBJECTIVES – SESSION 1

• Need for JSP


• Life cycle of JSP application
• Components of JSP
• Classes in JSP API
• Steps to create and deploy a JSP application

2
JSP TECHNOLOGGY

• Introduction to JSP Technology :

• JSP technology facilitates the segregation of the work


profiles of a Web designer and a Web developer.
• A Web designer can design and formulate the layout for a
Web page by using HTML.
• A Web developer, working independently, can use Java
code and other JSP specific tags to code the business logic.

3
JSP TECHNOLOGY

• Differences between servlets and JSP are:


• Servlets tie up files to independently handle the static
presentation logic and the dynamic business logic. On the
other hand, JSP allows Java to be embedded directly into an
HTML page by using special tags.

• Servlet programming involves extensive coding. Any changes


made to the code requires identification of the static code
content and dynamic code content to facilitate incorporation of
the changes. On the other hand, a JSP page, by virtue of the
separate placement of the static and dynamic content,
facilitates both Web developers and the Web designer to work
independently.

4
JSP LIFE CYCLE

• JSP Life Cycle is represented as shown:

5
JSP LIFE CYCLE

• JSP life cycle methods are:

• jspInit(): Is invoked at the time when the servlet is initialized.


• jspService(): Is invoked when request for the JSP page is
received.
• jspDestroy(): Is invoked before the servlet is removed from the
service.

6
JSP PAGE - SAMPLE

• The following code shows the sample structure of a JSP Page:

<%--This is the HTML content--%>


<%@ page language=”java” %>
<HTML>
<HEAD><TITLE>Simple JSP example></TITLE></HEAD>
<BODY>
<H1>This is a code within the JSP tags to display the server time</H1>
<%--This is the JSP content that displays the server time by using the Date
class of the java.util package--%>
<% java.util.Date now=new java.util.Date(); %>
<H2><%= now.getHours() %>:<% =now.getMinutes() %>:<%
=now.getSeconds() %></H2>
</BODY>
</HTML>

7
COMPONENTS OF A JSP PAGE

Components of a JSP Page

• The three components of a JSP page are:

• JSP directives
• JSP scripting
• JSP actions

8
DIRECTIVE

• A directive element in a JSP page provides global information


about a particular JSP page and is of three types:

• page Directive
• taglib Directive
• include Directive

• The syntax for defining a directive is:


<%@ directive attribute=”value” %>

9
PAGE DIRECTIVE

• The page Directive

• Defines attributes that notify the Web container about the


general settings of a JSP page.

• The syntax of the page directive is:


<%@ page attribute_list %>

10
PAGE DIRECTIVE

• The following table describes the attributes supported by the


page directive:

Attribute Name Description

language Defines the scripting language of the JSP page.

extends Defines the extended parent class of the JSP


generated servlet.

import Imports the list of packages, classes, or interfaces


into the generated servlet.

session Specifies if the generated servlet can access the


session or not. An implicit object, session, is
generated if the value is set to true. The default
value of session attribute is true.

11
PAGE DIRECTIVE

Attributes supported by the page directive are (Contd.):

Attribute Name Description

buffer Specifies the size of the out buffer. If size is


set to none, no buffering is performed. The
default value of buffer size is 8 KB.

autoFlush Specifies that the out buffer be flushed


automatically if the value is set to true. If
the value is set to false, an exception is
raised when the buffer is full. The default
value of autoFlush attribute is true.
isThreadSafe Specifies whether a JSP page is thread safe
or not.

12
PAGE DIRECTIVE

Attributes supported by the page directive are (Contd.):

Attribute Name Description

errorPage Specifies that any un-handled exception


generated will be directed to the URL.

isErrorPage Specifies that the current JSP page is an error


page, if the attribute value is set to true. The
default value of isErrorPage attribute is false.

contentType Defines the Multipurpose Internal Mail


Extension (MIME) type for a response. The
default value of the contentType attribute is
 
text/html.

13
INCLUDE DIRECTIVE

• The include Directive

• Specifies the names of the files to be inserted during the


compilation of the JSP page.
• Creates the contents of the included files as part of the
JSP page.
• Inserts a part of the code that is common to multiple
pages.
• The syntax of the include directive is:
<%@ include file = ”URLname” %>

14
TAGLIB DIRECTIVE

• The taglib Directive

• Imports a custom tag into the current JSP page.


• Associates itself with a URI to uniquely identify a custom
tag.
• Associates a tag prefix string that distinguishes a custom tag
with the other tag library used in a JSP page.
• The syntax to import a taglib directive in the JSP page is:
<%@ taglib uri=“tag_lib_URI” prefix=”prefix” %>

15
TAGLIB DIRECTIVE

• The following table describes the attributes of the taglib


directive:

Attribute Description

uri Locates the TLD file of a custom tag.

prefix Defines a prefix string to be used for


  distinguishing a custom tag instance.

16
JSP SCRIPTING ELEMENTS

JSP Scripting Elements


• Embed Java code directly into an HTML page.
• Include various scripting elements, which are:
• Declarations: Provide a mechanism to define variables and
methods. Declarative statements are placed within
<%! and %> symbols and always end with a semicolon.
• Expressions: Insert values directly into the output. The
syntax to include a JSP expressions in the JSP file is:
<%= expression%>
• Scriptlets: Consists of valid Java code snippets that are
enclosed within <% and %> symbols. The syntax to declare
JSP scriptlets to include valid Java code is:
<% Java code %>

17
JSP IMPLICIT OBJECTS

• JSP Implicit Objects are:

• Pre-defined variables that can be included in JSP


expressions and scriptlets.

• Implemented from servlet classes and interfaces.

18
JSP IMPLICIT OBJECTS

• The following table describes various JSP implicit objects:

Implicit Object Class Description

application javax.servlet.Servlet The application object defines a Web


Context application. Usually, it is the
application in the current Web context.

config javax.Servlet.Servlet Represents the object of a


Config ServletConfig class.

exception java.lang.Throwable Represents the Throwable exception,


in a JSP page.
 

19
JSP IMPLICIT OBJECTS

• The following table describes various JSP implicit objects:

Implicit Object Class Description

out javax.servlet.jsp.Jsp Represents an object of JspWriter to


Writer send response to the client.
JspWriter extends the PrintWriter
class and is used by JSP pages to
send client responses.

page java.lang.Object Represents the current instance of the


JSP page that in turn is used to refer
to the current instance of the
generated servlet.

session javax.servlet.http.Ht Represents a session object of


tpSession HttpSession interface.

20
JSP IMPLICIT OBJECTS

• The following table describes various JSP implicit objects:

Implicit Object Class Description

response javax.servlet.http.Ht Represents a response object of


tpServletResponse HttpServletResponse that is used to
send an HTML output to the client.

request javax.servlet.http.Ht Represents a request object of


tpServletRequest HttpServletRequest. It is used to
retrieve data submitted along with a
request.

pageContext javax.servlet.jsp.Pag Represents a page context for a JSP


eContext page.
 

21
JSP API

Programming in JSP

• Classes of JSP API

• The ErrorData Class


• The JSPWriter Class
• The PageContext Class

22
ERRORDATA CLASS

The ErrorData Class


• Defines error information for error pages.
• Sets the value of the page directive, isErrorPage to true, to indicate
that a page is an error page.
• Contains various methods, which are:
• getRequestURL(): Returns the requested URL in the form of a
string.
• setServletName(): Returns the name of the servlet invoked in the
form of a string.
• getStatusCode(): Returns the status code of the error in the form
of an integer.
• getThrowable(): Returns the Throwable exception that caused
the error.

23
JSPWRITER CLASS

• The JspWriter Class


• Writes action and template data in a JSP page.
• Refers the object of JspWriter class by the implicit variable, out.
• Contains various methods, which are:
• clear()
• close()
• flush()
• getBufferSize()
• print()
• println()

24
PAGECONTEXT CLASS

• The PageContext Class


• Provides context information when JSP is used in the servlet
environment
• Contains various methods, which are:
• forward()
• getPage()
• getRequest()
• getResponse()
• getServletConfig()
• getServletContext()
• getSession()
• include()
25
JSP APPLICATION - STEPS

• Steps to Create a JSP Application


• Create a user interface using HTML: Accepts inputs from the
user. The user inputs are passed as a request to the JSP page.
• Create a JSP page: Provides the business logic to process the
request passed from the HTML interface. In addition, the JSP page
sends the response back to the Web browser.
• Package the user interface and JSP page: Packs the HTML and
JSP page using the deploytool utility to create a JSP application.
• Deploy the package you have created: Deploys the JSP
application on the (J2EE1.4) Application Server to access its
services.
• Access the application using a Web browser: Accesses the JSP
application using a Web browser.

26
EXAMPLE

Developing a JSP Application


•Problem Statement
• John Barrett, the Chief Technology Officer (CTO) has entrusted the
development team with the task of creating an application that
validates the customer id and password of every customer before
they can access their account details. The customer id has to be in a
numeric form. He also wants that an error message should be
displayed to the customer, if the entered customer id or password is
incorrect. Before the changes can be made to the entire application,
John wants to test this functionality by making a sample application
for a specific customer. Larry Williams, the programmer has been
assigned the task of implementing this functionality. Larry decides
to use JSP for developing this application and incase any error
occurs, the details of the error should be displayed.

27
ERRORDATA CLASS

Developing a JSP Application (Contd.)


• Solution
• To solve the given problem, perform the following tasks:
1. Create a Login Page Using HTML.
2. Create an Authentication Page Using JSP.
3. Create an Error Page Using JSP.
4. Package the JSP Application.
5. Deploy the JSP Application.
6. Access the JSP Application.

28
THANK YOU
Q&A

You might also like