JSP TUTORIAL
Core JSP Elements Syntax
core JSP elements
comment
Declaration
Expression
Scriptlet
EL Expression
1. Comment
JSP Syntax
<%-- comment --%>
Example
<%@ page language="java" %>
<html>
<head><title>A Comment JSP program </title></head>
<body>
<h2> JSP Comment test program</h2>
<%-- comment, this statement wont be included into the reponse --%>
</body>
</html>
Explanation
The JSP container does not process anything within the <%-- and --%>
characters or within the <!-- and --> tags. A comment is not inserted into the response.
2. Declaration
To decalre variables or methods we can use the following tags
JSP Syntax
<%! declaration; [ declaration; ]+ ... %>
Example
<%! --- (1)
int i = 0; --- (2)
int a, b, c; --- (3)
%> --- (4)
<%! myDB a = new myDB("CONNECT"); %>
(1) -- Beginning of declaration tag
(2) -- Java variable declaration end with semicolon
(3) -- Java variable declaration end with semicolon
(4) -- End of declaration tag
Explanation
We need to define all variables which you plan to use in the program in these tags.
Any number of declare statements you can define within one set of tags ( <%! %>
3. Expression
Expression element is used to assign/include value dynamically(can be statically) into html
JSP Syntax
<%= expression %>
Example
Number of professionals visited this site :
<%= num.getVisitorCount() %>
Explanation
An expression element contains a scripting language expression that is evaluated, converted to a String, and inserted into the response where the expression appears in the JSP page. Dont use semicolon at the end of expression
4. Scriptlet
Contains a code fragment valid in the page scripting language.
JSP Syntax
<% code fragment %>
Example
<% ---- (1)
String name = null; ---- (2)
if (request.getParameter("name") == null) { ---- (3)
%> ---- (4)
<%@ include file="exception.html" %> ---- (5)
<% ---- (6)
} else { ---- (7)
myObj.setName(request.getParameter("name")); ---- (8)
if (myObj.getName().equalsIgnoreCase("ramesh")) ---- (9)
name = "hsbc"; ---- (10)
} ---- (11)
%> ---- (12)
(1) - Begining of the scriptlet
(2-3) - Java statements
(4) - End of the scriptlet
(5) - Jsp Directive which include the error html code into response
(6) - Beginning of the scriptlet
(7 - 11) - Java statements
(12) - End of the scriptlet
Explanation
A scriptlet can contain any number of language statements, variable or method declarations, or
expressions that are valid in the page scripting language
5. EL Expression
A primary feature of JSP technology version 2.0 is its support for an expression language (EL). An expression language makes it possible to easily access application data stored in JavaBeans components. For example, the JSP expression language allows a page author to access a bean using simple syntax such as ${name} for a simple variable or ${name.foo.bar} for a nested property.
The test attribute of the following conditional tag is supplied with an EL expression that compares the number of items in the session-scoped bean named cart with 0:
<c:if test="${sessionScope.cart.numberOfItems > 0}">
...
</c:if>
The JSP expression evaluator is responsible for handling EL expressions, which are enclosed by the ${ } characters and can include literals. Here's an example:
<c:if test="${bean1.a < 3}" >
...
</c:if>
Any value that does not begin with ${ is treated as a literal and is parsed to the expected type using the PropertyEditor for the type:
<c:if test="true" >
...
</c:if>
|
|
|
|
|