The Role of Java Standard Tag Library- EL (JSTL-EL) in JSP

Introduction

The JSTL EL or JSTL Expression Language is used by JSP programmers in order to avoid the usage of Java Code for accessing data. The EL statements are always used within { } along with the prefix $. Here is a simple example in which EL expression is used on the JSP page. The HTML page that contains the user name is first shown, and the entered username is then presented using a JSP page.

Source Code

Create an input.html page.

<!DOCTYPE html>
<html>
<head>
    <title>Form Example</title>
</head>
<body>
    <form method="post" action="demoel.jsp">
        <input type="text" name="ashish_text">
        <input type="submit" value="Submit">
    </form>
</body>
</html>

Create other page demoel.jsp page.

<%@ page contentType="text/html" %>
<%@ taglib prefix="c" url="http://java.sun.com/jsp/jstl/core" %>
<html>
    <body>
        You Have Entered String:
        <strong><c:out value="${param.ashish_text}" /></strong>
    </body>
</html>

Keywords Used

Various keywords used in EL Expression as : 
and, div, empty, eq, false, ge, gt, instanceof, le, lt, mod, ne, not, null

Operators Used 

Various operators that can be used as: Arithmetic operators like +,-,*,/,% (mod), Logical Operators like &&, || , ! , Relational Operators like : == or eq, != or ne, < or lt, <=or le, > or gt, >=ge

Declaring Variables and Methods

JSP allows us to use the user-defined variable and methods using declaration. These variables and methods can be used along with the scriptlets and expressions.

The syntax for declaration is as follows.

<%!
    // Any Java Coding
%>

Source Code

<%@ page language="java" contentType="text/html" %>
<%!
    String msg = "Hello";
%>
<%!
    public String MyFunction1(String msg) {
        return msg;
    }
%>
<html>
<head>
    <title>Define Variables</title>
</head>
<body>
    <% out.print("Before Function Call: " + msg) %>
    <br/>
    After Function call: <%= MyFunction1("Hello Users") %>
</body>
</html>

Another example of a JSP page that uses Date from java.util.date package.

<%@ page language="java" contentType="text/html" %>
<%@ page import="java.util.*" %>
<%
String showTime() {
    Date d = new Date();
    String msg = "Hello";
    if (d.getHours() < 12) {
        msg += " Morning !!!!!!";
    } else if (d.getHours() < 18) {
        msg += " Afternoon !!!!!!";
    } else {
        msg += " Evening !!!!!!";
    }
    return msg;
}
%>
<html>
<head>
    <title>Demonstration</title>
</head>
<body>
    <h2><%= showTime() %></h2>
</body>
</html>

Summary

The EL statements are always used within { } along with the prefix $. JSP allows us to use the user-defined variable and methods using declaration.


Similar Articles