You are on page 1of 5

12/12/2009 JavaServer Pages - Wikipedia, the free …

JavaServer Pages
From Wikipedia, the free encyclopedia

JavaServer Pages (JSP) is a server side Java technology that allows software developers to create dynamically
generated web pages, with HTML, XML, or other document types, in response to a Web client request to a Java
Web Application container (server). Architecturally, JSP may be viewed as a high-level abstraction of Java servlets.
JSP pages are loaded in the server and operated from a structured special installed Java server packet called a
J2EE Web Application often packaged as a .war or .ear file archive.

The technology allows Java code and certain pre-defined actions to be embedded into static page content and
compiled on the server at runtime of each page request. Both the Java Server (J2EE specification) and the page
scripts and/or extended customised programming added operate by (in the runtime context of being loaded
programs used) a special pre-installed base program called a virtual machine (VM) that integrates with the host
operating system, this type being the Java Virtual Machine (JVM).

JSP syntax has two basic forms, scriptlet and markup though fundamentally the page is either HTML or XML
markup. Scriptlet tagging (called Scriptlet Elements)(delimited) blocks of code with the markup are not effectively
markup and allows any java server relevant API (e.g. the servers running binaries themselves or database
connections API or java mail API) or more specialist JSP API language code to be embedded in an HTML or
XML page provided the correct declarations in the JSP file and file extension of the page are used. Scriptlet blocks
do not require to be completed in the block itself only the last line of the block itself being completed syntactically
correctly as a statement is required, it can be completed in a later block. This system of split inline coding sections is
called step over scripting because it can wrap around the static markup by stepping over it. At runtime (during a
client request) the code is compiled and evaluated, but compilation of the code generally only occurs when a change
to the code of the file occurs. The JSP syntax adds additional XML-like tags, called JSP actions, to be used to
invoke built-in functionality. Additionally, the technology allows for the creation of JSP tag libraries that act as
extensions to the standard HTML or XML tags. JVM operated Tag libraries provide a platform independent way
of extending the capabilities of a web server. Note that not all company makes of Java servers are J2EE
specification compliant.

Contents
1 History
2 Example
3 JSP 2.0
4 See also
5 Further reading
6 References
7 External links

History
en.wikipedia.org/wiki/JavaServer_Pages 1/5
12/12/2009 JavaServer Pages - Wikipedia, the free …
The JSP 1.0 specification has been released in 1999 as Java's answer to ASP and PHP.[1] Both servlets and JSPs
were originally developed at Sun Microsystems. Starting with version 1.2 of the JSP specification, JavaServer
Pages have been developed under the Java Community Process. JSR 53 defines both the JSP 1.2 and Servlet 2.3
specifications and JSR 152 defines the JSP 2.0 specification. As of May 2006 the JSP 2.1 specification has been
released under JSR 245 as part of Java EE 5.

Example
JSPs are compiled into servlets by a JSP compiler. The compiler either generates a servlet in Java code that is then
compiled by the Java compiler, or it may compile the servlet to byte code which is directly executable. JSPs can
also be interpreted on-the-fly, reducing the time taken to reload changes.

Regardless of whether the JSP compiler generates Java source code for a servlet or emits the byte code directly, it
is helpful to understand how the JSP compiler transforms the page into a Java servlet. For example, consider the
following input JSP and its resulting generated Java Servlet.

Input JSP

<%@ page errorPage="myerror.jsp" %>


<%@ page import="com.foo.bar" %>

<html>
<head>
<%! int serverInstanceVariable = 1;%>

<% int localStackBasedVariable = 1; %>


<table>
<tr><td><%= toStringOrBlank( "expanded inline data " + 1 ) %></td></tr>

Resulting servlet

package jsp_servlet;
import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

import com.foo.bar; // Imported as a result of <%@ page import="com.foo.bar" %>


import …

class _myservlet implements javax.servlet.Servlet, javax.servlet.jsp.HttpJspPage {


// Inserted as a
// result of <%! int serverInstanceVariable = 1;%>
int serverInstanceVariable = 1;

public void _jspService( javax.servlet.http.HttpServletRequest request,


javax.servlet.http.HttpServletResponse response )
throws javax.servlet.ServletException,
java.io.IOException

en.wikipedia.org/wiki/JavaServer_Pages 2/5
12/12/2009
java.io.IOException JavaServer Pages - Wikipedia, the free …
{
javax.servlet.ServletConfig config = …; // Get the servlet config
Object page = this;
PageContext pageContext = …; // Get the page context for this request
javax.servlet.jsp.JspWriter out = pageContext.getOut();
HttpSession session = request.getSession( true );
try {
out.print( "<html>\r\n" );
out.print( "<head>\r\n" );

// From <% int localStackBasedVariable = 1; %>
int localStackBasedVariable = 1;

out.print( "<table>\r\n" );
out.print( " <tr><td>" );
// From <%= toStringOrBlank( "expanded inline data " + 1 ) %>
out.print( toStringOrBlank( "expanded inline data " + 1 ) );
out.print( " </td></tr>\r\n" );

} catch ( Exception _exception ) {
// Clean up and redirect to error page in <%@ page errorPage="myerror.jsp" %>
}
}
}

