JSP EL & JSTL Part-1
JSP EL & JSTL Part-1
JSP EL & JSTL Part-1
JSP Page
${ param.uname }
${ param.pword }
6) initParam:
It is used to retrieve initialization parameters.
Example:
web.xml
<init-param>
<param-name>message</param-name>
<param-value>Welcome</param-value>
</init-param>
JSP Page
${ initParam.message }
7) cookie:
It is used to retrieve cookie values.
Example:
set.jsp
<% Cookie c=new Cookie("book", "Java2 Complete Reference");
response.addCookie(c); %>
get.jsp
${ cookie["book"].value }
JSTL
JSTL stands for JSP Standard Tag Library.
JSTL introduced in JSP 2.0 version to simplify the JSP.
1) Core Tags:
1) <c:out>
2) <c:set>
3) <c:remove>
4) <c:if>
5) <c:choose>
6) <c:when>
7) <c:otherwise>
8) <c:forEach>
9) <c:forTokens>
10) <c:redirect>
In order to use the above tags we must include the following taglib directive in a
JSP.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
1) <c:out>:
It is used to display output messages.
2) <c:set>:
It is used to set the value in a variable.
3) <c:remove>:
It is used to remove the value from variable.
Example:
<html>
<body bgcolor=green text=yellow>
<h1>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="a" value="10" />
<c:out value="${ a }" />
<c:remove var="a" />
<c:out value="${ a }" />
</h1>
</body>
</html>
4) <c:if>:
It is used to express the condition.
Example:
<html>
<body bgcolor=green text=yellow>
<h1>
</c:otherwise>
</c:choose>
</h1>
</body>
</html>
8) <c:forEach>:
It is equivalent to for loop
Example:
<html>
<body bgcolor=green text=yellow>
<h1>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:forEach var="i" begin="1" end="10" step="1">
<c:out value="${ i }" /><br>
</c:forEach>
</h1>
</body>
</html>
9) <c:forTokens>:
It is equivalent to StringTokenizer class.
Example:
<html>
<body bgcolor=green text=yellow>
<h1>
SQL Tags:
1) <sql:setDataSource>
2) <sql:update>
3) <sql:query>
In order to use the above tags we must include the following taglib directive in a
JSP.
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
1) <sql:setDataSource>:
It is used to establish the connection between JSP and database.
2) <sql:update>:
It is used to execute non select queries.
3) <sql:query>:
It is used to execute select queries.
Example:
<html>
<body bgcolor=red text=yellow>
<table border=20 width=500 height=300>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<sql:setDataSource var="con" driver="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@localhost:1521:xe" user="system" password="manager" />
<sql:query var="rs" dataSource="${ con }" sql="select * from uinfo" />
<c:forEach var="record" items="${ rs.rows }">
<tr><td><c:out value="${ record.fname }" /></td>
<td><c:out value="${ record.lname }" /></td>
<td><c:out value="${ record.uname }" /></td>
<td><c:out value="${ record.pword }" /></td></tr>
</c:forEach>
</table>
</body>
</html>