Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Jakarta Server Pages
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
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!
== 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.
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Jakarta Server Pages
(section)
Add topic