Forms are a common way to interact with websites. JSP makes form processing easier. The standard way to process forms in JSP is to define a "bean ". This "bean" is not a full Java bean. You only need to define a class so that it has a region, which corresponds to each region in the form. This class area must have "setters" to match the name of the form area. For example, let's modify the previous tutorial's getname.html and collect the Email address and age of the user. Modify the Code as follows:
<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 data, we need to define a Java class so that it has "username", "email", and "age" areas, in addition, we need to provide the "setter" method "setUsername", "setEmail", and "setAge ". This "setter" method is just a method that starts with "set" and is followed by a region name. The first letter of the region name must be in uppercase. Therefore, if the region is "email", its "setter" method is "setEmail ". Similarly, the "Getter" method is similar to the definition. It only replaces "set" with "get. In addition, setters and getters must be public ). For example:
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 ;}
}