Architecturally, JSP may be viewed as a high-levelabstraction ofJakarta Servlets. JSPs are translated into servlets at runtime, therefore JSP is a Servlet; each JSP servlet is cached and re-used until the original JSP is modified.[3]
Jakarta Server Pages can be used independently or as the view component of a server-sidemodel–view–controller design, normally withJavaBeans as the model and Java servlets (or a framework such asApache Struts) as the controller. This is a type ofModel 2 architecture.[4]
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 thanmachine code. Like any other .jar or Java program, code must be executed within aJava virtual machine (JVM) that interacts with the server's hostoperating system 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.[5]
TheWeb container creates JSP implicit objects like request, response, session, application, config, page, pageContext, out and exception. JSP Engine creates these objects during translation phase.
JSPs use several delimiters forscripting functions. The most basic is <% ... %>, which encloses a JSPscriptlet. A scriptlet is a fragment of Java code[6] that runs when the user requests the page.
Other common delimiters include <%= ... %> forexpressions, where the scriptlet and delimiters are replaced with the result of evaluating the expression, anddirectives, 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 Javaif/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 anif block will only appear in the output when theif 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 JSPuseBean tag enables the developer to access and create a Javabean.[8]Although using theuseBean tag looks similar to an HTML tag, all JSP tags for JavaBeans use XML syntax. Therefore the code containing theuseBean tag is case-sensitive.[9]
TheuseBean tag contains several attributes. Theid attribute declares the name that is used for gaining access to the bean. Theclass attribute declares the package and class for the bean. Thescope declares the object responsible for storing the bean. The value for the scope defines the duration for which the bean is available for the rest of the java application to use. The scope can be one of the following four values:[9]
Thepage scope implies that the bean is located in the implicitly definedPageContext object, and is only available for the current page. By default, all beans have a scope ofpage.
Therequest scope implies that the bean can be found in theHttpServletRequest object. This bean can be accessed by all other JSPs and servlets that have access to the current request object.
Thesession scope implies that the bean can be found in theHttpSession object. This bean can be accessed by all other JSPs and servlets that have access to the specifiedHttpSession object.
Theapplication scope implies that the bean can be found in theServletContext object. This bean can be accessed by all other JSPs and servlets that have access to the specifiedServletContext object.
After a bean has been created using theuseBean tag, thegetProperty andsetProperty tags can be used for getting and setting the properties of the bean.The JSPgetProperty is used to get the property of created bean.The JSPsetProperty tag is used to set the properties for a bean.For thegetProperty andsetProperty tags, the name attribute is used to specify the bean's name. So the name attribute must match the id attribute provided by theuseBean tag.[10]
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 theUnified Expression Language, which is also used inJavaServer Faces.[11]
The JSP Expression Language uses a compact syntax which enables the developer to get attributes and JavaBean properties from a given request object. When using EL, a dollar sign ("$") must be added at the beginning of the code. The dollar symbol is followed by an opening brace ("{"), as well as a closing brace ("}"). The code is then written between the opening and closing braces.[12]
The JSP syntax add additional tags, called JSP actions, to invoke built-in functionality.[7] Additionally, the technology allows for the creation of custom JSPtag libraries that act as extensions to the standard JSP syntax.[13] One such library is theJSTL.[14]
Jakarta Standard Tag Library (JSTL) supports common tasks that must be performed in JSPs.[15] Examples includes iteration and conditionals (the equivalent of "for" and "if" statements in Java).[14]
Out of all the libraries in JSTL, the JSTL core library is most commonly used. A taglib directive must be used to specify the URI of the JSTL core library using a prefix. Although there are many different choices for the prefix, the "c" prefix is commonly used for this library.[16]
JSP pages may also be written in fully valid XML syntax.[17] 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 usingXML namespaces, instead of the usual<%@ taglib .. %> tag.
AJavaServer Pages compiler is a program that parses JSPs and transforms them into executableJava Servlets. A program of this type is usually embedded into theapplication 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 JSPfiletimestamps to see whether the page has changed. Typically, this timestamp would be set to a short interval (perhaps seconds) duringsoftware development, and a longer interval (perhaps minutes, or even never) for a deployedWeb application.[19]
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 theServlet.[20] In this scenario, theServlet is responsible for processing, and the JSP is responsible for displaying the HTML,[20] maintaining a clearseparation 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'sActive 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.