jsp life cycle _ tech artifact

8
About Disclaimer 141Subscribers  175Fans  26Followers Home Oracle ADF Java .NET Forums Submit articles 0 JSP Life cycle Vinay 17 Jan 2012 J2EE, Java, jsp, JSP Life cycle, lifecycle Views: 927 views About author I am Java/oracle professional.Working on Java/J2EE technologies and i.e Java,J2ee,Oracle ADF,hibernate,J2ee,PL/sql,Apps for 4+ years.I am passionate about learning new technologies.I am sharing my knowledge. Give your views and suggestion on [email protected] http://www.linkedin.com  /in/vinaykumar2 Read more from this author Life cycle of a JSP page consists of two phases, translation phase and execution phase. Every JSP is a Servlet, a JSP page is translated and compiled into servlet and the resulting servlet handles the request, So life cycle of a JSP page largely depends on the Servlet API. JSP engine does the following 7 phases. • Page translation: page is parsed, and a java file which is a servlet is created. • Page compilation: page is compiled into a class file • Page loading : This class file is loaded. • Create an instance : Instance of servlet is created • jspInit() method is called • jspService is called to handle service calls • jspDestroy is called to destroy it when the servlet is not required. T ranslation phase During the translation phase, JSP page is translated into a servlet class. The entire static markup in the JSP page is converted into the code that writes the data to response stream. If you look at the source of the generated Servlet class you will find calls to the out.write() which write data to ServletOutputStream. If you have following HTML code into your JSP page JSP life cycle tutorial It will generate code like out.write(“JSP life cycle tutorial”) SP Life cycle | Techartifact http://www.techartifact.com/blogs/2012/01/jsp-life-cycle.html 1 of 7 09-03-2012 04:51

Upload: prajwal-praj-g

Post on 06-Apr-2018

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: JSP Life Cycle _ Tech Artifact

8/2/2019 JSP Life Cycle _ Tech Artifact

http://slidepdf.com/reader/full/jsp-life-cycle-tech-artifact 1/7

About

Disclaimer

141Subscribers 175Fans  26Followers

Home

Oracle ADF

Java

.NET

Forums

Submit articles

0

JSP Life cycle

Vinay 17 Jan 2012 J2EE, Java, jsp, JSP Life cycle, lifecycle Views: 927 views

About author I am Java/oracle professional.Working on Java/J2EE technologies and i.e Java,J2ee,Oracle ADF,hibernate,J2ee,PL/sql,Apps for 4+ years.I am

passionate about learning new technologies.I am sharing my knowledge. Give your views and suggestion on [email protected] http://www.linkedin.com

 /in/vinaykumar2 Read more from this author

Life cycle of a JSP page consists of two phases, translation phase and execution phase. Every JSP is a Servlet,

a JSP page is translated and compiled into servlet and the resulting servlet handles the request, So life cycle

of a JSP page largely depends on the Servlet API.

JSP engine does the following 7 phases.

• Page translation: page is parsed, and a java file which is a servlet is created.

• Page compilation: page is compiled into a class file

• Page loading : This class file is loaded.

• Create an instance : Instance of servlet is created

• jspInit() method is called

• jspService is called to handle service calls

• jspDestroy is called to destroy it when the servlet is not required.

Translation phase

During the translation phase, JSP page is translated into a servlet class. The entire static markup in the JSP

page is converted into the code that writes the data to response stream. If you look at the source of the

generated Servlet class you will find calls to the out.write() which write data to ServletOutputStream.

If you have following HTML code into your JSP page

JSP life cycle tutorial

It will generate code like

out.write(“JSP life cycle tutorial”)

ife cycle | Techartifact http://www.techartifact.com/blogs/2012/01/jsp-life-c

09-03-20

Page 2: JSP Life Cycle _ Tech Artifact

8/2/2019 JSP Life Cycle _ Tech Artifact

http://slidepdf.com/reader/full/jsp-life-cycle-tech-artifact 2/7

During the translation phase JSP elements are treated as follows:

• JSP directives controls the behavior of the resultant servlet.

• Scripting elements results into the equivalent Java code.

• Custom tags are converted into the code that calls method on tag handlers.

JSP Page Compilation:

The generated java servlet file is compiled into a java servlet class.

Note: The translation of a JSP source page into its implementation class can happen at any time between

initial deployment of the JSP page into the JSP container and the receipt and processing of a client request for

the target JSP page.

Class Loading:

The java servlet class that was compiled from the JSP source is loaded into the container

Execution phase

JSP life cycle’s execution phase is almost similar to that of the Servlet life cycle, because ultimately it’s a

servlet which is being executed. The Servlet class generated at the end of the translation phase represents the

contract between container and the JSP page. According to the JSP specification the servlet class must

implement the HttpJspPage interface which defines the life cycle methods.

JSP life cycle includes three methods jspInit(), _jspService() and jspDestroy()

Initialization:

 jspInit() method is called immediately after the instance was created. It is called only once during JSP life

cycle.

_jspService() execution:This method is called for every request of this JSP during its life cycle. This is where it serves the purpose of 

creation. Oops! it has to pass through all the above steps to reach this phase. It passes the request and the

response objects. _jspService() cannot be overridden.

 jspDestroy() execution:

This method is called when this JSP is destroyed. With this call the servlet serves its purpose and submits

