Forms are a very common method of interactions in web sites.
JSP makes easy forms processing.
A D V E R T I S E M E N T
To Create Form
Here we show how to create and process an html form.
Code below you save as a file name myform.jsp
Go to myform.jsp in your browser and open it.
It won't do anything yet.
<html>
<head>
<!-- Example4 -->
<title>VisualBuilder.com</title>
</head>
<body>
<form action="myformconfirm.jsp" method="post">
Enter in a website name:<br>
<input type="text" name="website"><br>
<input type="submit" name="submit">
</form>
</body>
</html>
Processing a Form
Code written here which process the html form your just created.
Copy the code below and place in a file named: myformconfirm.jsp
Go to myform.jsp
Fill in some details and submit the form
You should see the results of your submission
<html>
<head>
<!-- Example4 -->
<title>VisualBuilder.com</title>
</head>
<body>
<font size=3>
Your info has been received:
<br><br>
<%
String sName = request.getParameter("website");
out.print(sName);
%>
</font>
</body>
</html>
Beans and Form Processing
The standard way of handling JSP forms 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 GetName.html to also
collect email address and age.
To collect 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 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 &
getters method 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 below. 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.
Now we have to add the jsp:useBean tag and the jsp:setProperty
tag! The useBean tag will look for an instance of the "user.UserData" in the
session. If the instance is already there, it will update the old instance.
Otherwise,it will create a new instance of user.UserData (the instance of the
user.UserData is called a bean), and put it in the session.
The setProperty tag will automatically collect the input data, match names
against the bean method names, and place the data in the bean!
Let us modify NextPage.jsp to retrieve the data from bean..
Notice that the same useBean tags is repeated. The bean is available as the
variable named "user" of class "user.UserData". The data entered by the user is
all collected in the bean.
We do not actually need the "SaveName.jsp", the target of GetName.html could
have been NextPage.jsp, and the data would still be available the same way as
long as we added a jsp:setProperty tag. But in the next tutorial, we will
actually use SaveName.jsp as an error handler that automatically forwards the
request to NextPage.jsp, or asks the user to correct the erroneous data.