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
(section)
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!
=== 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>
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)