Wednesday, 5 May 2010

Develop a web application for registration form using Beans (java beans) in a JSP

                                                                Declaring a Bean in a JSP:

follow these steps to declare a bean in a JSP:
1. create a bean (java program)
2. Declare the bean in a JSP by using the <jsp:useBean> tag
3. Access the Bean properties
4. Generate Dynamic Content
5. Deploy and run the application

1. create a bean (java program)
Before declaring a Bean in a JSP , you must create a Bean:(java program)
/* creating Bean (java beans) called RegForm.java
 Before declaring a Bean in a JSP , we must create a Bean
 * */
package com.rajendra.jsp.beans;

import java.io.Serializable;

public class RegBean implements 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(fn); 
 }
 public String getFirstName()
 {
return(fn);
 }
 public String getLastName()
 {
return(ln);
 }
 public String getAddress()
 {
return(address);
 }
}//end of the class RegBean

2. Declare the bean in a JSP by using the <jsp:useBean> tag



  • After creating the Bean, you can declare it in a JSP page by using <jsp:useBean> tag, to declare this Bean in a JSP, You have to create a JSP

<%@ page errorPage="Registration.html"%>
<html>
 <body>
  <jsp:useBean id="regform" class="com.rajendra.jsp.beans.RegBean" scope="session"/>
  /*this is the jsp action tag used to declare bean*/
  <jsp:setProperty name="regform" property="*"/>
  <form action="RegProcessFinal.jsp" method="get">
  <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>b
 </body>
</html>


  • The <jsp:useBean> action tag creates an instance (object) of a bean class (RegBean.java) according to the attributes specified in the JSP page.
  • id: specifies the name of the Bean class, which helps the jsp container in searching the Bean class and creating its instance.
  • class: specifies name of the Bean class, which helps the jsp container in searching the Bean class and creating its instance
  • scope: specifies the scope of the bean
All these attributes help JSP Container to create an instance of the Bean class. The <jsp:useBean> action tag has some more attributes along with the attributes used in. You can also specify these attributes to make the Bean more specific.

3. Access the Bean properties



  • you can verify the bean properties by using the <jsp:getProperty> action tag. The <jsp:getProperty> tag  uses the name and property tags to read the Bean properties.

  • we have created the ViewRegistrationDetails.jsp page in this application to tells how the <jsp:getProperty> action tag is used to read the Bean property.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<jsp:useBean id="regform" type="com.rajendra.jsp.beans.RegBean" scope="session"/>
<%@page errorPage="Registration.html"%>
<html>
 <body>
 <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>
 <form action="javascript:alert('the remaining process is under construction');">
  <input type="submit" value="Register"/>
 </form>
 </body>
</html>
  • before reading the Bean properties, the JSP container needs to locate the instance of the Bean class (RegBean.java) created
  • After JSP Container locates the object of the Bean class, it starts reading the Bean properties by using the <jsp:getProperty> tag
<jsp:getProperty> tag attributes are: 
name: attribute specifies the name of the Bean class to the JSP container and the property attribute specific the Bean properties that have to be read

4. Generate Dynamic Content

  • now we need to create the html page Registration.html, to retrieve the required data from users.
  • this html page contains a button called Register, when a user clicks the Register button after providing all user details, that user is directed to the RegProcess.jsp page to set the properties of "RegBean" java Bean, based on the details entered by the user
  • in other words, we use the Registration.html page to generate dynamic content in the RegProcess.jsp page

<html>
 <body>
  <pre>
  <form action="RegProcess.jsp" method="post"/>
  
  <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>



  •  In the Registration.html page, the param names such as userName, password, and rePassword are the same as the property names in the RegForm bean
  • the other param names, such as first_name and last_name, are different from the property names such as firstName and lastName. Therefore, the RegProcess.jsp page is created to set these properties in the RegBean.
RegProcessFinal.jsp page also collects some more information about the user in the RegBean java bean in addition to what we have collected by using the Registration.html page. Here Register button is provided, when you click on that button after entering all user information, you are redirected to the RegProcessFinal.jsp page:


<%@page errorPage="Registration.html"%>
<jsp:useBean id="regform" type="com.rajendra.jsp.beans.RegBean" scope="session"/>
<jsp:setProperty name="regform" property="firstName" param="fist_name"/>
<jsp:setProperty name="regform" property="lastName" param="last_name"/>
<jsp:setProperty name="regform" property="address" param="address"/>

<html>
 <body>
  <pre>
   Your registration details are correct <a href="ViewRegistrationDetails.jsp">Click</a>
   to view Registration Details and Confirm
  </pre>
 </body>
</html>



                                                                   web.xml file (this is optional)
<web-app>
<display-name>DeclareBean</display-name>
<welcome-file-list>
<welcome-file>Registration.html</welcome-file>
</welcome-file-list>
</web-app>

                                                   5. Deploy and run the application



  • maintain the folder structure
  • Here my folder name is DeclareBean(project name), copy this folder and place into the <tomcat-home>/webapps folder and start the Tomcat Server:













What is java beans? and its advantages

Jsp Action Tags :

       <jsp:setProperty>

No comments:

Post a Comment