You are on page 1of 4

JSP Overview

JavaServer Pages is a technology for developing web pages that include dynamic content.
Unlike a plain HTML page, which contains static content that always remains the same, a
JSP page can change its content based on any number of variable items, including the
identity of the user, the user's browser type, information provided by the user, and
selections made by the user.

A JSP page contains standard markup language elements, such as HTML tags, just like a
regular web page. However, a JSP page also contains special JSP elements that allow the
server to insert dynamic content in the page. JSP elements can be used for a variety of
purposes, such as retrieving information from a database or registering user preferences.
When a user asks for a JSP page, the server executes the JSP elements, merges the results
with the static parts of the page, and sends the dynamically composed page back to the
browser.

JSP Life Cycle

1. Page Translation
2. Servlet Creation
3. Instantiation
4. jspInit()
5. _jspService()
6. jspDestroy()
While a JSP does follow the Servlet life cycle, the methods have different names.
Initialization corresponds to the jspInit() method, service corresponds to the
_jspService() method, and destruction corresponds to the jspDestroy() method. The
three phases are all used the same as a Servlet and allow a JSP to load resources, provide
service to multiple client requests, and destroy loaded resources when the JSP is taken
out of service.

JSP Elements
There are three types of JSP elements you can use: directive, action, and scripting.

Directive elements
The directive elements specify information about the page itself that remains the same
between requests -- for example, if session tracking is required or not, buffering
requirements, and the name of a page that should be used to report errors, if any.

Directive elements
Element Description
<%@ page ... %> Defines page-dependent attributes, such as session tracking, error
page, and buffering requirements
<%@ include ... %> Includes a file during the translation phase
<%@ taglib ... %> Declares a tag library, containing custom actions, that is used in
the page

Standard action elements


Action elements typically perform some action based on information that is required at
the exact time the JSP page is requested by a browser. An action can, for instance, access
parameters sent with the request to do a database lookup. It can also dynamically
generate HTML, such as a table filled with information retrieved from an external
system.

The JSP specification defines a few standard action elements.


Standard action elements
Action element Description
<jsp:useBean> Makes a JavaBeans component available in
a page
<jsp:getProperty> Gets a property value from a JavaBeans
component and adds it to the response
<jsp:setProperty> Sets a JavaBeans component property value
<jsp:include> Includes the response from a servlet or JSP
page during the request processing phase
<jsp:forward> Forwards the processing of a request to
servlet or JSP page
<jsp:param> Adds a parameter value to a request handed
off to another servlet or JSP page using
<jsp:include> or <jsp:forward>
<jsp:plugin> Generates HTML that contains the
appropriate browser-dependent elements
(OBJECT or EMBED) needed to execute
an applet with the Java Plug-in software

Custom action elements and the JSP Standard Tag Library

In addition to the standard actions, the JSP specification includes a Java API a
programmer can use to develop custom actions to extend the JSP language. The JSP
Standard Tag Library (JSTL) is such an extension, with the special status of being
defined by a formal specification from Sun and typically bundled with the JSP container.
JSTL contains action elements for processes needed in most JSP applications, such as
conditional processing, database access, internationalization, and more.

If JSTL isn't enough, programmers on your team (or a third party) can use the extension
API to develop additional custom actions, maybe to access application-specific resources
or simplify application-specific processing.

Scripting elements
Scripting elements allow you to add small pieces of code (typically Java code) in a JSP
page, such as an if statement to generate different HTML depending on a certain
condition. Like actions, they are also executed when the page is requested. You should
use scripting elements with extreme care: if you embed too much code in your JSP pages,
you will end up with the same kind of maintenance problems as with servlets embedding
HTML.

Scripting elements
Element Description
<% ... %> Scriptlet, used to embed scripting code.
<%= ... %> Expression, used to embed scripting code expressions when the result shall
be added to the response. Also used as request-time action attribute values.
<%! ... %> Declaration, used to declare instance variables and methods in the JSP page
implementation class.

Implicit Objects

You might also like