Reader Level:
ARTICLE

Action Tag: useBean in JSP

Posted by Vikas Mishra Articles | JSP November 29, 2011
The action tag is used to instantiate a java bean, or to locate an exisiting bean instance, and assign it to variable name or id. Bean is a reusable component which mostly contains the setter and getter values, we also called it as mutators.
  • 0
  • 0
  • 9396
Download Files:
 

Describing the useBean Action Tag:

The <jsp:useBean> action tag is used to instantiate a java bean, or to locate an exisiting bean instance, and assign it to variable name or id. Bean is a reusable component which mostly contains the setter and getter values, we also called it as mutators. We can also specify the life time of the object by giving it a specific scope. The <jsp:useBean> action tag ensures that the object is available, with the specified id, in the appropriate scope as specified by the tag.

Syntax for it is: <jsp:useBean attributes>
                       [body content]
                       </jsp:useBean>

E.g: <jsp:useBean id="regform" class="RegForm.RegForm" scope="session"/>

Attributes of the <jsp:useBean> Tag:

  • Id: The id attribute of the jsp:useBean action tag represents the variable name assigned to the id attribute of the jsp:useBean.
  • scope: The scope attribute represents the scope in which the bean instance has to be located or created. scopes can be page,request,session, application.

    • page: It means that we can use the Bean within the JSP page.
    • request: It means that we can use the Bean from any JSP page processing the same request.
    • session: It means that we use the Bean from any Jsp page in the same session as the JSP page that created the Bean.
    • application: It means that we use the Bean from any page in the same application as the Jsp page that created the Bean.

  • Class: It take the qualified class name to create a bean instance if the bean instance is not found in the given scope.
  • BeanName: The beanName attributes takes a qualified class name or an expression that resolve to a qualified class name or serialized template.
  • Type: The type attribute takes a qualified class or interface name which can be the class name given in the class or beanName attribute or its super type. 

For declaring the bean in JSP we follow the following steps:

  • Create a bean
  • Declare the bean in jsp by using the <jsp:usebean> tag
  • Access the bean properties
  • Generating the Dynamic content
  • Deploy and Run the application

EXAMPLE:

RegForm.java:

package RegForm;

public class RegForm implements java.io.Serializable{
private String uname,pass,repass,email,fn,ln,address;
public void setUserName(String s){uname=s;}
public void setPassword(String s){pass=s;}
public void setRePassword(String s){repass=s;}
public void setEmail(String s){email=s;}
public void setFirstName(String s){fn=s;}
public void setLastName(String s){ln=s;}
public void setAddress(String s){address=s;}


public String getUserName(){return uname;}
public String getPassword(){return pass;}
public String getRePassword(){return repass;}
public String getEmail(){return email;}
public String getFirstName(){return fn;}
public String getLastName(){return ln;}
public String getAddress(){return address;}
}

RegProcess.jsp:

<%@page errorPage="Registration.html" %>
<html>
<body bgcolor="skyblue">
<jsp:useBean id="regform" class="RegForm.RegForm" scope="session"/>
<jsp:setProperty name="regform" property="*"/>
<form action="RegProcessFinal.jsp" ><pre><b>
First Name:<input type="text" name="first-name"/>
Last Name :<input type="text" name="last-name"/>
Address :<input type="text" name="address"/>
<input type="submit" value="Register"/>
</b>
</pre>
</form>
</body>
</html>

ViewRegistrationDetails.jsp:

<jsp:useBean id="regform" class="RegForm.RegForm" scope="session"/><jsp:useBean id="regform" type="RegForm.RegForm" scope="session"/>
<%@page errorPage="Registration.html" %>
<html>
<body bgcolor="cyan">
<pre>
<b>User Name :</b><jsp:getProperty name="regform" property="userName"/>
<b>Password :</b><jsp:getProperty name="regform" property="password"/>
<b>Email_Id :</b><jsp:getProperty name="regform" property="email"/>
<b>First Name :</b><jsp:getProperty name="regform" property="firstName"/>
<b>Last Name :</b><jsp:getProperty name="regform" property="lastName"/>
<b>Address :</b><jsp:getProperty name="regform" property="address"/>
</pre>
</body>
</html>

Registration.html:

<html>
<body bgcolor="cyan">
<form action="RegProcess.jsp"><pre><b>
UserName :<input type="text" name="userName"/>
Password :<input type="password" name="password"/>
RePassword :<input type="password" name="repassword"/>
Email Id :<input type="text" name="email"/>
<input type="submit" value="Register"/>
</b></pre></form>
</body>
</html>

RegProcessFinal.jsp:

<%@page errorPage="Registration.html" %>
<jsp:useBean id="regform" class="RegForm.RegForm" scope="session"/>
<jsp:setProperty name="regform" property="firstName" param="first-name"/>
<jsp:setProperty name="regform" property="lastName" param="last-name"/>
<jsp:setProperty name="regform" property="address" />
<html>
<body bgcolor="pink">
<pre>
Your details are valid
<a href="ViewRegistrationDetails.jsp">CLICK</a>to view and confirm
</pre>
</body>
</html>

OUTPUT:

Registration.html:

html.gif

RegProcess.jsp:

regprocess.gif

RegProcessFinal.jsp:

regfinal.gif

RegDetail:

detail.gif

COMMENT USING

Trending up