JSP--beans and Forms processing

Source: Internet
Author: User
Tags define return
JS forms (forms) are a common way to interact on 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.