Monday, December 9, 2013

JSP - Enhance the first JSP example with Java Beans

In the previous article, we saw a first example of using JSP, where Java was embedded into the HTML to perform the dynamic part.
In this article, we are going to enhance the structure of the previous example, by making use of a Java Bean.
This structure enhances readability, and separates the code of the user interface from the code to perform computations.

In the case of more complex applications, the structure of the first example would lead to a solution difficult to understand and maintain. The structure of the solution presented in this article is more flexible, readable and so more easy to maintain and evolve.

So, we shall create a Net Beans project, just like in the first example, and make the same modifications to index.jsp. Then, we create the new JSP file (newjsp.jsp):


Contrary to what we saw in the First JSP Example, this file has not embedded the code to obtain the parameters or the calculation of the operation result. It has, instead, a JavaBean component that is no more than a class who are passed the parameters and performs the required calculations.


Tag <jsp:useBean> is an action element used to instanciate a JavaBean component. In this case, the component to instanciate is class Calculator that is localized in the package Beans.

The scope of the useBean is defined as page, meaning that the instanced bean may be used anywhere in the current page.
Other possible scopes are:
request:  the bean may be used from any JSP that processes the same request.
session:  the bean may be used from any JSP in the same session of the JSP that created the bean.
application: the bean may be used from any page in the same application of the JSP that created the bean.

Tag <jsp:setProperty> is used to set the bean's parameter values. It is equivalent to invoking the setter of the identified class' property, and assigning it the value of the identified parameter (obtained in the request via URL).

Now, we only have to create our JavaBean:
- Create a package Beans:



- Then create a new class (Calculator) in that package:



The class' code is:

package Beans;

public class Calculator {
    private int lhs   = 0;
    private int rhs   = 0;
    private int result= 0;
    private String oper = "";

    public int getLhs() {
        return lhs;
    }

    public void setLhs(int lhs) {
        this.lhs = lhs;
    }

    public int getRhs() {
        return rhs;
    }

    public void setRhs(int rhs) {
        this.rhs = rhs;
    }

    public String getOper() {
        return oper;
    }

    public void setOper(String oper) {
        this.oper = oper;
    }
    
    public int getResult() {
        if(oper.equals("add")){
            result = lhs + rhs;
        }
        else if(oper.equals("sub")){
            result = lhs - rhs;
        }
        else if(oper.equals("mul")){
            result = lhs * rhs;
        }
        else if(oper.equals("div")){
            result = lhs / rhs;
        }
        return result;
    }
}


The result is exactly the same as the one obtained in the First JSP Example.

See you next time!!!


Related articles:



1 comment: