Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Jakarta Server Pages
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{Short description|Jakarta EE dynamic web page technology}} {{Infobox file format | name = JSP | icon = | mime = application/jsp{{Citation needed|date=February 2021|reason=This is not registered in IANA.}} | extension = .jsp, .jspx, .jspf | standard = [http://jcp.org/en/jsr/detail?id=245 JSR 245] | developer = [[Eclipse Foundation]] | type = [[Dynamic web page]] | released = {{start date and age|1999}} | latest release version = [https://jakarta.ee/specifications/pages/4.0/jakarta-server-pages-spec-4.0.pdf 4.0] | latest_release_date = {{Start date and age|2024|04|09}} | open = Yes | url = {{Official URL}} }} '''Jakarta Server Pages''' ('''JSP'''; formerly '''JavaServer Pages''')<ref>{{cite web |url=https://www.infoworld.com/article/3336161/what-is-jsp-introduction-to-javaserver-pages.html |title=What is JSP? Introduction to Jakarta Server Pages |author=Matthew Tyson |date=September 9, 2022 |access-date=May 30, 2024 |website=InfoWorld}}</ref> is a collection of technologies that helps [[software developer]]s create [[dynamic web page|dynamically generated web pages]] based on [[HTML]], [[XML]], [[SOAP]], or other document types. Released in 1999 by [[Sun Microsystems]],<ref>{{Cite web|url=http://www.xent.com/FoRK-archive/apr99/0822.html|title=FoRK Archive: Sun JSP 1.0 *not* available|website=www.xent.com}}</ref> JSP is similar to [[PHP]] and [[Active Server Pages|ASP]], but uses the [[Java (programming language)|Java programming language]]. To deploy and run Jakarta Server Pages, a compatible web server with a [[servlet container]], such as [[Apache Tomcat]] or [[Jetty (web server)|Jetty]], is required. ==Overview== [[File:JSP Model 2.svg|thumb|The JSP Model 2 architecture.]] Architecturally, JSP may be viewed as a high-level [[Abstraction (computer science)|abstraction]] of [[Jakarta Servlet]]s. 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.<ref>[http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/JSPIntro4.html The Life Cycle of a JSP Page (Sun documentation)]</ref> Jakarta Server Pages can be used independently or as the view component of a server-side [[model–view–controller]] design, normally with [[JavaBeans]] as the model and Java servlets (or a framework such as [[Apache Struts]]) as the controller. This is a type of [[JSP model 2 architecture|Model 2]] architecture.<ref>{{cite web |last1=Seshadri |first1=Govind |date=1999-12-29 |df=mdy |url=https://www.infoworld.com/article/2076557/understanding-javaserver-pages-model-2-architecture.html |title=Understanding JavaServer Pages Model 2 architecture |work=[[JavaWorld]] |access-date=2020-07-17}}</ref> 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]]. Like any other .jar or Java program, code must be executed within a [[Java virtual machine]] (JVM) that interacts with the server's host [[operating 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.<ref>{{Cite web|url=https://coderanch.com/t/286297/JSP/java/OutputStream-already-obtained|title=OutputStream already obtained (JSP forum at Coderanch)|website=coderanch.com}}</ref> The [[Web container]] creates JSP implicit objects like request, response, session, application, config, page, pageContext, out and exception. JSP Engine creates these objects during translation phase. == Syntax == === Directives, scriptlets, and expressions, declaration === JSPs use several delimiters for [[server-side scripting|scripting]] functions. The most basic is <code> <% ... %></code>, which encloses a JSP ''scriptlet.'' A scriptlet is a fragment of Java code{{sfn | Murach | Urban | 2014 | loc=§2 Essential servlet and JSP skills - How to use JSP tags | pp=180-182}} that runs when the user requests the page. Other common delimiters include <code> <%= ... %></code> for ''expressions,'' where the scriptlet and delimiters are replaced with the result of evaluating the expression, and ''directives'', denoted with <code><%@ ... %></code>.{{sfn | Murach | Urban | 2014 | loc=§2 Essential servlet and JSP skills - How to use JSP tags | pp=180-182}}<ref name="syntax">[http://www.oracle.com/technetwork/java/syntaxref12-149806.pdf JSP 1.2 Syntax Reference]</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. ==== Example ==== The following would be a valid [[for loop]] in a JSP page: <syntaxhighlight lang="jsp"><p>Counting to three:</p> <% for (int i=1; i<4; i++) { %> <p>This number is <%= i %>.</p> <% } %> <p>OK.</p> </syntaxhighlight> The output displayed in the user's web browser would be: <pre> Counting to three: This number is 1. This number is 2. This number is 3. OK. </pre> === Standard JSP Tags === ==== The useBean Tag ==== The JSP <code>useBean</code> tag enables the developer to access and create a Javabean.{{sfn | Murach | Urban | 2014 | loc=§2 Essential servlet and JSP skills - Summary | pp=198}} Although using the <code>useBean</code> tag looks similar to an HTML tag, all JSP tags for JavaBeans use XML syntax. Therefore the code containing the <code>useBean</code> tag is case-sensitive.{{sfn | Murach | Urban | 2014 | loc=§2 Essential servlet and JSP skills - How to code the useBean tag | pp=186-187}} The <code>useBean</code> tag contains several attributes. The <code>id</code> attribute declares the name that is used for gaining access to the bean. The <code>class</code> attribute declares the package and class for the bean. The <code>scope</code> 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: {{sfn | Murach | Urban | 2014 | loc=§2 Essential servlet and JSP skills - How to code the useBean tag | pp=186-187}} * The {{code|page}} scope implies that the bean is located in the implicitly defined {{Javadoc:EE|javax/servlet/jsp|PageContext}} object, and is only available for the current page. By default, all beans have a scope of {{code|page}}. * The {{code|request}} scope implies that the bean can be found in the {{Javadoc:EE|javax/servlet/http|HttpServletRequest}} object. This bean can be accessed by all other JSPs and servlets that have access to the current request object. * The {{code|session}} scope implies that the bean can be found in the {{Javadoc:EE|javax/servlet/http|HttpSession}} object. This bean can be accessed by all other JSPs and servlets that have access to the specified {{code|HttpSession}} object. * The {{code|application}} scope implies that the bean can be found in the {{Javadoc:EE|javax/servlet|ServletContext}} object. This bean can be accessed by all other JSPs and servlets that have access to the specified {{code|ServletContext}} object. ==== The getProperty and setProperty Tags ==== After a bean has been created using the <code>useBean</code> tag, the <code>getProperty</code> and <code>setProperty</code> tags can be used for getting and setting the properties of the bean. The JSP <code>getProperty</code> is used to get the property of created bean. The JSP <code>setProperty</code> tag is used to set the properties for a bean. For the <code>getProperty</code> and <code>setProperty</code> tags, the name attribute is used to specify the bean's name. So the name attribute must match the id attribute provided by the <code>useBean</code> tag.{{sfn | Murach | Urban | 2014 | loc=§2 Essential servlet and JSP skills - How to code the getProperty and setProperty tags | pp=188}} === Expression Language === {{main article|Jakarta 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]], which is also used in [[JavaServer Faces]].<ref>[http://java.sun.com/products/jsp/reference/techart/unifiedEL.html The Unified Expression Language (Sun Developer Network)]</ref> 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.{{sfn | Murach | Urban | 2014 | loc=§2 Essential servlet and JSP skills - How to use EL to get attributes and JavaBean properties | pp=176-177}} ==== Example ==== The following is an example of EL [[syntax (programming languages)|syntax]]: The value of <code>variable</code> in the object <code>javabean</code> is <code>${javabean.variable}</code>. === Additional tags === The JSP syntax add additional tags, called JSP actions, to invoke built-in functionality.<ref name="syntax"/> Additionally, the technology allows for the creation of custom JSP ''tag libraries'' that act as extensions to the standard JSP syntax.<ref>[http://java.sun.com/products/jsp/tutorial/TagLibraries3.html#63159 Tag Libraries Tutorial - What is a Tag Library? (Sun)] {{webarchive |url=https://web.archive.org/web/20120419215245/http://java.sun.com/products/jsp/tutorial/TagLibraries3.html#63159 |date=April 19, 2012 }}</ref> One such library is the [[JavaServer Pages Standard Tag Library|JSTL]].<ref name="docs.oracle.com">{{Cite web|url=https://docs.oracle.com/javaee/5/tutorial/doc/bnakc.html|title=JavaServer Pages Standard Tag Library - The Java EE 5 Tutorial|website=docs.oracle.com}}</ref> ==== Jakarta Standard Tag Library ==== {{main article|Jakarta Standard Tag Library}} Jakarta Standard Tag Library (JSTL) supports common tasks that must be performed in JSPs.{{sfn | Murach | Urban | 2014 | loc=§2 Essential servlet and JSP skills -An Introduction to JSTL | pp=270-273}} Examples includes iteration and conditionals (the equivalent of "for" and "if" statements in Java).<ref name="docs.oracle.com"/> 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.{{sfn | Murach | Urban | 2014 | loc=§2 Essential servlet and JSP skills - How to enable the core JSTL library | pp=178}} === XML-compliant JSP === JSP pages may also be written in fully valid XML syntax.<ref>{{cite web|url=https://docs.oracle.com/cd/E19316-01/819-3669/6n5sg7b3b/index.html|title=The Java EE 5 Tutorial, Chapter 6 JavaServer Pages Documents|publisher=oracle.com|access-date=2022-07-27}}</ref> Such JSP files commonly use the alternative <code>.jspx</code> file extension, which usually causes the application server to validate the XML syntax. Since the usual JSP syntax <code><% ... %></code> is not valid in XML, a developer must use alternative tags provided by JSP. For example, the common <code><%@ page .. %></code> directive may instead be written as a <code><jsp:directive.page .. /></code> tag, and tag libraries are imported using [[XML namespace]]s, instead of the usual <code><%@ taglib .. %></code> tag. == Compiler == 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.<ref>{{Cite web|url=https://www.ibm.com/docs/en/products|title=IBM Docs|website=www.ibm.com}}</ref> Some JSP containers support configuring how often the container checks JSP [[Computer file|file]] [[timestamp]]s 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]].<ref>{{Cite web|url=https://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.help.eas_5.0.easperf/html/easperf/easperf111.htm|title=SyBooks Online|website=infocenter.sybase.com}}</ref> == Criticism == 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.{{sfn | Murach | Urban | 2014 | loc=§1 Get started right - The JSP for the second page | pp=46-47}} A better approach would be to migrate the back-end logic embedded in the JSP to the Java code in the {{code|Servlet}}.{{sfn | Murach | Urban | 2014 | loc=§1 Get started right - The JSP for the second page | pp=46-47}} In this scenario, the {{code|Servlet}} is responsible for processing, and the JSP is responsible for displaying the HTML,{{sfn | Murach | Urban | 2014 | loc=§1 Get started right - The JSP for the second page | pp=46-47}} maintaining a clear [[separation of concerns]]. In 2000, Jason Hunter, author of "Java Servlet Programming" described a number of "problems" with JavaServer Pages.<ref name="problems">[http://servlets.com/soapbox/problems-jsp.html The Problems with JSP] (January 25, 2000)</ref> 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).<ref name="problems" /> Today, several alternatives and a number of JSP-oriented pages in larger web apps are considered to be technical debt. == See also == {{Portal|Computer programming}} === Servlet containers === * [[Apache Tomcat]] * [[Apache TomEE]] * [[Jetty (web server)]] * [[GlassFish]] * [[Oracle iPlanet Web Server]] * [[WebSphere Application Server]] === Java-based template alternatives === * [[Adobe ColdFusion]] * [[Lucee]] * [[FreeMarker]] * [[JHTML]] * [[Thymeleaf]] ==References== {{Reflist}} ===Works cited=== * {{cite book |last1=Murach |first1=Joel |last2=Urban |first2=Michael |title=Murach's Java Servlets and JSP |date=2014 |publisher=Mike Murach & Associates |isbn=978-1-890774-78-3 |url=https://www.murach.com/shop-books/web-development-books/murach-s-java-servlets-and-jsp-3rd-edition-detail |language=en}} == Further reading == * {{cite book |last= Bergsten |first= Hans |title= JavaServer Pages |edition= 3rd |year= 2003 |publisher= [[O'Reilly Media]] |isbn= 978-0-596-00563-4 |url= https://archive.org/details/javaserverpages000berg |ref=none}} * {{cite book |author1 = Brown, Simon |author2=Dalton, Sam |author3= Jepp, Daniel |author4= Johnson, Dave |author5= Li, Sing |author6= Raible, Matt |title= Pro JSP 2 | publisher= [[Apress]] |isbn= 1-59059-513-0 |ref=none}} * {{cite book | author = Hanna, Phil | title = JSP 2.0 - The Complete Reference |publisher = [[McGraw-Hill|McGraw-Hill Osborne Media]] | year = 2003| isbn = 978-0-07-222437-5 |ref=none}} * {{cite book |first1 =Kathy |last1 =Sierra |author2 =Bates, Bert |author3 =Basham, Bryan |title =Head First Servlets & JSP |publisher =[[O'Reilly Media]] |isbn =978-0-596-00540-5 |url =https://archive.org/details/headfirstservlet00bash |ref=none}} ==External links== {{Wikibooks|J2EE Programming/JavaServer Pages}} {{Commons category|Jakarta Server Pages}} {{Wikibooks|J2EE Programming/JavaServer Pages}} <!-- There is no JSP section in Java EE 6 Official Tutorial, it seems that they pay their complete attention to JSF in Java EE 6 --> * {{Official website}} * [https://web.archive.org/web/20120625231747/http://java.sun.com:80/products/jsp/syntax/2.0/syntaxref20.html JSP v2.0 Syntax Reference] * [https://web.archive.org/web/20041207155029/http://java.sun.com/products/jsp/syntax/2.0/card20.pdf JavaServer Pages v2.0 Syntax Card] ([http://www.oracle.com/technetwork/java/card12-149784.pdf v1.2]) <!-- ^ contains brief overview of jsp syntax, comparing <% ... %> (at the left) variant and "pure xml" variant (at the right) in a single table, in flavour of new trends, pure xml syntax and [[Unified Expression Language|EL expressions]] seems to be more preferable then <% ... %> syntax. also bottom table contains brief list of all context variables that are available in [[Unified Expression Language|EL expressions]] ; this pdf is availabe as a link from "<%= expression %>"-section of "JSP Syntax Reference" --> * [https://github.com/eclipse-ee4j/jsp-api/blob/master/spec/src/main/asciidoc/ServerPages.adoc Jakarta Server Pages Specification, Latest] * [http://download.oracle.com/javaee/5/tutorial/doc/bnagx.html Official tutorial: The Java EE 5 Tutorial, Chapter 5, JavaServer Pages Technology] * [https://web.archive.org/web/20200815055946/https://community.oracle.com/blogs/driscoll/2005/12/10/servlet-history Servlet History] {{Jakarta EE}} {{Authority control}} [[Category:Java enterprise platform]] [[Category:Java specification requests]] [[Category:Template engines]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Authority control
(
edit
)
Template:Cite book
(
edit
)
Template:Cite web
(
edit
)
Template:Code
(
edit
)
Template:Commons category
(
edit
)
Template:Infobox file format
(
edit
)
Template:Jakarta EE
(
edit
)
Template:Javadoc:EE
(
edit
)
Template:Main article
(
edit
)
Template:Official website
(
edit
)
Template:Portal
(
edit
)
Template:Reflist
(
edit
)
Template:Sfn
(
edit
)
Template:Short description
(
edit
)
Template:Sister project
(
edit
)
Template:Webarchive
(
edit
)
Template:Wikibooks
(
edit
)