Reputation: 49
In my java code
package com.luv2code.jsp.tagdemo;
public class Student {
public String firstName;
public String lastName;
public boolean goldCustomer;
public Student(String firstName, String lastName, boolean goldCustomer) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.goldCustomer = goldCustomer;
}
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 boolean isGoldCustomer() {
return goldCustomer;
}
public void setGoldCustomer(boolean goldCustomer) {
this.goldCustomer = goldCustomer;
}
}
In JSP Code
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page import="java.util.*,com.luv2code.jsp.tagdemo.Student" %>
<%
ArrayList<Student> data=new ArrayList<>();
data.add(new Student("Subhajit","Maity",true));
data.add(new Student("Biswajit","Kundu",true));
data.add(new Student("Sourav","Das",false));
pageContext.setAttribute("myStudent", data);
%>
<html>
<head>
</head>
<body>
<c:forEach var="tempStudent" items="${myStudent}">
${tempStudent.firstName}
</c:forEach>
</body>
</html>
If I use getter and setter method for setting and getting the firstname,lastname and goldcustomer then the code works fine.But with out getter and setter it gives error.If I declare the class variable as public then it can be accessible to any where then ahy should I use getter and setter menthod?
Upvotes: 2
Views: 1248
Reputation: 78905
Your JSP page uses two several languages (besides HTML and JSP tags).
1 - Java
The code close to the top is regular Java:
ArrayList<Student> data=new ArrayList<>();
data.add(new Student("Subhajit","Maity",true));
If you wanted to, you could access the fields directly, i.e. without getter and setter methods:
Student student = ...;
student.lastName = "Maity";
2 - Expression Language
Tags such as <c:forEach items="${myStudent}">
and expressions such as ${tempStudent.firstName}
do not use Java but the Expression Language of Java EE. It is designed as a simple scripting language.
As per specification, the expression ${tempStudent.firstName}
accesses the property firstName of the JavaBean tempStudent. A JavaBean is basically any Java object that conforms to certain rules. In particular, getter and setter methods becomes properties.
As per JavaBeans Specification, chapter 7.1:
Properties are always accessed via method calls on their owning object.
That's why it doesn't work with fields only but requires getters and possibly setters.
Upvotes: 0
Reputation: 2608
You need to follow encapsulation rules for using JSP Pages. In your jsp you can call anything that start with get.
public String getAnyString() {
return "Any String";
}
You can call it to your JSP page. It doesn't look for you variables it will go for you encapsulated methods.
For readable properties there will be a getter method to read the property value. For writable properties there will be a setter method to allow the property value to be updated. Thus even when a script writer types in something such as “b.Label = foo” there is still a method call into the target object to set the property, and the target object has full programmatic control. So properties need not just be simple data fields, they can actually be computed values. Updates may have various programmatic side effects
From java doc Beans
Upvotes: 0
Reputation: 76817
The getter and setter are good tools for encapsulation. These methods may contain some logic besides their main purpose. You might want to get to check privileges whether the getter and setter should be executable, also, you might want to do some different things. For instance, if you set a husband's family name, you might need to also set the family name of the individual. You are not able to do this if you simply set the values of public data members.
Upvotes: 0
Reputation: 116
JSP/Servlet engine was designed to work following some basic principles. Among those principles is Encapsulation, which you should never give direct access to your class properties. When your jsp is compiled the EL expression ${tempStudent.firstName} will look like com.luv2code.jsp.tagdemo.Student.getFirstName() assuming you are following encapsulation principle.
That's is part of spec, you cannot change this behavior.
Upvotes: 2