Jakarta Server Pages (JSP; formerly JavaServer Pages) is a collection of technologies that helps
software developer
Software development is the process of conceiving, specifying, designing, Computer programming, programming, software documentation, documenting, software testing, testing, and Software bugs, bug fixing involved in creating and maintaining applic ...
s create
dynamically generated web pages based on
HTML
The HyperText Markup Language or HTML is the standard markup language for documents designed to be displayed in a web browser. It can be assisted by technologies such as Cascading Style Sheets (CSS) and scripting languages such as JavaScri ...
,
XML
Extensible Markup Language (XML) is a markup language and file format for storing, transmitting, and reconstructing arbitrary data. It defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. T ...
,
SOAP
Soap is a salt of a fatty acid used in a variety of cleansing and lubricating products. In a domestic setting, soaps are surfactants usually used for washing, bathing, and other types of housekeeping. In industrial settings, soaps are use ...
, or other document types. Released in 1999 by
Sun Microsystems
Sun Microsystems, Inc. (Sun for short) was an American technology company that sold computers, computer components, software, and information technology services and created the Java programming language, the Solaris operating system, ZFS, the ...
, JSP is similar to
PHP
PHP is a general-purpose scripting language geared toward web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1993 and released in 1995. The PHP reference implementation is now produced by The PHP Group ...
and
ASP, but uses the
Java programming language
Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. It is a general-purpose programming language intended to let programmers ''write once, run anywh ...
.
To deploy and run Jakarta Server Pages, a compatible web server with a
servlet container
A web container (also known as a servlet container;
and compare "webcontainer"
) is the component of a web server that interacts with Jakarta Servlets. A web container is responsible for managing the lifecycle of servlets, mapping a URL to a pa ...
, such as
Apache Tomcat
Apache Tomcat (called "Tomcat" for short) is a free and open-source implementation of the Jakarta Servlet, Jakarta Expression Language, and WebSocket technologies. It provides a "pure Java" HTTP web server environment in which Java code can also ...
or
Jetty
A jetty is a structure that projects from land out into water. A jetty may serve as a breakwater, as a walkway, or both; or, in pairs, as a means of constricting a channel. The term derives from the French word ', "thrown", signifying somet ...
, is required.
Overview
![JSP Model 2](https://upload.wikimedia.org/wikipedia/commons/7/72/JSP_Model_2.svg)
Architecturally, JSP may be viewed as a high-level
abstraction
Abstraction in its main sense is a conceptual process wherein general rules and concepts are derived from the usage and classification of specific examples, literal ("real" or "concrete") signifiers, first principles, or other methods.
"An abstr ...
of
Java servlet
A Jakarta Servlet (formerly Java Servlet) is a Java software component that extends the capabilities of a server. Although servlets can respond to many types of requests, they most commonly implement web containers for hosting web application ...
s. JSPs are translated into
servlet
A Jakarta Servlet (formerly Java Servlet) is a Java software component that extends the capabilities of a server. Although servlets can respond to many types of requests, they most commonly implement web containers for hosting web application ...
s at runtime, therefore JSP is a Servlet; each JSP servlet is cached and re-used until the original JSP is modified.
Jakarta Server Pages can be used independently or as the view component of a server-side
model–view–controller
Model–view–controller (MVC) is a software architectural pattern commonly used for developing user interfaces that divide the related program logic into three interconnected elements. This is done to separate internal representations of infor ...
design, normally with
JavaBeans
In computing based on the Java Platform, JavaBeans is a technology developed by Sun Microsystems and released in 1996, as part of JDK 1.1.
The 'beans' of JavaBeans are classes that encapsulate one or more objects into a single standardized objec ...
as the model and Java servlets (or a framework such as
Apache Struts
Apache Struts 2 is an open-source web application framework for developing Java EE web applications. It uses and extends the Java Servlet API to encourage developers to adopt a model–view–controller (MVC) architecture. The WebWork framework ...
) as the controller. This is a type of
Model 2 architecture.
JSP allows Java code and certain predefined actions to be interleaved with static web markup content, such as HTML. The resulting page is compiled and executed on the server to deliver a document. The compiled pages, as well as any dependent Java libraries, contain Java bytecode rather than
machine code
In computer programming, machine code is any low-level programming language, consisting of machine language instructions, which are used to control a computer's central processing unit (CPU). Each instruction causes the CPU to perform a very ...
. Like any other .jar or Java program, code must be executed within a
Java virtual machine
A Java virtual machine (JVM) is a virtual machine that enables a computer to run Java programs as well as programs written in other languages that are also compiled to Java bytecode. The JVM is detailed by a specification that formally describes ...
(JVM) that interacts with the server's host
operating system
An operating system (OS) is system software that manages computer hardware, software resources, and provides common services for computer programs.
Time-sharing operating systems schedule tasks for efficient use of the system and may also in ...
to provide an abstract, platform-neutral environment.
JSPs are usually used to deliver HTML and XML documents, but through the use of OutputStream, they can deliver other types of data as well.
The
Web container
A web container (also known as a servlet container;
and compare "webcontainer"
) is the component of a web server that interacts with Jakarta Servlets. A web container is responsible for managing the lifecycle of servlets, mapping a URL to a par ...
creates JSP implicit objects like request, response, session, application, config, page, pageContext, out and exception. JSP Engine creates these objects during translation phase.
Syntax
JSP pages use several delimiters for
scripting functions. The most basic is
<% ... %>
, which encloses a JSP ''scriptlet.'' A scriptlet is a fragment of Java code that runs when the user requests the page. Other common delimiters include
<%= ... %>
for ''expressions,'' where the scriptlet and delimiters are replaced with the result of evaluating the expression, and ''directives'', denoted with
<%@ ... %>
.
[JSP 1.2 Syntax Reference](_blank)
/ref>
Java code is not required to be complete or self-contained within a single scriptlet block. It can straddle markup content, provided that the page as a whole is syntactically correct. For example, any Java ''if/for/while'' blocks opened in one scriptlet must be correctly closed in a later scriptlet for the page to successfully compile. This allows code to be intermingled and can result in poor programming practices.
Content that falls inside a split block of Java code (spanning multiple scriptlets) is subject to that code. Content inside an ''if'' block will only appear in the output when the ''if'' condition evaluates to true. Likewise, content inside a loop construct may appear multiple times in the output, depending upon how many times the loop body runs.
The following would be a valid for loop
In computer science a for-loop or for loop is a control flow statement for specifying iteration. Specifically, a for loop functions by running a section of code repeatedly until a certain condition has been satisfied.
For-loops have two part ...
in a JSP page:
Counting to three:
<% for (int i=1; i<4; i++) %>
OK.
The output displayed in the user's web browser would be:
Counting to three:
This number is 1.
This number is 2.
This number is 3.
OK.
Expression Language
Version 2.0 of the JSP specification added support for the Expression Language (EL), used to access data and functions in Java objects. In JSP 2.1, it was folded into the Unified Expression Language
The Jakarta Expression Language (EL; formerly Expression Language and Unified Expression Language) is a special purpose programming language mostly used in Jakarta EE web applications for embedding and evaluating expressions in web pages.
The s ...
, which is also used in JavaServer Faces
Jakarta Faces, formerly Jakarta Server Faces and JavaServer Faces (JSF) is a Java specification for building component-based user interfaces for web applications and was formalized as a standard through the Java Community Process being part of t ...
.
An example of EL syntax
In linguistics, syntax () is the study of how words and morphemes combine to form larger units such as phrases and sentences. Central concerns of syntax include word order, grammatical relations, hierarchical sentence structure ( constituency) ...
:
The value of variable
in the object javabean
is $
.
Additional tags
The JSP syntax add additional tags, called JSP actions, to invoke built-in functionality. Additionally, the technology allows for the creation of custom JSP ''tag libraries'' that act as extensions to the standard JSP syntax. One such library is the JSTL, with support for common tasks such as iteration and conditionals (the equivalent of "for" and "if" statements in Java).
XML-compliant JSP
JSP pages may also be written in fully valid XML syntax. Such JSP files commonly use the alternative .jspx
file extension, which usually causes the application server to validate the XML syntax.
Since the usual JSP syntax <% ... %>
is not valid in XML, a developer must use alternative tags provided by JSP. For example, the common <%@ page .. %>
directive may instead be written as a <jsp:directive.page .. />
tag, and tag libraries are imported using XML namespace
XML namespaces are used for providing uniquely named elements and attributes in an XML document. They are defined in a W3C recommendation. An XML instance may contain element or attribute names from more than one XML vocabulary. If each vocabulary ...
s, instead of the usual <%@ taglib .. %>
tag.
Compiler
A JavaServer Pages compiler is a program that parses JSPs, and transforms them into executable Java Servlets
A Jakarta Servlet (formerly Java Servlet) is a Java software component that extends the capabilities of a server. Although servlets can respond to many types of requests, they most commonly implement web containers for hosting web application ...
. A program of this type is usually embedded into the application server
An application server is a server that hosts applications or software that delivers a business application through a communication protocol.
An application server framework is a service layer model. It includes software components available to a ...
and run automatically the first time a JSP is accessed, but pages may also be precompiled for better performance, or compiled as a part of the build process to test for errors.
Some JSP containers support configuring how often the container checks JSP file
File or filing may refer to:
Mechanical tools and processes
* File (tool), a tool used to ''remove'' fine amounts of material from a workpiece
**Filing (metalworking), a material removal process in manufacturing
** Nail file, a tool used to gent ...
timestamp
A timestamp is a sequence of characters or encoded information identifying when a certain event occurred, usually giving date and time of day, sometimes accurate to a small fraction of a second. Timestamps do not have to be based on some absolut ...
s to see whether the page has changed. Typically, this timestamp would be set to a short interval (perhaps seconds) during software development
Software development is the process of conceiving, specifying, designing, programming, documenting, testing, and bug fixing involved in creating and maintaining applications, frameworks, or other software components. Software development invol ...
, and a longer interval (perhaps minutes, or even never) for a deployed Web application
A web application (or web app) is application software that is accessed using a web browser. Web applications are delivered on the World Wide Web to users with an active network connection.
History
In earlier computing models like client-serve ...
.
Criticism
In 2000, Jason Hunter, author of "Java Servlet Programming" described a number of "problems" with JavaServer Pages.[The Problems with JSP](_blank)
(January 25, 2000) Nevertheless, he wrote that while JSP may not be the "best solution for the Java Platform" it was the "Java solution that is most like the non-Java solution," by which he meant Microsoft's Active Server Pages
Active Server Pages (ASP) is Microsoft's first server-side scripting language and engine for dynamic web pages.
It was first released in December 1996, before being superseded in January 2002 by ASP.NET.
History
Initially released as an add ...
. Later, he added a note to his site saying that JSP had improved since 2000, but also cited its competitors, Apache Velocity
Apache Velocity first released in April 2001, is a Java-based template engine that provides a template language to reference objects defined in Java code. It aims to ensure clean separation between the presentation tier and business tiers in a W ...
and Tea (template language). Today there are several alternatives and a number of JSP-oriented pages in larger web apps are considered to be technical debt.
See also
Servlet containers
* Apache Tomcat
Apache Tomcat (called "Tomcat" for short) is a free and open-source implementation of the Jakarta Servlet, Jakarta Expression Language, and WebSocket technologies. It provides a "pure Java" HTTP web server environment in which Java code can also ...
* Apache TomEE
Apache TomEE (pronounced "Tommy") is the Java Enterprise Edition of Apache Tomcat (Tomcat + Jakarta EE = TomEE) that combines several Java enterprise projects including Apache OpenEJB, Apache OpenWebBeans, Apache OpenJPA, Apache MyFaces and other ...
* Jetty (web server)
Eclipse Jetty is a Java web server and Java Servlet container. While web servers are usually associated with serving documents to people, Jetty is now often used for machine to machine communications, usually within larger software frameworks. Je ...
* GlassFish
GlassFish is an open-source Jakarta EE platform application server project started by Sun Microsystems, then sponsored by Oracle Corporation, and now living at the Eclipse Foundation and supported by Payara, Oracle and Red Hat. The supported v ...
* Oracle iPlanet Web Server
Oracle iPlanet Web Server (OiWS) is a web server designed for medium and large business applications. Previous versions were marketed as Netscape Enterprise Server, iPlanet Web Server, Sun ONE Web Server, and Sun Java System Web Server.
Oracle ...
* WebSphere Application Server
WebSphere Application Server (WAS) is a software product that performs the role of a web application server. More specifically, it is a software framework and middleware that hosts Java-based web applications. It is the flagship product with ...
Java-based template alternatives
* Adobe ColdFusion
Adobe ColdFusion is a commercial rapid web-application development computing platform created by J. J. Allaire in 1995. (The programming language used with that platform is also commonly called ColdFusion, though is more accurately known as CF ...
* Lucee
Lucee is an open source implementation of a lightweight dynamically-typed scripting language for the Java virtual machine (JVM).
The language is used for rapid development of web applications that compile directly to Java bytecode, and is comp ...
* FreeMarker
Apache FreeMarker is a free Java-based template engine, originally focusing on dynamic web page generation with MVC software architecture. However, it is a general purpose template engine, with no dependency on servlets or HTTP or HTML, and i ...
* JHTML
JHTML stands for Java within HTML. This is a page authoring system developed at Art Technology Group (ATG). Files with a ".jhtml" filename extension contain standard HTML tags in addition to proprietary tags that reference Java objects running on ...
* Thymeleaf
Thymeleaf is a Java XML/XHTML/HTML5 template engine that can work both in web (servlet-based) and non-web environments. It is better suited for serving XHTML/HTML5 at the view layer of MVC-based web applications, but it can process any XML file e ...
References
Further reading
*
*
*
*
External links
*
JSP v2.0 Syntax Reference
JavaServer Pages v2.0 Syntax Card
v1.2
Jakarta Server Pages Specification, Latest
* ttps://web.archive.org/web/20200815055946/https://community.oracle.com/blogs/driscoll/2005/12/10/servlet-history Servlet History
{{Jakarta EE
Java enterprise platform
Java specification requests
Template engines