Use Of Form Bean In JSP

This section gives you the best illustration about the procedure of handling sessions by using Java Bean.

Use Of Form Bean In JSP

Use Of Form Bean In JSP

        

This section gives you the best illustration about the procedure of handling sessions by using Java Bean. This section provides the complete code of example with it's output particularly based on the module of an application. Following example helps you in developing your Java web based application. You can easily copy the code in you application for handling values or data using session through the Java Bean.

Program Summary:

There are three file have been used in the section for giving the complete soln of the topic "Handling Session From The Form Bean In JSP". These files are explained ahead particularly.

Here is the code of the getname.html file:

Following code has been used for constructing a form that takes some value like the name of the person, Email ID, Phone Number and the Age of the person. After submission the form it will refer to the savename.jsp file that will show the anchor tagged text "continue" and this file also sets all the value from the getname.html form to the Java Bean and proceed the value for the next operations with the session environment.

<HTML>
     <BODY>
          <FORM METHOD="POST" ACTION="savename.jsp">
          <center>
               <table bgcolor="#FFFFCC" cellpadding="0"cellspacing
="0" height="30%" width="50%">
                   <tr>
                      <td style="border-bottom-width:1px; border-
bottom-style:solid; border-bottom-color:#00000;"><B>User Name:<sup>
*</sup></B></td>
                      <td style="border-bottom-width:1px; border-
bottom-style:solid; border-bottom-color:#00000;"><INPUT TYPE=TEXT 
NAME=username SIZE="30"></td>
                   </tr>
                   <tr>
                      <td style="border-bottom-width:1px; border-
bottom-style:solid; border-bottom-color:#00000;"><B>Email Address:
<sup>*</sup></B></td>
                      <td style="border-bottom-width:1px; border-
bottom-style:solid; border-bottom-color:#00000;"><INPUT TYPE=TEXT 
NAME=email SIZE="20"></td>
                   </tr>
                   <tr>
                      <td style="border-bottom-width:1px; border-
bottom-style:solid; border-bottom-color:#00000;"><B>Phone Number:
<sup>*</sup></B></td>
                      <td style="border-bottom-width:1px; border-
bottom-style:solid; border-bottom-color:#00000;"><INPUT TYPE=TEXT
 NAME="pnumber" SIZE="11"></td>
                   </tr>
                   <tr>
                      <td style="border-bottom-width:1px; border-
bottom-style:solid; border-bottom-color:#00000;"><B>Age:<sup>*</
sup></B></td>
                      <td style="border-bottom-width:1px; border-
bottom-style:solid; border-bottom-color:#00000;"><INPUT TYPE=TEXT
 NAME=age SIZE="3"></td>
                   </tr>
                   <tr>
                      <td><INPUT TYPE="submit" value="Submit"/></td>
                   </tr>
               </table>
          </center>
          </FORM>
     </BODY>
</HTML>

Form created by the above code of the html file:


Code of the savename.jsp file.

<jsp:useBean id="user" class="roseindia.net.UserData" scope="session"/> 
<jsp:setProperty name="user" property="*"/>

<html>
     <body bgcolor="#ff99cc">
        <center>
        <a href="nextPage.jsp"><b>Continue</b></a>
        </center>
     </body>
</html>

Output for the above code of the savename.jsp file.



Following is the code of the Java Bean named UserData.java:

package roseindia.net;
public class UserData{
    String username;
    String email;
    String pnumber;
    int age;
    public void setUsername(String value){
         username = value;
    }

    public void setEmail(String value){
         email = value;
    }

    public void setPnumber(String value){
         pnumber = value;
    }

    public String getPnumber(){
         return pnumber;
    }

    public void setAge(int value){
         age = value;
    }

    public String getUsername(){
         return username;
    }

    public String getEmail(){
	 return email;
    }

    public int getAge(){
         return age;
    }
}

Code of the nextPage.jsp file:

<jsp:useBean id="user" class="roseindia.net.UserData" scope=
"session"/> 
<HTML>
     <BODY bgcolor="#FFFFCC">
       <center>
       <table border="0"  cellpadding="0" cellspacing="0" 
height="40%" width="40%">
          <tr>
             <td><FONT SIZE="4" COLOR="#333399">You have 
entered the following information:</FONT><BR></td>
          </tr>
          <tr>
             <td><B>Name:</B> <%= user.getUsername() %>
<BR></td>
          </tr>
          <tr>
             <td><B>Email:</B> <%= user.getEmail() %>
<BR></td>
          </tr>
          <tr>
             <td><B>Phone Number:</B> <%= user.getPnumber()
%><BR></td>
          </tr>
          <tr>
             <td><B>Age:</B> <%= user.getAge() %><BR></td>
          </tr>
       </table>
       </center>
     </BODY>
</HTML>

Output for the last file nextPage.jsp:

Above output shows the complete summary for the entered values in the getname.html constructed form.

Download complete example.