Jakarta Server Pages
| Company type | Software technology |
|---|---|
| Industry | Software development, Web development |
| Predecessor | JavaServer Pages |
| Founded | 1999 |
| Founder | Sun Microsystems |
| Fate | Renamed to Jakarta Server Pages |
Area served | Worldwide |
| Products | Technology for building dynamic web pages using Java |
| Owner | Eclipse Foundation (under Jakarta EE) |
| Parent | Jakarta EE |
| Website | jakarta |
Jakarta Server Pages (JSP; previously known as JavaServer Pages) is a technology designed for developers to build web pages that are generated dynamically, using formats such as HTML, XML, SOAP, or other document types. Introduced by Sun Microsystems in 1999, JSP shares similarities with PHP and ASP but is distinguished by its use of the Java programming language.
To run JSP applications, a web server equipped with a servlet container—examples include Apache Tomcat or Jetty—is necessary.
Overview
From an architectural perspective, JSP functions as a higher-level abstraction built on top of Jakarta Servlets. During execution, JSP files are converted into servlets; these servlet objects are then cached and reused until the corresponding JSP source undergoes changes.
JSP can operate on its own or serve as the view layer within a server-side model–view–controller (MVC) framework. This typically involves JavaBeans representing the model, with Java servlets or frameworks like Apache Struts managing the controller component—commonly referred to as Model 2 architecture.
The technology enables embedding Java code and predefined actions directly into static markup languages such as HTML. When processed on the server, these pages are compiled into Java bytecode that executes within a Java Virtual Machine (JVM), ensuring platform independence.
While JSP is predominantly utilized for generating HTML and XML content, it is also capable of producing other data formats through OutputStream.
During the translation process, the web container automatically provides several implicit JSP objects—such as request, response, session, application, config, page, pageContext, out, and exception—for developer use.
Directives, scriptlets, expressions, declaration
JSP employs specific delimiters to denote server-side scripting elements. The primary delimiter <% ... %> encloses a scriptlet, which is a segment of Java code executed when the page is requested.
Additional delimiters include <%= ... %> for expressions, where the enclosed code is evaluated and its result inserted into the output. Directives are marked with <%@ ... %>.
Java code within scriptlets can be distributed across multiple blocks provided that the overall page maintains syntactic correctness. For instance, control structures like if, for, or while opened in one scriptlet can be closed in another. This allows mixing of code and markup but may encourage suboptimal coding practices.
Content enclosed within such control structures is conditional; for example, content inside an if block renders only if the condition evaluates to true, and looped content repeats according to iteration count.
Example
An example of a valid for loop in JSP:
<syntaxhighlight lang="jsp">
Counting to three:
<% for (int i=1; i<4; i++) { %>
This number is <%= i %>.
<% } %>
OK.
</syntaxhighlight>
This produces the following output in a browser:
Counting to three: This number is 1. This number is 2. This number is 3. OK.
The useBean Tag
The JSP useBean tag enables developers to instantiate and interact with a JavaBean. Although it resembles an HTML tag visually, it follows case-sensitive XML syntax rules.
Important attributes include id, which specifies the bean’s identifier for access; class, indicating the fully qualified package and class name of the bean; and scope, which defines the bean's lifecycle and storage context. Available scope options are:
- : Stores the bean within the object; accessible only during the current page request (this is the default).
- : Places the bean in the object; accessible throughout all JSPs and servlets involved in handling this particular request.
- : Saves the bean in the object; available across JSPs and servlets sharing the same user session.
- : Keeps the bean in the object; accessible application-wide across all JSPs and servlets.
The getProperty and setProperty Tags
Following the creation of a bean via the useBean tag, its properties are manipulated using the getProperty and setProperty tags. The getProperty tag is used to obtain a property value from the bean, whereas the setProperty tag assigns a value to a bean property. Both tags must specify a name attribute that corresponds exactly to the bean's identifier defined in the useBean tag.
Expression Language
Expression Language (EL) was introduced with JSP 2.0 as a streamlined method for accessing data and invoking functions within Java objects. By JSP 2.1, EL was integrated into the Unified Expression Language, which is also utilized by JavaServer Faces. EL features a succinct syntax enclosed by "${" and "}", allowing straightforward retrieval of attributes and JavaBean properties.
Example
For instance, the EL expression ${javabean.variable} fetches the value of the property variable from the javabean instance.
Additional tags
JSP offers several built-in tags known as JSP actions and supports custom JSP tag libraries that enhance standard functionality. A notable example is the JSTL.
Jakarta Standard Tag Library
The Jakarta Standard Tag Library (JSTL) simplifies frequent JSP operations such as loops and conditional processing, mirroring Java's "for" and "if" constructs. The core JSTL library is widely adopted and commonly activated through a taglib directive using the prefix "c".
XML-compliant JSP
JSP pages can be authored using fully valid XML syntax, typically saved with the .jspx extension, which instructs the application server to enforce XML syntax validation.
Since traditional JSP syntax like <% ... %> is not valid XML, developers employ alternative JSP tags. For example, the directive <%@ page .. %> is replaced with <jsp:directive.page .. />, and tag libraries are included via XML namespace declarations rather than using the standard <%@ taglib .. %> approach.
Compiler
A JavaServer Pages compiler processes JSP files by parsing them and converting them into executable Java Servlets. This compiler is typically embedded within the application server and triggers automatically upon initial access. JSPs can also be precompiled to enhance runtime performance or compiled during build time to identify errors early in development.
Certain containers offer configurable options for how often they check JSP file timestamps for modifications—short intervals during active software development phases and extended or disabled checks in production environments.
Criticism
Joel Murach and Michael Urban, authors of "Murach's Java Servlets and JSP," criticize embedding Java code directly within JSP pages as poor design. They advocate relocating backend logic into servlets while reserving JSPs exclusively for HTML presentation to uphold clear separation of concerns.
In 2000, Jason Hunter pointed out various "issues" with JavaServer Pages but acknowledged it as Java's closest counterpart to Microsoft's Active Server Pages. Over time, he recognized enhancements in JSP and mentioned alternatives such as Apache Velocity and the Tea template language. Presently, numerous options exist, and heavy reliance on JSP in large-scale web applications is often considered a form of technical debt.
Servlet Containers
- Apache Tomcat
- Apache TomEE
- Jetty (web server)
- GlassFish
- Oracle iPlanet Web Server
- WebSphere Application Server