JSP from simple to deep (10) -- Beans and Forms processing

Source: Internet
Author: User

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

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.