0% found this document useful (0 votes)
6 views26 pages

Introduction to JSP Concepts and Lifecycle

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

Introduction to JSP Concepts and Lifecycle

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

Department of

CSE
ADVANCED OBJECT
ORIENTED PROGRAMMING
LANGUAGE
22CS2116AA

Topic:

JSP
Session - 1

CREATED BY K. VICTOR BABU


AIM OF THE
SESSION
To familiarize students with the basic concept of JSP & Advantages over servlets, Features, syntax, Life
Cycle of JSP, Environmental Setup for JSP, Interaction between client, JSP & server.

INSTRUCTIONAL
OBJECTIVES

This Session is designed to:


1. Demonstrate
2. Describe
3. List out the
4. Describe the

LEARNING OUTCOMES

At the end of this session, you should be able to:


1. Define
2. Describe
3. Summarize

CREATED BY K. VICTOR BABU


Why JSP?

 Increases the Productivity


 JSP will separate presentation logic and Business logic
 We can develop application without writing java code (Custom Tags)
 It provides additional features than Servlet like Expression Language, Custom Tags
etc.

CREATED BY K. VICTOR BABU


Advantages of JSP

 Extension to Servlet
 An extension to servlet with additional features like implicit objects, predefined
tags, expression language and Custom tags in JSP, that makes JSP development
easy.
 Easy to maintain
 Business Logic and Presentation Logic are separated in JSP Unlike Servlet.
 Fast Development
 No recompilation and redeployment if JSP page is modified unlike Servlet code as
it needs to be recompiled if modified.
 Less Code than Servlet
 Tags in JSP will reduce the amount of code.

CREATED BY K. VICTOR BABU


Phases in JSP Page Lifecycle

1. Translation of JSP Page

2. Compilation of JSP Page

3. Classloading (the classloader loads class file)

4. Instantiation (Object of the Generated Servlet is created).

5. Initialization ( the container invokes jspInit() method).

6. Request processing ( the container invokes _jspService() method).

7. Destroy ( the container invokes jspDestroy() method).

CREATED BY K. VICTOR BABU


Phases in JSP Page Lifecycle

CREATED BY K. VICTOR BABU


Lifecycle of JSP

JSP page  Servlet(.java file)  .class file  JRE(loading) Processing(_jspService())

nitialization(jspInit())  instantiation  cleaning(jspDestroy())

CREATED BY K. VICTOR BABU


JSP and Webserver(Servlet
Container)

 Webserver always expect a java file but JSP is not a java program.
 Webserver automatically creates an equivalent java program for the JSP file

JSP ELEMENTS :
1. Template text
2. JSP Scriplet
3. JSP Declaration
4. JSP Directive
5. JSP Expression
6. JSP Action Tags
7. JSP Custom Tags

CREATED BY K. VICTOR BABU


JSP ELEMENTS(Conti..)

1. Template text

A text/code that is directly written inside the jsp file but outside the tags.
This template text will directly shown in the browser when executed.
Template text will be stored inside [Link](“ “ ) inside _jspService() when converted
to Java program.

2. JSP Scriplet tag(Scripting element)

In JSP we will write java code inside scriplet tag.


Syntax : <% java code %>
JSP identifies the text written inside the scriplet tag as java code.
The scriplet tag code will be stored inside _jspService() but outside [Link]().

CREATED BY K. VICTOR BABU


JSP ELEMENTS(Conti..)

3. JSP Declaration tag(Scripting element)


To declare variables and methods (instance / static ), we use JSP Declarations.
Syntax : <%! variables/methods %>
JSP will place these variables/methods inside class in the equivalent java program.
What will happen if I place variable in scriplet tag instead of declaration tag?

4. JSP Expression tag(Scripting element)


To evaluate expressions and print the result automatically without writing print
statement, we use JSP Expressions.
Syntax : <%= expression %>
Writing expression in the expression tag means  stores in [Link](expression) in
java file.
CREATED BY K. VICTOR BABU
JSP ELEMENTS(Conti..)

5. JSP Directive Elements

The jsp directives are messages/instructions that tells the web container how to

translate a JSP page into the corresponding servlet(java file).

Syntax : <%@ directive attribute = “value” %>

There are 3 types of directives


 page directive

 include directive

 taglib directive

JSP page directive


page directive : The page directive defines attributes that apply to an entire JSP
page.
Syntax : <%@page attribute = “value” %>
CREATED BY K. VICTOR BABU
JSP ELEMENTS(Conti..)

JSP page directive


Syntax : <%@page attribute = “value” %>
JSP page directive attributes are:

CREATED BY K. VICTOR BABU


JSP ELEMENTS(Conti..)

Page Directive
language : The language attribute specifies the scripting language used in the JSP
page. The default value is "java".
Syntax : <%@ page language = “java” %>
import : The import attribute is used to import class, interface or packages. It is
similar to import keyword in java class or interface.
Syntax : <%@ page import = “[Link].*,[Link].*” %>
extends : The extends attribute defines the parent class that will be inherited by the
generated [Link] is rarely used.
Syntax : <%@ page extends = “classname” %>
session :It indicates that we are writing sessions in JSP.
Syntax : <%@ page session = “true” %>

