JSP
1.JavaServer Pages (JSP) is a
server-side programming technology that enables the creation of dynamic,
platform-independent method for building Web-based applications.2.JSP have access to the entire family of Java APIs, including the JDBC API to access enterprise databases.
3.JavaServer Pages (JSP) is a technology for developing web pages that support dynamic content which helps developers insert java code in HTML pages by making use of special JSP tags, most of which start with <% and end with %>
4.JSP
tags can be used for a variety of purposes, such as retrieving information from
a database or registering user preferences, accessing JavaBeans components,
passing control between pages and sharing information between requests, pages
etc.
JSP - Architecture
The
web server needs a JSP engine ie. Container to process JSP pages. The JSP
container is responsible for intercepting requests for JSP pages.
A
JSP container works with the Web server to provide the runtime environment and
other services a JSP needs. It knows how to understand the special elements
that are part of JSPs.
JSP Processing
:
The following steps explain how the
web server creates the web page using JSP:
- As with a normal page, your browser sends an HTTP request to the web server.
- The web server recognizes that the HTTP request is for a JSP page and forwards it to a JSP engine. This is done by using the URL or JSP page which ends with .jsp instead of .html.
- The JSP engine loads the JSP page from disk and converts it into a servlet content.
- The JSP engine compiles the servlet into an executable class and forwards the original request to a servlet engine.
- A part of the web server called the servlet engine loads the Servlet class and executes it. During execution, the servlet produces an output in HTML format, which the servlet engine passes to the web server inside an HTTP response.
- The web server forwards the HTTP response to your browser in terms of static HTML content.
- Finally web browser handles the dynamically generated HTML page inside the HTTP response exactly as if it were a static page.
All the above mentioned steps can be
shown below in the following diagram:
JSP - Life Cycle
The following are the paths followed
by a JSP
- Compilation
- Initialization
- Execution
- Cleanup
(1) JSP Compilation:
When a browser asks for a JSP, the
JSP engine first checks to see whether it needs to compile the page. If the
page has never been compiled, or if the JSP has been modified since it was last
compiled, the JSP engine compiles the page.
The compilation process involves
three steps:
- Parsing the JSP.
- Turning the JSP into a servlet.
- Compiling the servlet.
(2) JSP Initialization:
When a container loads a JSP it
invokes the jspInit() method before servicing any requests. If you need to
perform JSP-specific initialization, override the jspInit() method:
public void jspInit(){
// Initialization
code...
}
|
(3) JSP
Execution:
Whenever a browser requests a JSP
and the page has been loaded and initialized, the JSP engine invokes the _jspService()
method in the JSP.
The _jspService() method takes an HttpServletRequest
and an HttpServletResponse as its parameters as follows:
void _jspService(HttpServletRequest
request,
HttpServletResponse
response)
{
// Service handling code...
}
|
(4) JSP
Cleanup:
Override jspDestroy when you need to
perform any cleanup, such as releasing database connections or closing open
files.
The jspDestroy() method has the
following form:
public void jspDestroy()
{
// Your cleanup code goes here.
}
|
JSP - Syntax
The Scriptlet:
A scriptlet can contain any number of JAVA
language statements, variable or method declarations, or expressions that are
valid in the page scripting language.
the syntax of Scriptlet: <% code fragment %> |
JSP Declarations:
A declaration declares one or more variables or methods that you can use in Java code later in the JSP file. You must declare the variable or method before you use it in the JSP file.Following is the syntax of JSP Declarations:
<%! declaration; [ declaration; ]+ ... %>
|
<%! int i = 0; %>
<%! int a, b, c; %>
<%! Circle a = new Circle(2.0); %>
|
JSP Expression:
A JSP expression element contains a scripting language expression that is evaluated, converted to a String, and inserted where the expression appears in the JSP file.the syntax of JSP Expression:
<%= expression %>
|
<html>
<head><title>A Comment Test</title></head>
<body>
<p>
Today's date: <%= (new java.util.Date()).toLocaleString()%>
</p>
</body>
</html>
|
|
| Output: Today's date: 11-Sep-2010 21:24:25 | |
JSP Comments:
JSP comment marks text or statements that the JSP container should ignore. A JSP comment is useful when you want to hide or "comment out" part of your JSP page.Following is the syntax of JSP comments:
<%-- This is JSP comment --%>
|
JSP Directives:
A JSP directive affects the overall structure of the servlet class. It usually has the following form:<%@ directive attribute="value" %>
|
Directive
|
Description
|
<%@
page ... %>
|
Defines
page-dependent attributes, such as scripting language, error page, and
buffering requirements.
|
<%@
include ... %>
|
Includes
a file during the translation phase.
|
<%@
taglib ... %>
|
Declares
a tag library, containing custom actions, used in the page
|
JSP Actions:
JSP actions use constructs in XML syntax to control the behavior of the servlet engine. You can dynamically insert a file, reuse JavaBeans components, forward the user to another page, or generate HTML for the Java plugin.<jsp:action_name attribute="value" />
|
JSP Implicit Objects:
JSP supports nine automatically defined variables, which are also called implicit objects. These variables are:
Objects
|
Description
|
request
|
This
is the HttpServletRequest object associated with the request. The request object is an instance of a
javax.servlet.http.HttpServletRequest object. Each time a client requests a
page the JSP engine creates a new object to represent that request.
The
request object provides methods to get HTTP header information including form
data, cookies, HTTP methods etc.
|
response
|
This
is the HttpServletResponse object associated with the response to the
client.
The response object is an instance
of a javax.servlet.http.HttpServletResponse object. Just as the server
creates the request object, it also creates an object to represent the
response to the client.
|
out
|
This
is the PrintWriter object used to send output to the client.
The out implicit object is an
instance of a javax.servlet.jsp.JspWriter object and is used to send content
in a response.
The initial JspWriter object is
instantiated differently depending on whether the page is buffered or not.
Buffering can be easily turned off by using the buffered='false' attribute of
the page directive.
The
JspWriter object contains most of the same methods as the java.io.PrintWriter
class. However, JspWriter has some additional methods designed to deal with
buffering. Unlike the PrintWriter object, JspWriter throws IOExceptions.
|
session
|
This
is the HttpSession object associated with the request.
The session object is an instance
of javax.servlet.http.HttpSession and behaves exactly the same way that
session objects behave under Java Servlets.
The
session object is used to track client session between client requests
|
application
|
This
is the ServletContext object associated with application context.
|
config
|
This
is the ServletConfig object associated with the page.
|
pageContext
|
This
encapsulates use of server-specific features like higher performance JspWriters.
|
page
|
This
is simply a synonym for this, and is used to call the methods defined
by the translated servlet class.
|
Exception
|
The Exception
object allows the exception data to be accessed by designated JSP.
|
JSP - Directives
JSP directives provide directions and instructions to the container, telling it how to handle certain aspects of JSP processing.A JSP directive affects the overall structure of the servlet class. It usually has the following form:
<%@ directive attribute="value" %>
|
The blanks between the @ symbol and the directive name, and between the last attribute and the closing %>, are optional.
There are three types of directive tag:
Directive
|
Description
|
<%@
page ... %>
|
Defines
page-dependent attributes, such as scripting language, error page, and
buffering requirements.
|
<%@
include ... %>
|
Includes
a file during the translation phase.
|
<%@
taglib ... %>
|
Declares
a tag library, containing custom actions, used in the page
|
The page Directive:
The page directive is used to provide instructions to the container that pertain to the current JSP page. By convention, page directives are coded at the top of the JSP page.syntax of page directive:
<%@ page attribute="value" %>
|
Attribute
|
Purpose
|
buffer
|
Specifies
a buffering model for the output stream.
|
autoFlush
|
Controls
the behavior of the servlet output buffer.
|
contentType
|
Defines
the character encoding scheme.
|
errorPage
|
Defines
the URL of another JSP that reports on Java unchecked runtime exceptions.
|
isErrorPage
|
Indicates
if this JSP page is a URL specified by another JSP page's errorPage
attribute.
|
extends
|
Specifies
a superclass that the generated servlet must extend
|
import
|
Specifies
a list of packages or classes for use in the JSP as the Java import statement
does for Java classes.
|
info
|
Defines
a string that can be accessed with the servlet's getServletInfo() method.
|
isThreadSafe
|
Defines
the threading model for the generated servlet.
|
language
|
Defines
the programming language used in the JSP page.
|
session
|
Specifies
whether or not the JSP page participates in HTTP sessions
|
isELIgnored
|
Specifies
whether or not EL expression within the JSP page will be ignored.
|
isScriptingEnabled
|
Determines
if scripting elements are allowed for use.
|
The include Directive:
The include directive is used to includes a file during the translation phase. This directive tells the container to merge the content of other external files with the current JSP during the translation phase.<%@ include file="relative url" >
|
The taglib Directive:
The JavaServer Pages API allows you to define custom JSP tags that look like HTML or XML tags and a tag library is a set of user-defined tags that implement custom behavior.The taglib directive declares that your JSP page uses a set of custom tags, identifies the location of the library, and provides a means for identifying the custom tags in your JSP page.
The taglib directive follows the following syntax:
<%@ taglib uri="uri" prefix="prefixOfTag" >
|
You can write XML equivalent of the above syntax as follows:
<jsp:directive.taglib uri="uri" prefix="prefixOfTag" />
|
JSP - Actions
JSP actions use constructs in XML syntax to control the behavior of the servlet engine. You can dynamically insert a file, reuse JavaBeans components, forward the user to another page, or generate HTML for the Java plugin.There is only one syntax for the Action element, as it conforms to the XML standard:
<jsp:action_name attribute="value" />
|
Syntax
|
Purpose
|
jsp:include
|
Includes
a file at the time the page is requested
|
jsp:include
|
Includes
a file at the time the page is requested
|
jsp:useBean
|
Finds
or instantiates a JavaBean
|
jsp:setProperty
|
Sets
the property of a JavaBean
|
jsp:getProperty
|
Inserts
the property of a JavaBean into the output
|
jsp:forward
|
Forwards
the requester to a new page
|
jsp:plugin
|
Generates
browser-specific code that makes an OBJECT or EMBED tag for the Java plugin
|
jsp:element
|
Defines
XML elements dynamically.
|
jsp:attribute
|
Defines
dynamically defined XML element's attribute.
|
jsp:body
|
Defines
dynamically defined XML element's body.
|
jsp:text
|
Use to
write template text in JSP pages and documents.
|
Common Attributes:
There are two attributes that are common to all Action elements: the id attribute and the scope attribute.
·
Id attribute: The id attribute uniquely
identifies the Action element, and allows the action to be referenced inside
the JSP page. If the Action creates an instance of an object the id value can
be used to reference it through the implicit object PageContext
·
Scope attribute: This attribute
identifies the lifecycle of the Action element. The id attribute and the scope
attribute are directly related, as the scope attribute determines the lifespan
of the object associated with the id. The scope attribute has four possible
values: (a) page, (b)request, (c)session, and (d) application.
The <jsp:include> Action
This action lets you insert files into the page being generated. The syntax looks like this:<jsp:include page="relative URL" flush="true" />
|
Following is the list of attributes associated with include action:
Attribute
|
Description
|
page
|
The
relative URL of the page to be included.
|
flush
|
The
boolean attribute determines whether the included resource has its buffer
flushed before it is included.
|
No comments:
Post a Comment