JSP page elements consist of: static content, directives, expressions, small scripts, declarations, annotations.
JSP directives include:
Page directive: Usually at the top of a JSP page, the same page can have multiple page directives.
Include directive: Embed an external file into the current JSP file and parse the JSP statements in the page.
Taglib directives: Use the tag library to define new custom tags and enable custom behaviors in JSP pages.
Page instruction Syntax:
<% @page Property = "Property Value" Property 2 = "Property value 1, property value 2" ... Property N= "Property value N"%>
Property |
Describe |
Default value |
Language |
Develop the scripting language used by JSP pages |
Java |
Import |
Use this property to refer to the class file used in the scripting language |
No |
ContentType |
Used to specify the encoding used by the JSP page |
Text/html Iso-8859-1 |
ContentType is generally set to contenttype= "text/html; Charset=utf-8 "
JSP Notes
Comments on the JSP page. There are several ways:
Comments for HTML: (client visible)
<!--HTML comments--
Comments for JSP: (Client not visible)
<%--HTML Comment--%>
JSP script comments: (can be embedded in the comments of the JSP)
Single-line Comment
/* Multiline Comment */
JSP Script
The Java code that executes in the JSP page.
Grammar:
<% Java Code%>
Example:
<%
Out.println ("moonlit");
%>
JSP Statement
Define variables or methods in the JSP page.
Grammar:
<%! Java Code%>
Example:
<%!
String s = "moonlit"; A string variable was declared
int add (int x, int y) {//declares a function that returns an integral type, which implements the summation of two functions.
return x + y;
}
%>
After declaring the variables and functions, we can use the variables and functions in the next step.
JSP An expression
An expression that executes in a JSP page.
Grammar:
<%= expression%>//Note: The expression does not end with a semicolon
Example: (e.g. display JSP life defined s in JSP page)
<p> Name: <%=s%></p>
Basic elements of a JSP page