Jakarta Server Pages
Jakarta Server Pages (JSP; formerly JavaServer Pages)[1] is a collection of technologies that helps software developers create dynamically generated web pages based on HTML, XML, SOAP, or other document types. Released in 1999 by Sun Microsystems,[2] JSP is similar to PHP and ASP, but uses the Java programming language.
Filename extension
To deploy and run Jakarta Server Pages, a compatible web server with a servlet container, such as Apache Tomcat or Jetty, is required.
Syntax[edit]
Directives, scriptlets, and expressions, declaration[edit]
JSPs use several delimiters for scripting functions.
The most basic is <% ... %>
, which encloses a JSP scriptlet. A scriptlet is a fragment of Java code[6] 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 <%@ ... %>
.[6][7]
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.
Compiler[edit]
A JavaServer Pages compiler is a program that parses JSPs and transforms them into executable Java Servlets. A program of this type is usually embedded into the application server 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.[18]
Some JSP containers support configuring how often the container checks JSP file timestamps to see whether the page has changed. Typically, this timestamp would be set to a short interval (perhaps seconds) during software development, and a longer interval (perhaps minutes, or even never) for a deployed Web application.[19]
Criticism[edit]
According to Joel Murach and Michael Urban, authors of the book "Murach's Java Servlets and JSP", embedding Java code in JSP is generally bad practice.[20] A better approach would be to migrate the back-end logic embedded in the JSP to the Java code in the Servlet
.[20] In this scenario, the Servlet
is responsible for processing, and the JSP is responsible for displaying the HTML,[20] maintaining a clear separation of concerns.
In 2000, Jason Hunter, author of "Java Servlet Programming" described a number of "problems" with JavaServer Pages.[21] 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. Later, he added a note to his site saying that JSP had improved since 2000, but also cited its competitors, Apache Velocity and Tea (template language).[21] Today, several alternatives and a number of JSP-oriented pages in larger web apps are considered to be technical debt.