itself to heaven (garbage collection). This is the end of jsp life cycle.

 jspInit(), _jspService() and jspDestroy() are called the life cycle methods of the JSP.

The HttpJspPage Interface

The javax.servlet.jsp.HttpJspPage contains three methods

Public void jspInit()

This method is invoked when the JSP page is initialized. This method is similar to init() method in servlet. If 

you want to provide initialization for a JSP page, you define this method in declaration part of the JSP page.

But most of the time you will not need to define this method.

public void _jspService

void _jspService(HttpServletRequest request, HttpServletResponse response)ThrowsIOException,

ife cycle | Techartifact http://www.techartifact.com/blogs/2012/01/jsp-life-c

09-03-20

Page 3: JSP Life Cycle _ Tech Artifact

8/2/2019 JSP Life Cycle _ Tech Artifact

http://slidepdf.com/reader/full/jsp-life-cycle-tech-artifact 3/7

ServletException

This method represents the body of the JSP page and invoked at each client request. This method is similar to

service() method in servlet.

Note: You should never provide implementation _jspService() method as web container automatically

generates this method based on the content of the JSP page

Public void jspDestroy()

This method is invoked just before the JSP page is destroyed by the web container. This method is similar to

destroy() method in servlet. If you want to provide some cleanup code, you can define this method in

declaration part of the JSP page.

Understanding the Servlet life cycle with an example

This example explains how to execute code at JSP initialization phase and destroy phase of JSP Life Cycle.

It is a simple request counter that displays how many time the page has been called or how many requests the

page has received. The page declares a counter variable of type integer, the counter is set to zero when the

page is initialized. Each time the page receives a request, the value of counter variable is incremented by one

and displayed to user. During each life cycle stage, A message is logged to server log.lifecycle.jsp

123456789

1011121314151617181920

212223242526

<%!int counter;public void jspInit() {

counter = 0;log("The lifecycle jsp has been initialized");

}%>

<html>

<head><title>JSP Life Cycle Example</title>

</head><body>

<%log("The lifecycle jsp has received a request");counter++;%><h2>JSP Life cycle : Request counter</h2><p>This page has been called <%=counter %> times </p>

</body>

</html><%!public void jspDestroy() {log("The lifecycle jsp is being destroyed");

}%>

?

ife cycle | Techartifact http://www.techartifact.com/blogs/2012/01/jsp-life-c

09-03-20

Page 4: JSP Life Cycle _ Tech Artifact

8/2/2019 JSP Life Cycle _ Tech Artifact

http://slidepdf.com/reader/full/jsp-life-cycle-tech-artifact 4/7

  5 Votes

Related Posts

JSP EL expression is not evaluated

24 Jan 2012

SpringMVC example with Maven

23 Jan 2012

Servlet lifecycle

15 Jan 2012

Leave a Reply

Name (*)

Email (will not be published) (*)

Website

 18 1 64 74share

ife cycle | Techartifact http://www.techartifact.com/blogs/2012/01/jsp-life-c

09-03-20

Page 5: JSP Life Cycle _ Tech Artifact

8/2/2019 JSP Life Cycle _ Tech Artifact

http://slidepdf.com/reader/full/jsp-life-cycle-tech-artifact 5/7

 

Follow @techartifact 25 followers

Tags

.NET ADF ajax ASP.NET ASP.NET 2.0 ASP.NET 4.0 build Builder C#.NET C#.NET 2.0 code example Design

Pattern Email entity object extjs filter HashMap in Java Interview Question J2EE JAAS Java Java design Pattern JavaMail Api

Kingsley Tisha Chandrabhu Garv it Priyank  

Ramit Badr Partha Sarat Shreyanshi Selvaraj

Techartifact on Facebook 

176 people like Techartifact.

Like

Facebook social plugin

ife cycle | Techartifact http://www.techartifact.com/blogs/2012/01/jsp-life-c

09-03-20

Page 6: JSP Life Cycle _ Tech Artifact

8/2/2019 JSP Life Cycle _ Tech Artifact

http://slidepdf.com/reader/full/jsp-life-cycle-tech-artifact 6/7

Javascript Jetspeed2 Jquery Ldap logger Memory Microsoft Mysql Open Portal Open Source Oracle

ADF Oracle ADF interview Question Pl/Sql portal Security servlets Spring sql server Tips view object Web DevelopmentXML

Jobs

Senior Software

Engineer

Bangalore, India

SuccessFactors

Senior QA Engineer

Bangalore, India

SuccessFactors

Lead QA Engineer

Bangalore, India

SuccessFactors

QA Engineer

Bangalore, India

SuccessFactors

Senior SoftwareEngineer

Bangalore, India

SuccessFactors

QA Engineer

Bangalore, India

SuccessFactors

POST A JOB >

POWERED BY JOBTHREAD

ife cycle | Techartifact http://www.techartifact.com/blogs/2012/01/jsp-life-c

09-03-20

Page 7: JSP Life Cycle _ Tech Artifact

8/2/2019 JSP Life Cycle _ Tech Artifact

http://slidepdf.com/reader/full/jsp-life-cycle-tech-artifact 7/7

Go To Top

Copyright 2009-2012 Techartifact.com

Disclaimer

About

ife cycle | Techartifact http://www.techartifact.com/blogs/2012/01/jsp-life-c