Updated April 3, 2023
Introduction to JSP property
JSP properties are used to get and set the data while data flows from one web page to another. It works like getters, and setters inform action in HTML. Programmers can assign any value in the property and can derive the values from the properties too. The prerequisite to use Properties in JSP is that (Enterprise JAVA Bean) EJB is already defined. In this topic, we will see how properties are used to get the value from the user and keep it saved while transitioning from one page to another. This value then can be extracted to perform the action as per business logic.
Syntax
The syntax used by the JSP property is :
<jsp:setProperty name="Name_to_identify_bean"
property="name_of_property" />
<jsp:getProperty name="Name_to_identify_bean"
property="name_of_property" />
<jsp:useBean id="Name_to_identify_bean"
class="name_of_testpackage.JAVAClass" />
The “setProperty” or “getProperty” after colon JSP is the identifier for the compiler. By this compiler identifies that the property functionality of JSP has been used. “Name” tag is used to set the name to identify the JAVA bean, which will carry the property name along with values. “Property” is used to assign property names.
The useBean tag mentioned in the end is used to locate the remotely located bean, which can then be used for processing of JAVA beans. Note here that “name” on property and “id” in usebean JSP tags should be the same.
How does property work in JSP?
JSP property uses 4 attributes of “getProperty” and “setProperty” along with a JAVA Bean (EJB) to function. The four attributes in JSP property are:
- Name: This is a mandatory attribute. This attribute is used to identify the EJB. Refer to syntax carefully as this name should match to JSPBean’s name.
- Property: This is a mandatory attribute. This defines which properties should be set. For example, if “*” is given as a value for this parameter, then all the properties are set as per the setter method in EJB.
- Param: This attribute is optional. This attribute is equal to the request parameter name. This parameter is getting the incoming value that would be assigned to the property. “Param” and “Value” can not be used together as both of these attributes are used to assign value to the property in the tag itself, although the method of assigning values is different.
- Value: This attribute is optional. This attribute is used to set a value to the property in the tag itself rather than fetching it from some other place. If this attribute is not present or the value is zero, then the “setProperty” property is being ignored by the compiler.
- EJB: A JAVA bean should be defined with getting and set methods that would take the property values fetched from property tags and then assign them to the variables. These variables are sent again wherever required. We can understand more about its working with the help of an example provided in the below section.
Examples of JSP property
This is an example where we will create an index page with a form that collects email id, name, and password and then finally displays it in the output by carrying the user input name, password and email via properties. There are some files to be created to accomplish the task.
File: Index.html
<form action="useBean2.jsp" method="post">
Name:<input type="text" name="n"><br>
Password:<input type="password" name="p"><br>
Email:<input type="text" name="e"><br>
<input type="submit" value="register">
</form>
File: Test1.java
package test1JSP;
public class Test1 {
private String n,p,e;
//setters and getters
public String getName() {
return n;
}
public void setName(String name) {
this.n = name;
}
public String getPassword() {
return p;
}
public void setPassword(String password) {
this.p = password;
}
public String getEmail() {
return e;
}
public void setEmail(String email) {
this.e = email;
}
}
File: Test1.Class
/ Compiled from Test1.java (version 12 : 56.0, super bit)
public class test1JSP.Test1 {
// Field descriptor #6 Ljava/lang/String;
private java.lang.String n;
// Field descriptor #6 Ljava/lang/String;
private java.lang.String p;
// Field descriptor #6 Ljava/lang/String;
private java.lang.String e;
// Method descriptor #10 ()V
// Stack: 1, Locals: 1
public Test1();
0 aload_0 [this]
1 invokespecial java.lang.Object() [12]
4 return
Line numbers:
[pc: 0, line: 3]
Local variable table:
[pc: 0, pc: 5] local: this index: 0 type: test1JSP.Test1
File: useBean2.jsp
<jsp:useBean id="u" class="test1JSP.Test1"></jsp:useBean>
<jsp:setProperty property="*" name="u"/>
Record:<br>
<jsp:getProperty property="n" name="u"/><br>
<jsp:getProperty property="p" name="u"/><br>
<jsp:getProperty property="e" name="u" /><br>
Output:
Explanation:
In this example, three files are created named “Index.html”, “useBean2.jsp” and “Test1.java”. “Index.html” is simply the web page to input the values. Here the form is used to collect the details required. The “action” attribute in the form tag links this static HTML page to the dynamic JSP page named “useBean2.jsp”. This JSP page sets the property with the help of JSP property tags. One thing to notice here is that there is a “useBean” tag also defined on the JSP page, which establishes a link between the JSP page and the JAVA class. The name of the properties, the id of useBean tag is the same, which is “u”. “setProperty” sets all the properties with the value fetched from the HTML page and “getProperty” gets the value which is set via the “setProperty” tag. In the background, the JSP bean connects with the JAVA class page “Test1.java” and uses get and set methods to set the values. Get the method to fetch the value and displays it on the output screen.
Conclusion
JSP property is used commonly to streamline the data flow between multiple pages in the backend when we run any web application. In addition, it helps in enhancing user interactive sessions by the use of dynamic web content loading. One of the most useful tools for front end developers.
Recommended Articles
This is a guide to JSP property. Here we discuss How does property work in JSP and Examples along with the codes and outputs. You may also have a look at the following articles to learn more –