Processing of beans and forms in JSP

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

}
Once you define this class, compile it and place it in the class path of the network server. This server also needs to define a special directory to place bean classes. If you are using Blazix, you can place them in the "classes" directory. If you must change the class path and the server is running, the network server must be stopped and restarted.

Next let's modify "savename. jsp" to use bean to collect data:

<JSP: usebean id = "user" class = "userdata" Scope = "session"/>

<JSP: setproperty name = "user" property = "*"/>

<HTML>

<Body>

<A href = "nextpage. jsp"> continue </a>

</Body>

</Html>

Here all we need to do is add a JSP: usebean flag and JSP: setproperty flag. This usebean flag is used to find the "userdata" instance in the session. If an instance is found, it will update the old instance. If the instance is not found, it will create a new userdata instance (the userdata instance is called Bean ), then place it in the session. The setproperty flag automatically collects the input data, matches the bean method name, and places the data in the bean.

Next, modify nextpage. jsp to retrieve data from Bean:

<JSP: usebean id = "user" class = "userdata" Scope = "session"/>

<HTML>

<Body>

You entered <br>

Name: <% = user. GetUserName () %> <br>

Email: <% = user. getemail () %> <br>

Age: <% = user. getage () %> <br>

</Body>

</Html>

Note that the usebean label is reused. This bean is used as a variable of the class "userdata. Data typed by the user is collected in the bean. We can ignore savename.jsp”, and the goal of getname.html is "nextpage. jsp", and the data is equally valid as long as we add a JSP: setproperty flag. In the next tutorial, we will use savename. jsp as an error handling and automatically connect to nextpage. jsp. In other words, it will ask the user to correct the error data.

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.