Form processing in JSP

By: aathishankaran Emailed: 1697 times Printed: 2194 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

Form processing in JSP

Forms are a very common method of interactions in web sites.  JSP makes forms processing especially easy.

 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.  For instance, let us modify our GetName.html to also collect email address and age.

 The new version of GetName.html is

<HTML>

<BODY>

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

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 this data, we define a Java class with fields "username", "email" and "age" and we provide setter methods "setUsername", "setEmail" and "setAge", as shown.  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 uppercased.  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.

 package user;

 public class UserData {

    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; }

}

             The method names must be exactly as shown.  Once you have defined the class, compile it and make sure it is available in the web-server's classpath.  The server may also define special folders where you can place bean classes, e.g. with Blazix you can place them in the "classes" folder.  If you have to change the classpath, the web-server would need to be stopped and restarted if it is already running. 

 Note that we are using the package name user, therefore the file UserData.class must be placed in a folder named user under the classpath entry.

Now let us change "SaveName.jsp" to use a bean to collect the data.

 

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

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

<HTML>

<BODY>

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

</BODY>

</HTML>


JSP Home | All JSP Tutorials | Latest JSP Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(3)


1. View Comment

how about the field is multiple times?
is it retrieved as array or string also by the bean?

I thought we pass the form through the servlet...
and do manually detecting the field.... :(

Correct me if i'm wrong.


View Tutorial          By: g at 2010-10-11 01:09:59
2. View Comment

How can I execute the JSP program?
Please explain in detailed manner...


View Tutorial          By: Suresh at 2013-01-04 06:43:10
3. View Comment

can u plz send me the source code for employee salary details for every month by using jsp
for example current empsal is 8000 in jan emplyoee was abscent in 2 days so how much salary can take the employee on the month...
if u give the input for emp name month and salary then dispaly the output for employee detail and taken the salary of employee plz send me the source code its urgent


View Tutorial          By: prudhvi at 2013-01-27 13:47:12

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by aathishankaran
Web Security Issues
The Web User's Perspective
Server-side plug-Ins
The best way to avoid security vulnerabilities with new server
JavaScript Security
Window Object
Working with Status Bar Messages
Retrieving a Portion of a String
Referencing Windows
Math Object
Frame Object
Document Object
Closing Windows
Built-in Object in Javascript
Textarea Object

More Tutorials in JSP
LifecycleException: service.getName(): "Catalina"; Protocol handler start failed: `java.net.BindException: Permission denied <null>:80
JSP Alert Example
JSP CheckBox Example
Uploading an Image to a Database using JSP
Uploading a file to a server using JSP
A JSP page that gets properties from a bean
The page Directive in JSP
The taglib, tag, include, attribute and the variable Directive in JSP
Declarations in JSP
Scriptlets and Expressions in JSP
Tag Libraries in JSP
The Request Object in JSP
The Response Object in JSP
The Out Object in JSP
The Session Object in JSP

More Latest News
Most Viewed Articles (in JSP )
Comparison operators in JSP
Sending Email using JSP
JSP CheckBox Example
JSP Program for display Date
What are the different scopes in JSP?
JSP Alert Example
The Advantages of JSP
Radio Object
Arithmetic Evaluation Using the Expression Language in JSP
JSP Example to connect to MS SQL database and retrieve records
Writing your first JSP page
Getting Started with JSP
JSP Directives
Form processing in JSP
A Simple Servlet Generating Plain Text
Most Emailed Articles (in JSP)
What is JSP?
The JSP Program running first Time.
Scriptlets
Embedding java codes in jsp sciptlets
Scriptlets and Expressions in JSP
Sessions in JSP
Simple HTML-Building Utilities
Text Object
Server-side plug-Ins
The Response Object in JSP
Cookies using JSP or Java Bean
Click to Activate and Use this control
JSP pages in servlet
Declaring variable in JSP
Protecting your website with a login page