JSP from simple to deep (10) — beans and forms processing

Forms is a very common way to interact on websites. JSP makes form processing easier. The standard way to process forms in JSP is to define a "bean". This "bean" is not a complete java bean. You just need to define a class so that it has an area corresponding to each area in the form. This class area must have "setters" to match the name of the form area. For example, let's modify getname. In the previous tutorial HTML and collect email addresses and user ages. The specific code is modified as follows:

What's your name? < INPUT TYPE=TEXT NAME=username SIZE=20>

What's your e-mail address? < INPUT TYPE=TEXT NAME=email SIZE=20>

What's your age? < INPUT TYPE=TEXT NAME=age SIZE=4>

In order to collect data, we need to define a Java class with "username", "email" and "age", and we need to provide "setter" methods "setusername", "setemail" and "setage". This "setter" method is just a method starting with "set" followed by the region name. The first letter of the area name should be capitalized. Therefore, if the region is "email", its "setter" method is "setemail". Similarly, the "getter" method is similarly defined. It just uses "get" instead of "set". 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; }

}

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>