EL and complex JavaBeans1
EL means the expression language , it makes it possible to easily access application data stored in JavaBeans components. The jsp expression language allows a page author to access a bean using simple syntax such as $(name). Before JSP 2.0, we could use only a scriptlet, JSP expression, or a custom tag to include server state in the jsp page output. Using scripting in jsp all the time makes the program difficult to understand as it grows bigger in size. Expression Language (EL) was first introduced in JSTL 1.0. EL provides us a way to access the java code. EL is such a language which is liked by java programmers as well as by those who are not programmers like designers.
Java Beans: They are platform- independent component and usable software programs which you can use develop and assemble easily to create complex applications. JavaBean are also known as beans. Beans are called dynamic as they can be easily customized or changed.
In this example we have created one bean class consisting only of setter and getter method. These setter and getter method will be used in the jsp. To set the value in a jsp page use <jsp:setProperty> standard tag. We are using the EL to retrieve the value of the bean. To retrieve the value from the Html form or a jsp page and set those retrieved values in bean we use ${param.value}. This same as request.getParameter("value") used in scripting. To get the value from java bean by using EL we use ${instanceOfBean.value}. By using this we are able to retrieve the value which we have set in the bean.
The code of the program is given below:
<html> <head> <title>Complex Java Beans Using El</title> </head> <body> <form method = "get" action = "ComplexJavaBeansELProgram.jsp"> Enter your name = <input type = "text" name = "name"><br> Enter your age = <input type = "text" name = "age"><br> Enter your address = <input type = "text" name = "address"><br> Enter your phoneNo = <input type = "text" name = "phone"><br> <input type = "submit" name = "submit" value = "submit"> </form> </body> </html>
package Mybean; public class ComplexJavaBeansUsingEL{ private String name; private int age; private String address; private long phone; public void setName(String name){ this.name = name; } public String getName(){ return name; } public void setAge(int age){ this.age = age; } public int getAge(){ return age; } public void setAddress(String address){ this.address = address; } public String getAddress(){ return address; } public void setPhone(long phone){ this.phone = phone; } public long getPhone(){ return phone; } }
<jsp:useBean id="complex" class= "Mybean.ComplexJavaBeansUsingEL" scope="request" /> <jsp:setProperty name = "complex" property = "name" value = "${param.name}"/> <jsp:setProperty name = "complex" property = "age" value = "${param.age}"/> <jsp:setProperty name = "complex" property = "address" value = "${param.address}"/> <jsp:setProperty name = "complex" property = "phone" value = "${param.phone}"/> <html> <body> <h1>EL and Complex JavaBeans</h1> <table border="1"> <tr> <td>${complex.name}</td> <td>${complex.age}</td> <td>${complex.address}</td> <td>${complex.phone}</td> </tr> </table> </body> </html>
The output of the program is given below: