Aim: Demonstrate use of JSTL.
Index.jsp
<%@page contentType="text/html" 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>JSTL DEMO</title>
</head>
<body>
<h1>Hello World!</h1>
<a href="XMLDemo.jsp">XMLDemo</a>
<a href="SQLDemo.jsp">SQLDemo</a>
<a href="COREDemo.jsp">COREDemo</a>
<a href="FUNCTIONDemo.jsp">FUNCTIONDemo</a>
<a href="CUSTOMDemo.jsp">CUSTOMDemo</a>
</body>
</html>
COREDEMO.jsp
<%@page contentType="text/html" 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>JSP Page</title>
</head>
<body>
<h1>Core Demo</h1><hr>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<h4>c-out Demo..<br/>
<c:out value="6"></c:out></h4>
<c:set var="x" value="10"></c:set><br/>
<c:if test="${x eq 10}">
<h4>If Condition Checked..<br/>
Value of x is..<br/>
<c:out value="${x}"></c:out>
</h4>
</c:if>
</body>
</html>
SQLDEMO.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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>JSP Page</title>
</head>
<body>
<h1>SQL Demo</h1>
<hr>
<c:catch>
<sql:setDataSource
var="datasourceobj"
driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/book"
user="root" password="root"/>
<sql:query var="resultsetobj"
dataSource="${datasourceobj}">
Select * from book
</sql:query>
<table border="1">
<c:forEach var="row" items="${resultsetobj.rows}" >
<br>
<tr>
<th>Book</th>
<td> <c:out value="${row.bookname}" /></td>
<td><c:out value="${row.title}" /></td>
<td><c:out value="${row.author}" /></td>
</tr>
</c:forEach>
</table>
</c:catch>
Exception is : ${e}
</body>
</html>
FUNCTIONDEMO.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Function Demo</h1>
<c:set var="string1" value="This is Example of Function."/>
<p>Original String:${string1}</p><br/>
<hr>
<c:set var="string2" value="${fn:toUpperCase(string1)}" />
<c:set var="string3" value="${fn:toLowerCase(string1)}" />
<p>toUpper : ${string2}</p>
<p>toLower : ${string3}</p>
<p>Index of 'Example' : ${fn:indexOf(string1, "Example")}</p>
</body>
</html>
XMLDEMO.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %>
<html>
<head>
<title>For Each Examples</title>
</head>
<body>
<c:set var="students">
<students>
<student id="1">
<name>
<first>A</first>
<last>B</last>
<middle>T</middle>
</name>
<grade>
<points>88</points>
<letter>B</letter>
</grade>
</student>
<student id="2">
<name>
<first>C</first>
<last>D</last>
<middle>K</middle>
</name>
<grade>
<points>92</points>
<letter>A</letter>
</grade>
</student>
<student id="3">
<name>
<first>E</first>
<last>F</last>
<middle>A</middle>
</name>
<grade>
<points>72</points>
<letter>C</letter>
</grade>
</student>
</students>
</c:set>
<%--<c:import var="students" url="http://localhost:8080/students.xml" />
--%>
<x:parse var="doc" xml="${students}" />
<table>
<x:forEach var="student" select="$doc/students/student">
<tr>
<td>
<x:out select="$student/name/first" />
</td>
<td>
<x:out select="$student/name/last" />
</td>
<td>
<x:out select="$student/grade/points" />
</td>
<td>
<x:out select="$student/grade/letter" />
</td>
</tr>
</x:forEach>
</table>
</body>
</html>
CUSTOMDEMO.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@taglib prefix="mine" uri="/WEB-INF/tlds/custom" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>CUSTOM Demo</h1>
<hr>
<mine:custom user="${name}">
</mine:custom>
</body>
</html>
Custom tag descriptor:
<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd">
<tlib-version>1.0</tlib-version>
<uri>/WEB-INF/tlds/custom</uri>
<tag>
<name>custom</name>
<tag-class>customClass</tag-class>
<attribute>
<name>user</name>
</attribute>
</tag>
</taglib>
Custom class
import javax.servlet.jsp.tagext.*;
public class customClass extends SimpleTagSupport{
public String user;
@Override
public doTag() throws SimpleTagJSPexception,IOexception{
return getJSPContext().getOut().Write("hello"+user);
}
void setuser(string user){
this.user=user;
}
}
Index.jsp
<%@page contentType="text/html" 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>JSTL DEMO</title>
</head>
<body>
<h1>Hello World!</h1>
<a href="XMLDemo.jsp">XMLDemo</a>
<a href="SQLDemo.jsp">SQLDemo</a>
<a href="COREDemo.jsp">COREDemo</a>
<a href="FUNCTIONDemo.jsp">FUNCTIONDemo</a>
<a href="CUSTOMDemo.jsp">CUSTOMDemo</a>
</body>
</html>
COREDEMO.jsp
<%@page contentType="text/html" 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>JSP Page</title>
</head>
<body>
<h1>Core Demo</h1><hr>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<h4>c-out Demo..<br/>
<c:out value="6"></c:out></h4>
<c:set var="x" value="10"></c:set><br/>
<c:if test="${x eq 10}">
<h4>If Condition Checked..<br/>
Value of x is..<br/>
<c:out value="${x}"></c:out>
</h4>
</c:if>
</body>
</html>
SQLDEMO.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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>JSP Page</title>
</head>
<body>
<h1>SQL Demo</h1>
<hr>
<c:catch>
<sql:setDataSource
var="datasourceobj"
driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/book"
user="root" password="root"/>
<sql:query var="resultsetobj"
dataSource="${datasourceobj}">
Select * from book
</sql:query>
<table border="1">
<c:forEach var="row" items="${resultsetobj.rows}" >
<br>
<tr>
<th>Book</th>
<td> <c:out value="${row.bookname}" /></td>
<td><c:out value="${row.title}" /></td>
<td><c:out value="${row.author}" /></td>
</tr>
</c:forEach>
</table>
</c:catch>
Exception is : ${e}
</body>
</html>
FUNCTIONDEMO.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Function Demo</h1>
<c:set var="string1" value="This is Example of Function."/>
<p>Original String:${string1}</p><br/>
<hr>
<c:set var="string2" value="${fn:toUpperCase(string1)}" />
<c:set var="string3" value="${fn:toLowerCase(string1)}" />
<p>toUpper : ${string2}</p>
<p>toLower : ${string3}</p>
<p>Index of 'Example' : ${fn:indexOf(string1, "Example")}</p>
</body>
</html>
XMLDEMO.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %>
<html>
<head>
<title>For Each Examples</title>
</head>
<body>
<c:set var="students">
<students>
<student id="1">
<name>
<first>A</first>
<last>B</last>
<middle>T</middle>
</name>
<grade>
<points>88</points>
<letter>B</letter>
</grade>
</student>
<student id="2">
<name>
<first>C</first>
<last>D</last>
<middle>K</middle>
</name>
<grade>
<points>92</points>
<letter>A</letter>
</grade>
</student>
<student id="3">
<name>
<first>E</first>
<last>F</last>
<middle>A</middle>
</name>
<grade>
<points>72</points>
<letter>C</letter>
</grade>
</student>
</students>
</c:set>
<%--<c:import var="students" url="http://localhost:8080/students.xml" />
--%>
<x:parse var="doc" xml="${students}" />
<table>
<x:forEach var="student" select="$doc/students/student">
<tr>
<td>
<x:out select="$student/name/first" />
</td>
<td>
<x:out select="$student/name/last" />
</td>
<td>
<x:out select="$student/grade/points" />
</td>
<td>
<x:out select="$student/grade/letter" />
</td>
</tr>
</x:forEach>
</table>
</body>
</html>
CUSTOMDEMO.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@taglib prefix="mine" uri="/WEB-INF/tlds/custom" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>CUSTOM Demo</h1>
<hr>
<mine:custom user="${name}">
</mine:custom>
</body>
</html>
Custom tag descriptor:
<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd">
<tlib-version>1.0</tlib-version>
<uri>/WEB-INF/tlds/custom</uri>
<tag>
<name>custom</name>
<tag-class>customClass</tag-class>
<attribute>
<name>user</name>
</attribute>
</tag>
</taglib>
Custom class
import javax.servlet.jsp.tagext.*;
public class customClass extends SimpleTagSupport{
public String user;
@Override
public doTag() throws SimpleTagJSPexception,IOexception{
return getJSPContext().getOut().Write("hello"+user);
}
void setuser(string user){
this.user=user;
}
}
No comments:
Post a Comment