Você está na página 1de 3

The Life Cycle of a JSP Page

A JSP page services requests as a servlet. Thus, the life cycle and many of the
capabilities of JSP pages (in particular the dynamic aspects) are determined by Java
Servlet technology. You will notice that many sections in this chapter refer to classes
and methods described in Chapter 11.
When a request is mapped to a JSP page, the web container first checks whether the
JSP page's servlet is older than the JSP page. If the servlet is older, the web container
translates the JSP page into a servlet class and compiles the class. During
development, one of the advantages of JSP pages over servlets is that the build process
is performed automatically.
Translation and Compilation
During the translation phase each type of data in a JSP page is treated differently. Static
data is transformed into code that will emit the data into the response stream. JSP
elements are treated as follows:
Directives are used to control how the web container translates and executes the JSP
page.
Scripting elements are inserted into the JSP page's servlet class. See Chapter 16 for
details.
Expression language expressions are passed as parameters to calls to the JSP
expression evaluator.
jsp:[set|get]Property elements are converted into method calls to JavaBeans
components.
jsp:[include|forward] elements are converted into invocations of the Java Servlet API.
The jsp:plugin element is converted into browser-specific markup for activating an
applet.
Custom tags are converted into calls to the tag handler that implements the custom tag.

1. What is JSP? Describe its concept. JSP is a technology that combines


HTML/XML markup languages and elements of Java programming Language to
return dynamic content to the Web client, It is normally used to handle
Presentation logic of a web application, although it may have business logic.
2. What are the lifecycle phases of a JSP?
JSP page looks like a HTML page but is a servlet. When presented with JSP
page the JSP engine does the following 7 phases.
1. Page translation: -page is parsed, and a java file which is a servlet is
created.
2. Page compilation: page is compiled into a class file
3. Page loading : This class file is loaded.
4. Create an instance :- Instance of servlet is created
5. jspInit() method is called
6. _jspService is called to handle service calls
7. _jspDestroy is called to destroy it when the servlet is not required.
3. What is a translation unit? JSP page can include the contents of other HTML
pages or other JSP files. This is done by using the include directive. When the
JSP engine is presented with such a JSP page it is converted to one servlet
class and this is called a translation unit, Things to remember in a translation unit
is that page directives affect the whole unit, one variable declaration cannot
occur in the same unit more than once, the standard action jsp:useBean cannot
declare the same bean twice in one unit.
Hi,

What is the difference between page and pageContext implicit object?

page deals with the jsp page. Once the page has send back the response back to the
browser or once it forward the request to another resource the page will die.
The same thing also applies for pageContext where pageContext have more features
like set & get, forward,include,etc since it actaully extends JspContext class.

This much i know. Can u give me an example for page implicit object??

A PageContext instance provides access to all the namespaces associated with a JSP
page, provides access to several page attributes, as well as a layer above the
implementation details

It provides
a single API to manage the various scoped namespaces
a number of convenience API's to access various public objects
a mechanism to obtain the JspWriter for output
a mechanism to manage session usage by the page
a mechanism to expose page directive attributes to the scripting environment
mechanisms to forward or include the current request to other active components in the

JSP is Java code embedded in HTML; the Java code is compiled (if necessary) and run
by the container on the server and the client only sees the results of that code's
execution mixed in appropriately with the html.

Servlets are compiled pure Java class files (not mixed with HTML) that get posts from
the client and send html to in return.

Both require a container on the server, such as Tomcat, which provides the environment
and VM for the Java program. Download Tomcat from http://jakarta.apache.org/tomcat/
-- it has samples of both.

> can u tell me why Jsp code convert in


> Servlets code after Compile.

All your code must be compiled for the VM to execute it.

In real time we use servlet as controller and jsp as a View.


jsp is same as HTML.but using java code we can make it DHTML.
here some difference
jsp
separate the prsentation and business logic.(custom tags)
more easy to learn,easy to write
servlet
presentaion and business logic in same place.
everything should embedd in out.println("") it is too complex to write is it?
The initial JspWriter object is associated with the PrintWriter object of the
ServletResponse in a way that depends on whether the page is or is not buffered. If the
page is not buffered, output written to this JspWriter object will be written through to the
PrintWriter directly, which will be created if necessary by invoking the getWriter() method
on the response object. But if the page is buffered, the PrintWriter object will not be
created until the buffer is flushed and operations like setContentType() are legal. Since
this flexibility simplifies programming substantially, buffering is the default for JSP pages.

intitally jspwriter depends on printwriter But the thing diffrence is that if you want the
page is buffered in such cases you can use jspwriter becuase it has the lof of buffering
capabilities rather than printwriter.

eds

No, 25 Steeple Reach


Cathedral Road, Chennai – 86.

LANDMARK: NEAR PRINCE JEWELLERY

Contact person: SUJATHA S

MindTree Consulting Ltd.


Olympia Technology Park
FORTIUS, 7th Floor, Plot No. 1
SIDCO Industrial Estate, Guindy
Chennai – 600 032

Você também pode gostar