JSP--beans and Forms processing _jsp programming

Source: Internet
Author: User
Forms are a very common way to interact with a Web site. JSP makes form processing easier. The standard way to work with a form in a JSP is to define a "bean". This "Bean" is not a full Java bean. You just define a class so that it has an area that corresponds to each area of the form. This class area must have "setters" to match the name of the form area. For example, let's revise the getname.html of the previous tutorial and collect the email address and the age of the user. Specific code changes are 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 define a Java class that has "username", "email", "age" areas, and we provide "setter" methods "Setusername", "Setemail" and "Setage". This "setter" method is just a way to start with "set" followed by a region name. The first letter of the area name is capitalized. So if the area is "email", its "setter" method is "Setemail". Similarly, the Getter method is similar to a definition, it simply uses "get" instead of "set". and to make 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.