Using Java Beans in JSP
by, 05-24-2011 at 02:42 PM (2633 Views)
JavaBeans are classes written in the Java programming language conforming to a particular convention.
Consider the scenario: you are writing a JSP and you wish to write pure Java code in it. You may use scriptlets within JSP.
Using scriptlets means that your JSP will contains business logic as well which is not a good design approach. Ideally you would like to keep business logic separately in Java classes.Java Code:<% // Java code goes here %>
Here you can use reusable components called JavaBeans. JavaBeans in reality are simple Java classes very much like ActiveX controls: they provide known functionality and are
designed for off-the-shelf reuse.
You may access JavaBean's value through a set of properties, which provides access to the JavaBean's settings. For instance: Student is a JavaBean and student id, name, age, majors are properties.
Now lets get into details. JavaBean are written before JSP. You put the business logic in JavaBeans and then write JSP to use the beans where required. The first step is to tell the JSP page that it intends to use a bean.
<jsp:useBean> tag is used for this purpose. For example:
The <jsp:useBean> tag's id attribute is used to identify the bean. Bean will not be referred with this id through out the page. The class attribute is used to tell the page where to find the bean. You also must define the scope of bean using scope attribute. Using scope attribute, we tell the bean to maintain its information for the single page (the default) [scope="page"]; for the request [scope="request"]; for the session [scope="session"]; or for the whole application [scope="application"].Java Code:<jsp:useBean id="localName" class="com.test.Student" scope="application" />.
JavaBean's session scope is used to maintain items such as shopping carts very easily within JSP pages.
Now JavaBean is declared, we need to access its properties.
With the <jsp:getProperty> tag, we need to specify the following:Java Code:<jsp:getProperty> tag is used to get the value of a property.
- name of the bean to use (from the id field of useBean)
- the property whose value you want to get
To put value to JavaBean, we use <jsp:setProperty> tag. The requirements for this are as follwos:Java Code:<jsp:getProperty id="localName" property="name" />.
- identify the bean
- identify the property to modify
- provide the new value
If named properly, these can bedirectly acquired from a submitted form:
acquired from a parameter, in which you have to name the property and parameter directly:Java Code:<jsp:setProperty id="localName" property="*" />;
or just directly set with a name and value:Java Code:<jsp:setProperty id="localName" property="address" param="parameterName" />;
How can web server find you JavaBeans. You need to place their class files in a special place. Normally for Tomcat and JBoss, you need to place then under WEB-INF/classes folder.Java Code:<jsp:setProperty id="localName" property="serialNumber" value="string" /> // or <jsp:setProperty id="localName" property="serialNumber" value= <%= expression %> />.