JSP Tutorial

Use JSP Bean

Using a JavaBean class into JSP page the following tags are used to use a JavaBean class as a part of JSP:

<JSP:useBean/>:

This tag is used for creating an object of JavaBeans class as a part of JSP page.

Syntax:

<JSP:useBean	id="object name of a JavaBeans class"
             class="fully qualified name of JavaBeans class"
             scope="scope attribute"
             type="name of base interface or class" />
  • Here, JSP represents prefix or short name of useBean tag.
  • useBean represents a tag for representing details about JavaBeans class.
  • id and name are the mandatory attributes of useBean tag.
  • id represents object name of JavaBeans class.
  • name represents fully qualified name of JavaBeans class.
  • scope represents the visibility or accessibility of a JavaBeans class.

The scope attribute represents any one of the following:

  • page - represents a JavaBeans class object can be accessed in current JSP page only. It cannot be accessed in another JSP pages. By default the scope is page.
  • request - represents a JavaBeans class object can be accessed in those JSP pages which are participating in processing a single request.
  • session - represents a JavaBeans class object can be accessed in all the JSP pages which are participating in a session and it is not possible to access in those JSP pages which are not participating in session.
  • application - represents a JavaBeans class object can be accessed in all the JSP pages which belongs to the same web application but it is not possible to access in those JSP pages which are belongs to other web applications.

The type attribute represents specification of base interface or class name of a JavaBeans class.

For example:

<JSP:useBean id="eo"
             class="ep.Emp" 
             scope="session" 
             type="ep.GenEmp" />

When the above statement is executed the container creates an object eo is created in the following way:

ep.GenEmp eo=new ep.Emp ();

If we are not specifying the value for type attribute then the object eo is created in the following way:

ep.Emp eo=new ep.Emp ();

Note: In the above <JSP:useBean/> tag if we use a tag called <JSP:setProperty/> then that tag becomes body tag of <JSP:useBean/> tag.

<JSP:setProperty/>:

This tag is used for setting the values to the JavaBeans class object created with respect to tag.

Syntax-1:

<JSP:setProperty	name="object name of a JavaBeans class"
                         Property="property name of JavaBeans class"
                         Value="value for property" />

For example:

<JSP:useBean id="eo" class="ep.Emp">
    <JSP:setProperty	name="eo"
                        Property="empno" 
                        Value="123" />
</JSP:useBean>

When the above statement is executed by the container, the following statement will be taken place.

ep.Emp eo=new ep.Emp (); 
eo.setEmpno ("123");

The above syntax used to call a specific set method by passing a specific value statically.

Syntax-2:

<JSP:setProperty	name="object name of a JavaBeans class"
                        property="*" />
The above syntax is used for calling all generic set methods by passing there values dynamically.

For example:

<JSP:useBean id="eo" class="ep.Emp">
    <JSP:setProperty name="eo" property="*" />
</JSP:useBean>

Dynamically we can pass the values through HTML program to a JSP page. All the form fields of HTML program must be similar to data members or properties of a JavaBeans class and in the same order we must define set of set methods.

<JSP:getProperty/>:

This tag is used for retrieving the values from JavaBeans class object.

Syntax:

<JSP:getProperty	name="object name of JavaBeans class"
                        property="property name of JavaBeans class" />

For example:

<JSP:getProperty name="eo" property="empno" /> 

[or]

<%= eo.getEmpno()%>

Forexample (bean1):

web.xml:

<web-app>
</web-app>

Bean.html:

<html>
    <body>
        <h3>Bean tag test</h3>
        <form name="b1" action="bean.jsp" method="post">
            Enter ur name : <input type="text" name="b1_name"><p> Select the language :&nbsp;
                <select name="b1_lang">
                    <option value=""></option>
                    <option value="c"> C </option>
                    <option value="c++"> C++ </option>
                    <option value="java"> Java </option>
                    <option value=".net"> .NET </option>
                </select><p>
                <input type="submit" value="Send">&nbsp;
                <input type="reset" value="Clear">
        </form>
    </body>
</html>

Bean.jsp:

<html>
    <body>
        <jsp:useBean id="obj" class="tp.TechBean">
            <jsp:setProperty name="obj" property="*"/>
        </jsp:useBean>
        <h3>Result of bean action tags</h3>
        Hello <jsp:getProperty name="obj" property="b1_name"/><p>
        <jsp:getProperty name="obj" property="b1_lang"/><p>
        <jsp:getProperty name="obj" property="langComments"/><p>
        <h3>Result of expression tags</h3> Name : <%= obj.getName()%><br> Language : <%= obj.getLang()%><br> Comment : <%= obj.getLangComments%>
    </body>
</html>

TechBean.java:

package tp;

public class TechBean {

    String name;
    String lang;

    public TechBean() //recommended to write
    {
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setLang(String lang) {
        this.lang = lang;
    }

    public String getName() {
        return name;
    }

    public String getLang() {
        return lang;
    }

    public String getLangComments() {
        if (lang.equals("c")) {
            return ("Mahesh is the best faculty");
        } else if (lang.equals("c++")) {
            return ("Kalyan is the best institute for it");
        } else if (lang.equals("java")) {
            return ("Suresh is the best faculty ");
        } else if (lang.equals(".net")) {
            return ("Dinesh is the best faculty ");
        } else {
            return ("No idea..!");
        }
    }
};

For example (bean2):

web.xml:

<web-app>
</web-app>

CheckBean.html:

<html>
    <body>
        <form name="checkbean" action="CheckBean.jsp" method="post">
            Enter user name : <input type="text" name="checkbean_name" value=""><br>
            Enter password : <input type="password" name="checkbean_pwd" value=""><br>
            <input type="submit" value="Send">&nbsp;
            <input type="reset" value="Clear">
        </form>
    </body>
</html>

CheckBean.jsp:

<%@ page import="mypack.CheckBean" %>
<jsp:useBean id="check" class="CheckBean" scope="session">
    <jsp:setProperty name="check" property="*"/>
</jsp:useBean>
 <%= check.validate()%>

CheckBean.java:

package mypack;

public class CheckBean {

    String uname;
    String pwd;

    public CheckBean() {
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public String getUname() {
        return uname;
    }

    public String getPwd() {
        return pwd;
    }

    public boolean validate() {
        if (uname.equals("asha") && pwd.equals("krishna")) {
            return (true);
        } else {
            return (false);
        }
    }
};

Develop the JSP pages which illustrate the concept of implicit application object (application is an implicit object created with respect to ServletContext and the data can be accessed through out the entire web application)?

Answer:

First.jsp:

<html>
    <body>
        <h3>Application variable is defining</h3>
        <%!    String name = "asha";
            String pwd = "krishna";
        %>
        <%
            application.setAttribute("val1", name);
            application.setAttribute("val2", pwd);
        %>
        <a href="http://localhost:7001/application/second.jsp">Click here</a>
    </body>
</html>

Second.jsp:

<html>
    <body>
        <h3>Application variable is retrieving</h3>
        <%!    String name;
            String pwd;
        %>
        <%
            name = (String) application.getAttribute("val1");
            pwd = (String) application.getAttribute("val2");
        %>
        Name :: <%= name%> Password :: <%= pwd%>
    </body>
</html>