JSTL stands for (JSP Standard tag library). JSTL is a liberary that is available to use JSP tags to perform any logical and business operations. It is recommended to make use of JSTL rather than Scriptlet, Expression, or JSP elements.
In this JSTL tutorial, In order to read a bean object in JSP using JSTL. We need a Java web project using maven with a bean class and a JSP file.
Read bean in JSP using JSTL
Create a simple Java web Maven project in your ide(Eclipse or STS) and Add a Maven dependency for JSTL. below is the required maven dependency to use JSTL in your project.
<!-- https://mvnrepository.com/artifact/jstl/jstl --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency>
Copy the above code and add it into pom.xml in between the <dependencies></dependencies> tag.
Create a bean class (User.java)
Bean is a simple Java Class that contains getters and setters methods. In the below code, I have created a User bean with three private attributes firstName, lastName, and email.
Now, Under the default constructor of the User bean, Assign the default values as “Bhupi”, “Patel”, “bhupi@gmail.com”.
package com.jstltutorial.beans; public class User { private String firstName ; private String lastName ; private String email ; public User() { firstName = "Bhupi"; lastName = "Patel"; email = "bhupi@gmail.com"; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
Now in the JSP file(index.jsp), we will read the User bean and print these values on the browser page.
Create a JSP file(index.jsp)
The first thing that must be required in the JSP file is taglib. That is similar to importing the packages in java, To import JSTL code taglib use <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Read more about it.
In index.jsp, use <jsp:useBean></jsp:useBean> jstl tag with the arrtibutes Id = “userBean”, class=”com.jstltutorial.beans.User”, and scope=”page”.
Where Id is a unique name to read the values from user bean, class attribute defines the path of the bean class(User.java), and scope is used to define the access of the bean.
Now use the JSP expression to print the values from userBean. ${userBean.getFirstName()}
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" %> <%@ page isELIgnored="false" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <body> <h2>Read Beans in JSP Using JSTL</h2> <jsp:useBean id="userBean" class="com.jstltutorial.beans.User" scope="page"></jsp:useBean> ${userBean.getFirstName()} ${userBean.getLastName()} ${userBean.getEmail()} </body> </html>
Not able to print bean value using expression tag in JSP?
This is a very common error while you work with expression tags in JSP. In order to solve this error add a line <%@ page isELIgnored="false" %>
at the top of your JSP page.