Home » Java programming language

Introduction to JSTL (Java Standard Tag Library)

Learn: JSTL (Java Standard Tag Library) - Example to validate inputs through JSTL.
By: Vanka Manikanth, on 09 MAR 2017

JSTL is a collection of JSP tags which combines the core functionality which are common to many JSP applications. JSTL supports to iterate, conditionals, handle database operations. And it also used to operate the XML, SQL as well. For all this you need a jar file of JSTL you can download it from http://tomcat.apache.org/taglibs/index.html and add it to your classpath. If at all you're using Eclipse just add them to lib folder and buildpath as well.

JSTL.jsp

This is the basic page where we take the inputs and validating that if the user entered age is above 18 or not.

<!DOCTYPE html>
<html>
	<head>
		<meta charset="ISO-8859-1">
		<title>Insert title here</title>
		<style type="text/css">
			form{
				text-align:center;
				border:2px solid #000;
			}
			p{
				text-align:center;
				font-size: 16pt;
			}
			h2{
				color:#216aF3;
				text-decoration: underline ;
			}
			button{
				width:100px;
				font-size:16pt;
			}
			input[type="text"]{
				font-size:16pt;
			}
			input[type="text"]:focus{
				background-color:lightyellow;
			}
		</style>
	</head>
	<body>
		<form action="jstlaction.jsp" method="post">
			<h2>SIMPLE JSTL EXAMPLE</h2>
			<p>Your Age:<input type="text" name="age"/></p>
			<p> NAME:<input type="text" name="user" /></p>
			<p>EMAIL:<input type="text" name="email"/></p><br>
			<button type="submit">Validate</button><br><br><br>
		</form>
	</body>
</html>

JSTLACTION.jsp

Here, we are validating age, if it is below 18, an error message will display else welcome message with input details will display.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
  <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<!DOCTYPE html5>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
		<title></title>
	</head>
	<body>
		<jsp:include page="jstl.jsp"></jsp:include>
		<c:catch>
		<c:choose>

		<c:when test="${param.age > 18}">
		<p><c:out value="${'Hi! Welcome'}"></c:out></p>
		<p>Your Name :<c:out value="${param.user}"/></p>
		<p>Your Age :<c:out value="${param.age }"></c:out></p>
		<p>Your Mail ID:<c:out value="${param.email}"/><br><br></p>
		</c:when>

		<c:when test="${param.age < 18}">
		<p><c:out value="${'Sorry!You dont have any access at this time'}"></c:out></p>
		</c:when>
		<c:otherwise>
		<c:out value="${'Enter valid details'}"></c:out>
		</c:otherwise>
		</c:choose>
		</c:catch>
	</body>
</html>

By simply, you can write a code without again writing the java code whereas JSTL has got html format with tags open and close.

Output:

jstl example with source code

jstl example with source code output




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.