JSP

JSP

What is Java Server Pages (JSP)

The goal of the Java Server Pages (JSP) specification is to simplicity the creation and management of dynamic
web pages, by separating contents and presentation.
JSPs are basically that combine standard HTML (or XML) and new scripting tags. The presentation is using
HTML while the new tags specify the dynamic contents.
This is not to say that JSPs must contain HTML. A JSP page might be composed of only java code, or of
custom tags that refer to externally compiled classes.
JSP is not a product, but like other Java API’s, a specification provide by the sun Microsystems for vendors to
implement. The JSP specification builds on the functionality provided by the Servlet Specification.
JSPs get translated into Java Servlets the first time they are invoked by a client. From then on, as long as the
JSP source for the page is not modified, this compiled servlet process any browser request for that JSP page.

Servlet v/s JSP
• Servlets execute on the server and intercept browser requests, acting as a sort of middle Layer between
Clients and other applications. In doing so, Servlets tend to mix the dynamic content into the static part
to generate HTML.
• JSPs on the other hand, separate static and dynamic content – separating presentation and logic in a web
application.
• Whereas servlets force you to mix the code with the static content, JSP’s can use beans with a specified
scope to separate the code out, or tag extensions.

ASP v/s JSP
The main competing technology for JSPs is Active Server Page (ASP) , from Microsoft. The technologies are
quite similar in the way they support the creation of Dynamic pages, using HTML templates, scripting code and
components for business logic.

The following table provides a comparison:







Jsp Basic

JSP Scores over the ASP in that:
• JSPs are interpreted only once, to Java byte code and re-interpreted only when the file is modified.
• JSP’s run on all the main web servers.
• JSP’s provide better facilities for separation of page code and template data by mean of Java beans,
EJBs and custom tag libraries.






JSP Life Cycle

When the server receives a request for a JSP, the web server recognizes the JSP file extension in the URL
requested by the browser, indicating that the requested resources is a JSP, and therefore must be handled by the
JSP engine (Web Container).
The JSP page is translated into a Java file, which is then compiled into a servlet. This transaction and
compilation phase occurs only when the JSP file is first called, or when it subsequently changes. You will
notice a slight delay the first time that a JavaServer Page is run because of this conversion process.

Generated Implementation Class and JSP Life Cycle
The Servlet class generated at the end of the translation process must extend a super class that is either
• A JSP container specific implementation class that implements the javax.servlet.jsp.JspPage interface
and provides some basic page specific behavior.
Since most JSP pages use HTTP, their implementation must actually implement the
javax.servlet.jsp.HttpJspPage interface, which is a sub interface of javax.servlet.jsp.JspPage interface.

The javax.servlet.jsp.JspPage interface contains two methods
public void jspInit( )
This is invoked when the JSP is initialized. This is similar to init( ) method in servlets. Page authors are
free to provide initialization of the JSP by implementing this method in their JSP.
public void jspDestroy( )
This is invoked when the JSP is about to be destroyed by the container. This is similar to the
destroy( ) method in servlets. Page authors are free to provide cleanup of the JSP by implementing this
method in their JSPs.
The javax.servlet.jsp.HttpJspPage interface contains a single method:
public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
This method corresponds to the body of the JSP Page and is used for the threaded request processing,
just like the service( ) method in servlets.
The Implementation for this method is generated by the JSP container and should never be provided by
the page authors.

The order in which these three methods work together in a JSP page is given below:
1. jspInit( )
2. _jspService( )
    Every time a request comes to JSP, the container generated _jspService( ) method is invoked, the request is
    processed and the JSP generates the appropriate response. This response is taken by the container and
    passed back to the client.
3. jspDestroy( )
    Note that in most cases. The jspInit( ) and jspDestroy( ) methods do not need to be provided by the JSP
    authors and can be omitted. 
 

JSP Implicit Objects

JSP provides certain implicit objects, based on the Servlet API. These objects are access using standard
variables, and are automatically available for use in our JSP without writing any extra code.
The implicit objects available in a JSP page are:
    • request, response
    • page, session, application
    • pageContext
    • config
    • out
    • exception
The request Object
The request object has request scope, and is an instance of the javax.servlet.ServletRequest class.
It encapsulates the request coming from the Client and being processed by the JSP. It is passed to the JSP by the
container as a parameter of the _jspService( ) method. It has request scope.

The response object
The response object has page scope, and is an instance of javax.servlet.ServletResponse
It encapsulates the response generated by the JSP to be send back to the client in response to the request. It is
generated by the container and passed to the JSP as a parameter to the _jspService( ) method, where it is
modified by the JSP. It is legal to set HTTP status code and headers in the JSP page once output has been send
to the client, as JSP output streams are buffered by default.

The pageContext objects
The pageContext provides a single point of access to many of the page attribute and is convenient place to put
shared data within the page. It is of type javax.servlet.jsp.pageContext and has page scope.

The session object
The session object represents the session created for the requesting client. Session are created automatically,
and a new session is available even when there is no incoming session (unless, of course, you have used a
session=”false” attribute in the page directive, in which case this variable will not be available).
The session object is of type javax.servlet.http.HttpSession and has session scope.

The application object
The application object represents the servlet context, obtained from the servlet configuration object. It is of type
javax.servlet.ServletContext and has application scope.

The out object
The out object is the object that writes into the output stream to the client. To make the response object useful,
this is a buffered version of the javax.servlet.jsp.JspWriter.The buffer size can be adjusted by the
buffer attribute of the page directive.

The config object
The config is the ServletConfig for the JSP page and has page scope. It is of type
javax.servlet.ServletConfig.

The page object
The page object is an instance of the page’s implementation servlet class that is processing the current request.
It is of type java.lang.Object, and has page scope. The page object can be thought of as a synonym to
this within the page.


Scope In JSP
The scope of JSP objects- JavaBeans and implicit objects – is critical, as it defines the how long, and from
which JSP pages, the object will be available.
    • Page Scope
    • Request Scope
    • Session Scope
    • Application Scope


Page Scope
An object with page scope is bound to javax.servlet.jsp.PageContext.
The object is placed in the PageContext object for as long as the page is responding to the current request.
An object with this scope can be accessed by invoking the getAttribute( ) method on the implicit page Context
object. The object is created and destroyed for each client request to the page.
This is the default scope for objects used with the <jsp:useBean> action.

Request Scope
Request scope means that the object is bound to the javax.servlet.ServletRequest, and can be
accessed by invoking the getAttribute( ) method on the implicit request object.
The object is distinct for every client request. The object reference is available as long as the HttpRequest object
exists, even if the request is forwarded to different page, or if the <jsp:include> action is used.

Session Scope

An object with session scope is bound to the javax.servlet.jsp.PageContext, and can be
accessed by invoking getValue( ) method on the implicit session object. The generated servlet relies on binding
the object to the HttpSession using the setAttribute(String key, Object value) method. The object is different for
every client, and is available as long as the client’s session is valid.

Application Scope

Application Scope means that the object is bound to the javax.servlet.ServletContext. An object
with this scope can be accessed by invoking the getAttribute( ) method on implicit application object.
This is the most persistence scope. When accessing application variables, take care your code is thread safe.
Application variables are often populated on application startup, and read – only thereafter.

No comments:

Post a Comment