Expression Language
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
Expression
language
is, both
syntactically
and
semantically,
similar to
JavaScript
expressions.
- There is no typecasting
- Type conversions are usually done implicitly
- Double and single quotes are equivalent
object.property
object['property']
The
general
syntax : ${var|
object.var}
Example
<%@
page language="java"
contentType="text/html;
charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE
html PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta
http-equiv="Content-Type"
content="text/html;
charset=UTF-8">
<title>Expression
Language</title>
</head>
<body>
${
2+5 }
</body>
</html>
output
: 7
Implicit
Objects
- pageContext: The context for the JSP page. Provides access to various objects including:
- servletContext: The context for the JSP page’s servlet and any web components contained in the same application.
- session: The session object for the client.
- request: The request triggering the execution of the JSP page.
- response: The response returned by the JSP page.
In addition,
several implicit objects are available that allow easy access to the
following objects:
- param: Maps a request parameter name to a single value
- paramValues: Maps a request parameter name to an array of values
- header: Maps a request header name to a single value
- headerValues: Maps a request header name to an array of values
- cookie: Maps a cookie name to a single cookie
- initParam: Maps a context initialization parameter name to a single value
Finally, there
are objects that allow access to the various scoped variables
- pageScope: Maps page-scoped variable names to their values
- requestScope: Maps request-scoped variable names to their values
- sessionScope: Maps session-scoped variable names to their values
- applicationScope: Maps application-scoped variable names to their values
Example
:
(param
- Maps
a
request
parameter
name
to
a
single
value)
one.jsp
<%@
page language="java"
contentType="text/html;
charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE
html PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta
http-equiv="Content-Type"
content="text/html;
charset=UTF-8">
<title>login</title>
</head>
<body>
<form
action="display.jsp">
Enter
the name : <input
type="text"
name="email">
<input type="submit"
value="Enter">
</form>
</body>
</html>
display.jsp
<%@
page
language="java"
contentType="text/html;
charset=UTF-8"
pageEncoding="UTF-8"
isELIgnored="false"%>
<!DOCTYPE
html PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta
http-equiv="Content-Type"
content="text/html;
charset=UTF-8">
<title>Expression
Language</title>
</head>
<body>
Your
email id :${param['email'] }
<br><br>
Your
email id :${param.email}
</body>
</html>
(paramValues:
Maps a request parameter name to an array of values)
Login.jsp
<%@
page language="java"
contentType="text/html;
charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE
html PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta
http-equiv="Content-Type"
content="text/html;
charset=UTF-8">
<title>login</title>
</head>
<body>
<form
action="paramvalues.jsp">
Username:
<input
type="text"
name="uname"
/><br>
Password:
<input
type="password"
name="password"
/>
<input
type="submit"
value="Enter"
/>
</form>
</body>
</html>
paramValues.jsp
<%@
page language="java"
contentType="text/html;
charset=UTF-8"
pageEncoding="UTF-8"
isELIgnored="false"%>
<%@
taglib
uri="http://java.sun.com/jsp/jstl/core"
prefix="c"%>
<!DOCTYPE
html PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta
http-equiv="Content-Type"
content="text/html;
charset=UTF-8">
<title>Expression
Language</title>
</head>
<body>
<c:forEach
var="params"
items="${paramValues}">
<c:out
value="${params.key}"></c:out>
:
<c:forEach
var="value"
items="${params.value
}">
<c:out
value="${value}"></c:out>
<br>
</c:forEach>
</c:forEach>
</body>
</html>
Operators
In
addition to
the .
And []
operators
discussed
in Value
and Method Expressions,
the JSP
expression
language
provides
the
following
operators,
which can
be used
in value
expressions
only:
- Arithmetic: +,-(binary),*,/and div, % and mod, -(unary)
- Logical: and, &&, or, ||, not, !
- Relational: ==,eq,!=,ne,<,lt,>,gt,<=,ge,>=,le. Comparisons can be made against other values, or against boolean, string, integer, or floating point literals.
- Empty: The empty operator is a prefix operation that can be used to determine whether a value is null or empty.
- Conditional: A?B:C. Evaluate B or C, depending on the result of the evaluation of A.
The
precedence of operators highest to lowest, left to right is as
follows:
- [] .
- ()(used to change the precedence of operators)
- -(unary)not ! empty
- * / div % mod
- + -(binary)
- < > <= >= lt gt le ge
- == != eq ne
- && and
- || or
- ? :
Reserved Words
The
following words are reserved for the JSP expression language and
should not be used as identifiers.
And
|
or
|
not
|
eq
|
ne
|
lt
|
gt
|
le
|
ge
|
true
|
false
|
null
|
instanceof
|
empty
|
div
|
mod
|
Operators
& Reserved Words - Example:
<%@
page language="java"
contentType="text/html;
charset=UTF-8"
pageEncoding="UTF-8"
isELIgnored="false"%>
<%@
taglib
uri="http://java.sun.com/jsp/jstl/core"
prefix="c"%>
<!DOCTYPE
html PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta
http-equiv="Content-Type"
content="text/html;
charset=UTF-8">
<title>Expression
Language</title>
</head>
<body>
<pre>
The
sum of (2 + 3) : ${ 2 + 3 }<br>
The
result of (2 > 3) : ${ 2 gt 3
}<br>
The
result of (55 % 8) : ${ 55 mod 8}
</pre>
</body>
</html>
JSTL
(JavaServer Pages Tag Library)
JSTL
includes a wide variety of tags that fit into discrete functional
areas. To reflect this, as well as to give each area its own
namespace, JSTL is exposed as multiple tag libraries.
The
URIs for the libraries are as follows:
- Core:
<%@
taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"
%>
- XML:
<%@
taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml"
%>
- Internationalization:
<%@
taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"
%>
- SQL:
<%@
taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"
%>
- Functions:
<%@
taglib prefix="fn"
uri="http://java.sun.com/jsp/jstl/functions" %>
<%@
taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"
%>
Tag
Summary
|
|
Catches
any Throwable that occurs in its body and optionally exposes it.
|
|
Simple
conditional tag that establishes a context for mutually exclusive
conditional operations, marked by <when> and <otherwise>
|
|
Simple
conditional tag, which evalutes its body if the supplied condition
is true and optionally exposes a Boolean scripting variable
representing the evaluation of this condition
|
|
Retrieves
an absolute or relative URL and exposes its contents to either the
page, a String in 'var', or a Reader in 'varReader'.
|
|
The
basic iteration tag, accepting many different collection types and
supporting subsetting and other functionality
|
|
Iterates
over tokens, separated by the supplied delimeters
|
|
Like
<%= ... >, but for expressions.
|
|
Subtag
of <choose> that follows <when> tags and runs only if
all of the prior conditions evaluated to 'false'
|
|
Adds
a parameter to a containing 'import' tag's URL.
|
|
Redirects
to a new URL.
|
|
Removes
a scoped variable (from a particular scope, if specified).
|
|
Sets
the result of an expression evaluation in a 'scope'
|
|
Creates
a URL with optional query parameters.
|
|
Subtag
of <choose> that includes its body if its condition evalutes
to 'true'
|
<c:out>
Example
<%@
page language="java"
contentType="text/html;
charset=UTF-8"
pageEncoding="UTF-8"
isELIgnored="false"%>
<%@taglib
uri="http://java.sun.com/jsp/jstl/core"
prefix="c"%>
<!DOCTYPE
html PUBLIC
"-//W3C//DTD HTML 4.01
Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta
http-equiv="Content-Type"
content="text/html;
charset=UTF-8">
<title>JSTL
Example</title>
</head>
<body>
<c:out
value="welcome"></c:out>
<br>
The sum is : <c:out
value="${
2 + 5 }"></c:out>
</body>
</html>
<c:import>
Example
<%@
page language="java"
contentType="text/html;
charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib
uri="http://java.sun.com/jsp/jstl/core"
prefix="c"%>
<!DOCTYPE
html PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta
http-equiv="Content-Type"
content="text/html;
charset=UTF-8">
<title>Index
File</title>
</head>
<body>
<h4>Message!</h4>
<c:import
url="/viewMessage.jsp"></c:import>
</body>
</html>
<c:if>
Example
index.jsp
<%@
page language="java"
contentType="text/html;
charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE
html PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta
http-equiv="Content-Type"
content="text/html;
charset=UTF-8">
<title>Index
File</title>
</head>
<body>
<form
action="expression.jsp">
Enter
the A value :<input
type="text"
name="a"><br>
Enter
the B value :<input
type="text"
name="b"><br>
<input
type="submit"
value="sum"
name="operator">
<input
type="submit"
value="sub"
name="operator">
<input
type="submit"
value="mul"
name="operator">
</form>
</body>
</html>
expression.jsp
<%@
page language="java"
contentType="text/html;
charset=UTF-8"
pageEncoding="UTF-8"
isELIgnored="false"%>
<%@taglib
uri="http://java.sun.com/jsp/jstl/core"
prefix="c"%>
<!DOCTYPE
html PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta
http-equiv="Content-Type"
content="text/html;
charset=UTF-8">
<title>JSTL
Example</title>
</head>
<body>
<c:if
test="${param.operator
eq 'sum'
}">
The
sum is :${param.a + param.b}
</c:if>
<c:if
test="${param.operator
eq 'sub'
}">
The
sub is :${param.a - param.b}
</c:if>
<c:if
test="${param.operator
eq 'mul'
}">
The
mul is :${param.a * param.b}
</c:if>
</body>
</html>
<c:forEach>
Example1
<%@
page language="java"
contentType="text/html;
charset=UTF-8"
pageEncoding="UTF-8"
isELIgnored="false"%>
<%@taglib
uri="http://java.sun.com/jsp/jstl/core"
prefix="c"%>
<!DOCTYPE
html PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta
http-equiv="Content-Type"
content="text/html;
charset=UTF-8">
<title>Expression
Language</title>
</head>
<body>
<c:forEach
var="data"
begin="1"
end="10"
step="1">
<c:out
value="${data}"></c:out>
<br>
</c:forEach>
</body>
</html>
Example 2
import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/emp") public class Employee extends HttpServlet { private static final long serialVersionUID = 1L; public Employee() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Listlist = new ArrayList (); list.add("Manoj"); list.add("Krish"); list.add("Rajesh"); list.add("Balaji"); request.setAttribute("emp", list); RequestDispatcher rd = request.getRequestDispatcher("viewNames.jsp"); rd.forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
viewNames.jsp
<%@
page
language="java"
contentType="text/html;
charset=UTF-8"
pageEncoding="UTF-8"
isELIgnored="false"%>
<%@taglib
uri="http://java.sun.com/jsp/jstl/core"
prefix="c"%>
<!DOCTYPE
html PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta
http-equiv="Content-Type"
content="text/html;
charset=UTF-8">
<title>JSTL
Example</title>
</head>
<body>
<h3>Employee
Names are :</h3>
<table
border="2">
<tr><th>Names</th></tr>
<c:forEach
items="${emp}"
var="name">
<tr><td><c:out
value="${name}"></c:out></td></tr>
</c:forEach>
</table>
</body>
</html>
<c:forToken>
Examples
Example
1
<%@
page language="java"
contentType="text/html;
charset=UTF-8"
pageEncoding="UTF-8"
isELIgnored="false"%>
<%@taglib
uri="http://java.sun.com/jsp/jstl/core"
prefix="c"%>
<!DOCTYPE
html PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta
http-equiv="Content-Type"
content="text/html;
charset=UTF-8">
<title>Expression
Language</title>
</head>
<body>
<c:forTokens
items="lakshman,krish,balu,jsltech,manoj"
delims=","
var="name">
<c:out
value="${name}"></c:out>
<br>
</c:forTokens>
</body>
</html>
Example 2
<%@
page language="java"
contentType="text/html;
charset=UTF-8"
pageEncoding="UTF-8"
isELIgnored="false"%>
<%@taglib
uri="http://java.sun.com/jsp/jstl/core"
prefix="c"%>
<!DOCTYPE
html PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta
http-equiv="Content-Type"
content="text/html;
charset=UTF-8">
<title>JSTL
Example</title>
</head>
<body>
<c:forTokens
items="${names}"
delims=","
var="name">
<c:out
value="${name}"></c:out>
<br>
</c:forTokens>
</body>
</html>
<c:when><c:choose>
Example
index.jsp
<%@
page language="java"
contentType="text/html;
charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE
html PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta
http-equiv="Content-Type"
content="text/html;
charset=UTF-8">
<title>Index
File</title>
</head>
<body>
<form
action="viewGrade.jsp">
Enter
the salary :<input
type="text"
name="salary"><input
type="submit"
value="Enter">
</form>
</body>
</html>
viewGrade.jsp
<%@
page language="java"
contentType="text/html;
charset=UTF-8"
pageEncoding="UTF-8"
isELIgnored="false"%>
<%@taglib
uri="http://java.sun.com/jsp/jstl/core"
prefix="c"%>
<!DOCTYPE
html PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta
http-equiv="Content-Type"
content="text/html;
charset=UTF-8">
<title>JSTL
Example</title>
</head>
<body>
<c:choose>
<c:when
test="${param.salary
gt 50000
}">
<c:out
value="A
Grade Employee"></c:out>
</c:when>
<c:when
test="${param.salary
gt 30000
}">
<c:out
value="B
Grade Employee"></c:out>
</c:when>
<c:when
test="${param.salary
gt 15000
}">
<c:out
value="C
Grade Employee"></c:out>
</c:when>
<c:otherwise>
<c:out
value="He
doesn't have any grade"></c:out>
</c:otherwise>
</c:choose>
</body>
</html>
<c:set>
Example
<%@
page language="java"
contentType="text/html;
charset=UTF-8"
pageEncoding="UTF-8"
isELIgnored="false"%>
<%@taglib
uri="http://java.sun.com/jsp/jstl/core"
prefix="c"%>
<!DOCTYPE
html PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta
http-equiv="Content-Type"
content="text/html;
charset=UTF-8">
<title>Expression
Language</title>
</head>
<body>
<c:set
var="message"
value="Welcome
to JSTL Tech" scope="page"></c:set>
<c:out
value="${message}"></c:out>
</body>
</html>
<c:url>
Example
index.jsp
<%@
page language="java"
contentType="text/html;
charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib
uri="http://java.sun.com/jsp/jstl/core"
prefix="c"%>
<!DOCTYPE
html PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta
http-equiv="Content-Type"
content="text/html;
charset=UTF-8">
<title>Index
File</title>
</head>
<body>
<c:set
var="names"
value="Hari,Krish,Balu,Ramesh,Rajesh
Babu,RamaKrishna"
scope="session">
</c:set>
<a
href='<c:url
value="/viewNames.jsp"></c:url>'>view
Names</a>
</body>
</html>
viewNames.jsp
<%@
page language="java"
contentType="text/html;
charset=UTF-8"
pageEncoding="UTF-8"
isELIgnored="false"%>
<%@taglib
uri="http://java.sun.com/jsp/jstl/core"
prefix="c"%>
<!DOCTYPE
html PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta
http-equiv="Content-Type"
content="text/html;
charset=UTF-8">
<title>Expression
Language</title>
</head>
<body>
<c:forTokens
items="${names}"
delims=","
var="name">
<c:out
value="${name}"></c:out>
<br>
</c:forTokens>
</body>
</html>
<c:import>
Example
<%@
page language="java"
contentType="text/html;
charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib
uri="http://java.sun.com/jsp/jstl/core"
prefix="c"%>
<!DOCTYPE
html PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta
http-equiv="Content-Type"
content="text/html;
charset=UTF-8">
<title>Index
File</title>
</head>
<body>
<h4>Message!</h4>
<c:import
url="/viewMessage.jsp"></c:import>
</body>
</html>
viewMessage.jsp
<%@taglib
uri="http://java.sun.com/jsp/jstl/core"
prefix="c"%>
<font
color="green"><c:out
value="Welcome
to JSL Tech"></c:out></font>
<c:remove>
Example
<%@
page language="java"
contentType="text/html;
charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib
uri="http://java.sun.com/jsp/jstl/core"
prefix="c"%>
<!DOCTYPE
html PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta
http-equiv="Content-Type"
content="text/html;
charset=UTF-8">
<title>Index
File</title>
</head>
<body>
<c:set
value="JSL
Tech" scope="page"
var="message"></c:set>
Before
Removing :
<c:out
value="${message}"></c:out>
<br>
<c:remove
var="message"
scope="page"
/>
After
Removing :
<c:out
value="${message}"></c:out>
<br>
</body>
</html>
<c:param>
Example
index.jsp
<%@
page language="java"
contentType="text/html;
charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib
uri="http://java.sun.com/jsp/jstl/core"
prefix="c"%>
<!DOCTYPE
html PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta
http-equiv="Content-Type"
content="text/html;
charset=UTF-8">
<title>Index
File</title>
</head>
<body>
<c:url
value="/welcome.jsp"
var="myUrl">
<c:param
name="name"
value="JSLTech"
/>
<c:param
name="email"
value="lakshman.miani@gmail.com"
/>
</c:url>
<a
href="${myUrl}">ClickHere</a>
</body>
</html>
welcome.jsp
<%@
page language="java"
contentType="text/html;
charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib
uri="http://java.sun.com/jsp/jstl/core"
prefix="c"%>
<!DOCTYPE
html PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta
http-equiv="Content-Type"
content="text/html;
charset=UTF-8">
<title>Welcome
Page</title>
</head>
<body>
User
name :
<c:out
value="${param.name}"></c:out>
<br>
Email id :
<c:out
value="${param.email}"></c:out>
</body>
</html>
<c:exception>
Example
<%@
page language="java"
contentType="text/html;
charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib
uri="http://java.sun.com/jsp/jstl/core"
prefix="c"%>
<!DOCTYPE
html PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta
http-equiv="Content-Type"
content="text/html;
charset=UTF-8">
<title>Welcome
Page</title>
</head>
<body>
<c:catch
var="catchException">
<%
int
x = 5 / 0;
%>
</c:catch>
<c:if
test="${catchException
!= null}">
<p>
The
exception is : ${catchException} <br
/> There is an
exception:
${catchException.message}
</p>
</c:if>
</body>
</html>
<c:redirect>
Example
<%@
page
language="java"
contentType="text/html;
charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib
uri="http://java.sun.com/jsp/jstl/core"
prefix="c"
%>
<!DOCTYPE
html PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta
http-equiv="Content-Type"
content="text/html;
charset=UTF-8">
<title>Index
File</title>
</head>
<body>
<c:redirect
url="http://www.facebook.com/login.php?
email=lakshman@gmail.com"/>
</body>
</html>
<sql>
Tag Example
<%@
page language="java"
contentType="text/html;
charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib
uri="http://java.sun.com/jsp/jstl/core"
prefix="c"%>
<%@taglib
uri="http://java.sun.com/jsp/jstl/sql"
prefix="sql"%>
<%@taglib
uri="http://java.sun.com/jsp/jstl/fmt"
prefix="fmt"%>
<!DOCTYPE
html PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta
http-equiv="Content-Type"
content="text/html;
charset=UTF-8">
<title>Index
File</title>
</head>
<body>
<sql:setDataSource
var="dataSource"
driver="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@localhost:1521:xe"
user="lakshman"
password="lakshman"
scope="page"
/>
<sql:query
var="result"
dataSource="${dataSource}"
sql="select
empno,ename,job,sal from emp"></sql:query>
<c:forEach
items="${result.columnNames
}" var="col">
<c:out
value="${col}"></c:out>
</c:forEach>
<br>
<c:forEach
items="${result.rows}"
var="row">
<c:out
value="${row.empno}"></c:out>
<c:out
value="${row.ename}"></c:out>
<c:out
value="${row.job}"></c:out>
<fmt:formatNumber
value="${row.sal}"
minIntegerDigits="4"
minFractionDigits="2"></fmt:formatNumber>
<br>
</c:forEach>
</body>
</html>
Xml
Documentation
If
the XPath expression results in a boolean, <x:set> sets a
java.lang.Boolean object; for a string, java.lang.String; and for a
number, java.lang.Number.
book.xml
<?xml
version="1.0"
encoding="UTF-8"?>
<books>
<book>
<title>CoreJava</title>
<author>Lakshman</author>
</book>
<book>
<title>AdvancedJava</title>
<author>Harinath</author>
</book>
<book>
<title>ServletAndJsp</title>
<author>Rajesh</author>
</book>
<book>
<title>Kubuntu</title>
<author>Harinath</author>
</book>
</books>
index.jsp
<%@
taglib
uri="http://java.sun.com/jsp/jstl/xml"
prefix="x"%>
<%@
taglib
uri="http://java.sun.com/jsp/jstl/core"
prefix="c"%>
<html>
<head>
<title>Display
Student Information</title>
<style>
table
{
border:
1px solid blue;
width:
300px;
}
</style>
</head>
<body>
<c:import
var="xmlDoc"
url="book.xml"
/>
<x:parse
var="parsedDocument"
xml="${xmlDoc}"
/>
<table>
<tr><th>Title</th><th>Author</th></tr>
<x:forEach
select="$parsedDocument/books/book">
<tr>
<td><x:out
select="title"
/></td>
<td><x:out
select="author"
/></td>
</tr>
</x:forEach>
</table>
</body>
</html>
No comments:
Post a Comment