Home Tutorial Java Jsp Form processing using Bean

 
 

Share on Google+Share on Google+
Form processing using Bean
Posted on: June 26, 2010 at 12:00 AM
Advertisement
In this section, we will create a JSP form using bean ,which will use a class file for processing.

Form processing using Bean

In this section, we will create a JSP form using bean ,which will use a class file for processing. The standard way of handling forms in JSP is to define a "bean".  This is not a full Java bean.  You just need to define a class that has a field corresponding to each field in the form.  The class fields must have "setters" that match the names of the form fields. The html form looks like this :

<HTML>

<BODY>

<FORM METHOD=POST ACTION="SaveName.jsp">

<strong>ENTER INFORMATION :</strong><br>

What's your name? <INPUT TYPE=TEXT NAME=username SIZE=20><BR>

What's your e-mail address? <INPUT TYPE=TEXT NAME=email SIZE=20><BR>

What's your age? <INPUT TYPE=TEXT NAME=age SIZE=4>

<P><INPUT TYPE=SUBMIT>

</FORM>

</BODY>

</HTML>

To collect the data entered in form, we define a Java class with fields "username", "email" and "age" and we provide setter methods "setUsername", "setEmail" and "setAge".  A "setter" method is just a method that starts with "set" followed by the name of the field.  The first character of the field name is upper-cased.  So if the field is "email", its "setter" method will be "setEmail".  Getter methods are defined similarly, with "get" instead of "set".   Note that the setters  (and getters) must be public.

beanformprocess.java

package foo;

public class beanformprocess {

String username;

String email;

int age;

public void setUsername( String value )

{

username = value;

}

public void setEmail( String value )

{

email = value;

}

public void setAge( int value )

{

age = value;

}

public String getUsername() { return username; }

public String getEmail() { return email; }

public int getAge() { return age; }

}

beanformprocess1.jsp

Now let us create  "beanformprocess1.jsp" to use a bean to collect the data. 

<jsp:useBean id="user" class="foo.beanformprocess" scope="session"/>

<jsp:setProperty name="user" property="*"/>

<HTML>

<BODY>

<A HREF="NextPage.jsp">Continue</A>

</BODY>

</HTML>

The "setProperty" tag will automatically collect the input data, match names against the bean method names, and place the data in the bean.

beanformprocess2.jsp

Let us create "beanformprocess2.jsp" to retrieve the data from bean..

<jsp:useBean id="user" class="foo.beanformprocess" scope="session"/>

<HTML>

<BODY>

You entered<BR>

Name: <%= user.getUsername() %><BR>

Email: <%= user.getEmail() %><BR>

Age: <%= user.getAge() %><BR>

</BODY>

</HTML>

OUTPUT :

After submitting value the next page appears  like this:

Download Source Code

Advertisement

Related Tags for Form processing using Bean:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 26, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com