CREATED BY K. VICTOR BABU


JSP ELEMENTS(Conti..)
info : It sets some information regarding the JSP page that we are writing by using
getServletInfo() method of Servlet interface.
Syntax : <%@ page info = “Any comment that you write” %>
contentType : It indicates the browser regarding the type of response it will receive.
Syntax : <%@ page contentType = “text/html” %>
errorPage : errorPage redirects the error raised in JSP page to another [Link] page where it is
handled and shown to user in an understandable way.
Syntax : <%@ page errorPage = “[Link]” %>
isErrorPage : The isErrorPage attribute is used to declare that the current page is the error
page.
Syntax : <%@ page isErrorPage = “true” %>
isThreadSafe :It will control the multithreaded behaviour of JSP page.
Syntax : <%@ page isThreadSafe = “true” %>
isELIgnored :
Syntax : <%@ page isELIgnored = “true” %>

CREATED BY K. VICTOR BABU


${channel}  prints ${channel} as text
JSP ELEMENTS(Conti..)

Include Directive
The include directive is used to include the contents of any resource it may be jsp file,
html file or text file.
Syntax : <%@ include file = “resourcename” %>
<%@ include file = “[Link]” %>
6. Action tags
 jsp:include
 jsp:forward
 jsp:param
 jsp:useBean
 jsp:setProperty
 jsp:getProperty
CREATED BY K. VICTOR BABU
JSP ELEMENTS(Conti..)

jsp:include
includes another resource
jsp include directive will include the other jsp file at the time of translating the jsp
file to java servlet.
jsp include Action tag will include the file at Runtime.
includes another resource(jsp, html etc) in our current jsp file.
Syntax:

<jsp:include page = “[Link]”/>

CREATED BY K. VICTOR BABU


JSP ELEMENTS(Conti..)
jsp:forward
forwards the request and response from one page to another page.
Syntax:
<jsp:forward page = “[Link]”/> OR
<jsp:forward page = “[Link]”>
</jsp:forward>

jsp:param
To pass parameters from one page to another page.
Syntax:
<jsp:forward page = “[Link]”>
<jsp:param name = “city” value = “Cochin”/>
<jsp:param name = “Course” value = “JSP”/>
</jsp:forward>
CREATED BY K. VICTOR BABU
JSP ELEMENTS(Conti..)

<jsp:forward page = “[Link]”>


<jsp:param name = “city” value = “Cochin”/>
<jsp:param name = “Course” value = “JSP”/>
</jsp:forward>
---------------Holding the parameters in another
file--------------
<% [Link]([Link](“City”));
[Link]([Link](“Course”));
CREATED BY K. VICTOR BABU
JSP ELEMENTS(Conti..)

7. Custom tags
Custom tags are user-defined tags. They eliminates the possibility of scriptlet tag
and separates the business logic from the JSP page.
The same business logic can be used many times by the use of custom tag.

Advantages
[Link] the need of scriptlet tag The custom tags eliminates the need of
scriptlet tag.
[Link] of business logic from JSP The custom tags separate the business
logic from the JSP page so that it may be easy to maintain.
[Link]-usability The custom tags makes the possibility to reuse the same business
logic again and again.
CREATED BY K. VICTOR BABU
JSP ELEMENTS(Conti..)

Syntax:

<prefix:tagname attribute1=value/>
OR
<prefix:tagname attribute1=value>
body code
</prefix:tagname>

CREATED BY K. VICTOR BABU


CONTI..
JSP ELEMENTS(Conti..)

Custom Tag API

CREATED BY K. VICTOR BABU


CONTI..
JSP ELEMENTS(Conti..)

Steps to implement Custom Tag


Create a class that extends SimpleTagSupport class
Override the doTag() method to write logic
Create a tld(tag library descriptor) file(links tag and class)
tld will tells which tag when executed calls which class.

CREATED BY K. VICTOR BABU


SELF-ASSESSMENT QUESTIONS

1. 1. Which page directive should be used in JSP to generate a PDF page?

a) contentType
b) generatePdf
c) typePDF
d) contentPDF

2. Which attribute specifies a JSP page that should process any exceptions thrown
but not caught in the current page?

a)The ErrorPage Attribute


b) The IsErrorPage Attribute
c) Both A & B
d) None of the above

CREATED BY K. VICTOR BABU


TERMINAL QUESTIONS

1. Explain clearly Interaction between client, JSP & server.

2. List out JSP Advantages over servlets?

3. Analyze Life Cycle of JSP?

4. Explain the Environmental Setup for JSP?

CREATED BY K. VICTOR BABU


REFERENCES FOR FURTHER LEARNING OF
THE SESSION

Reference Books:
1.
2.
3.

Sites and Web links:


1.
2.
3.

CREATED BY K. VICTOR BABU


THANK YOU

Team – Course Name

CREATED BY K. VICTOR BABU

You might also like