JSP 2.0
The new version of the JSP specification includes new features meant to improve programmer productivity.
Namely:

An Expression Language (EL) which allows developers to create Velocity-style templates (among other
things).
A faster/easier way to display parameter values.
A clearer way to navigate nested beans.

In the Java EE 5 Platform, the focus has been ease of development by making use of Java language annotations that
were introduced by J2SE 5.0. JSP 2.1 supports this goal by defining annotations for dependency injection on JSP
tag handlers and context listeners.

Another key concern of the Java EE 5 specification has been the alignment of its webtier technologies, namely
JavaServer Pages (JSP), JavaServer Faces (JSF), and JavaServer Pages Standard Tag Library (JSTL).

The outcome of this alignment effort has been the Unified Expression Language (EL), which integrates the
expression languages defined by JSP 2.0 and JSF 1.1.

The main key additions to the Unified EL that came out of the alignment work have been: A pluggable API for
resolving variable references into Java objects and for resolving the properties applied to these Java objects,
support for deferred expressions, which may be evaluated by a tag handler when needed, unlike their regular
expression counterparts, which get evaluated immediately when a page is executed and rendered, and support for l-
value expression, which appear on the left hand side of an assignment operation. When used as an l-value, an EL
expression represents a reference to a data structure, for example: a JavaBeans property, that is assigned some user

en.wikipedia.org/wiki/JavaServer_Pages 3/5
12/12/2009 JavaServer Pages - Wikipedia, the free …
input. The new Unified EL is defined in its own specification document, which is delivered along with the JSP 2.1
specification.

Thanks to the Unified EL, JSTL tags, such as the JSTL iteration tags, can now be used with JSF components in an
intuitive way.

JSP 2.1 leverages the Servlet 2.5 specification for its web semantics.

See also
JSTL
Java EE
JavaServer Faces
Java Servlet
WAR (Sun file format)
EAR (file format)
JAR (file format)
Apache Tomcat
Sun Java System Web Server
ASP
CFM
JHTML

Further reading
Bergsten, Hans (2003). JavaServer Pages (3rd Edition ed.). O'Reilly Media. ISBN 978-0-596-00563-4.
Hanna, Phil. JSP 2.0 - The Complete Reference. McGraw-Hill Osborne Media. ISBN 978-0-072-22437-
5.
Kathy, Sierra; Bert Bates & Bryan Basham. Head First Servlets & JSP. O'Reilly Media. ISBN 978-0-
596-00540-5.

References
1. ^ JSP 1.0 specification (http://java.sun.com/products/jsp/tags/10/tags.html)

External links
Sun's JSP product description (http://java.sun.com/products/jsp/)
Learn JSP Tutorial - step by step (http://www.visualbuilder.com/jsp/tutorial/)
JSR 245 (http://www.jcp.org/en/jsr/detail?id=245) (JSP 2.1)
JSR 152 (http://www.jcp.org/en/jsr/detail?id=152) (JSP 2.0)
JSR 53 (http://www.jcp.org/en/jsr/detail?id=53) (JSP 1.2)
JSP 1.1 and 1.0 (http://java.sun.com/products/jsp/archive.html)
JSP tutorials with source code (http://www.courses.coreservlets.com/Course-Materials/csajsp2.html)
Quick JSP tutorial (http://www.jsptut.com/)

en.wikipedia.org/wiki/JavaServer_Pages 4/5
12/12/2009 JavaServer Pages - Wikipedia, the free …
JSP training courses (http://courses.coreservlets.com/servlet+jsp-training.html) Public courses co-sponsored
by Johns Hopkins University, or customized onsite versions
Beginners JSP tutorial (http://www.jsptube.com/)
Core Servlets and JavaServer Pages (http://pdf.coreservlets.com/)
Retrieved from "http://en.wikipedia.org/wiki/JavaServer_Pages"
Categories: Java enterprise platform | Java specification requests | Template engines

This page was last modified on 2 December 2009 at 22:45.


Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply.
See Terms of Use for details.
Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.
Contact us

en.wikipedia.org/wiki/JavaServer_Pages 5/5

You might also like