I need to understand how to use a bean and update its properties in JSP page. Please share an example.

JSPJava 8Object Oriented ProgrammingProgramming

The useBean action is quite versatile. It first searches for an existing object utilizing the id and scope variables. If an object is not found, it then tries to create the specified object.

The simplest way to load a bean is as follows −

<jsp:useBean id = "name" class = "package.class" />

Once a bean class is loaded, you can use jsp:setProperty and jsp:getProperty actions to modify and retrieve the bean properties.

Following table lists out the attributes associated with the useBean action −

S.No.Attribute & Description
1class
Designates the full package name of the bean.
2type
Specifies the type of the variable that will refer to the object.
3beanName
Gives the name of the bean as specified by the instantiate () method of the java.beans.Beans class.

Let us now discuss the jsp:setProperty and the jsp:getProperty actions before giving a valid example related to these actions.

The <jsp:setProperty> Action

The setProperty action sets the properties of a Bean. The Bean must have been previously defined before this action. There are two basic ways to use the setProperty action −

You can use jsp:setProperty after, but outside of a jsp:useBean element, as given below −

<jsp:useBean id = "myName" ... />
   ...
<jsp:setProperty name = "myName" property = "someProperty" .../>

In this case, the jsp:setProperty is executed regardless of whether a new bean was instantiated or an existing bean was found.

A second context in which jsp:setProperty can appear is inside the body of a jsp:useBean element, as given below −

<jsp:useBean id = "myName" ... >
   ...
   <jsp:setProperty name = "myName" property = "someProperty" .../>
</jsp:useBean>

Here, the jsp:setProperty is executed only if a new object was instantiated, not if an existing one was found.

Following table lists out the attributes associated with the setProperty action −

S.No.Attribute & Description
1name
Designates the bean the property of which will be set. The Bean must have been previously defined.
2property
Indicates the property you want to set. A value of "*" means that all request parameters whose names match bean property names will be passed to the appropriate setter methods.
3value
The value that is to be assigned to the given property. The the parameter's value is null, or the parameter does not exist, the setProperty action is ignored.
4param
The param attribute is the name of the request parameter whose value the property is to receive. You can't use both value and param, but it is permissible to use neither.

The <jsp:getProperty> Action

The getProperty action is used to retrieve the value of a given property and converts it to a string, and finally inserts it into the output.

The getProperty action has only two attributes, both of which are required. The syntax of the getProperty action is as follows −

<jsp:useBean id = "myName" ... />
   ...
<jsp:getProperty name = "myName" property = "someProperty" .../>

Following table lists out the required attributes associated with the getProperty action −

S.No.Attribute & Description
1name
The name of the Bean that has a property to be retrieved. The Bean must have been previously defined.
2property
The property attribute is the name of the Bean property to be retrieved.

Example

Let us define a test bean that will further be used in our example −

/* File: TestBean.java */
package action;

public class TestBean {
   private String message = "No message specified";

   public String getMessage() {
      return(message);
   }
   public void setMessage(String message) {
      this.message = message;
   }
}

Compile the above code to the generated TestBean.class file and make sure that you copied the TestBean.class in C:\apache-tomcat-7.0.2\webapps\WEB-INF\classes\action folder and the CLASSPATH variable should also be set to this folder −

Now use the following code in main.jsp file. This loads the bean and sets/gets a simple String parameter −

<html>
   <head>
      <title>Using JavaBeans in JSP</title>
   </head>
   <body>
      <center>
         <h2>Using JavaBeans in JSP</h2>
         <jsp:useBean id = "test" class = "action.TestBean" />
         <jsp:setProperty name = "test" property = "message"
          value = "Hello JSP..." />

         <p>Got message....</p>
         <jsp:getProperty name = "test" property = "message" />
      </center>
   </body>
</html>

Let us now try to access main.jsp, it would display the following result −

Using JavaBeans in JSP
Got message....
Hello JSP...
raja
Published on 20-Mar-2019 06:33:52
Advertisements