Js
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;}
}
Once you have defined this class, compile it and place it in the classpath of the network server. This server also wants to define a special directory to place the Bean class. If you are using Blazix you can place them in the "Classes" directory. If you have to change the classpath and the server is running, the network server must stop and reboot.
Let's revise "savename.jsp" to use the 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>
All we need to do here is add a Jsp:usebean logo as well as a Jsp:setproperty logo. This Usebean flag is an instance of finding "UserData" in the session. If the instance is found, it updates the old instance, and if the instance is not found, it creates a UserData new instance (the UserData instance is called a bean) and puts it in session. The SetProperty flag automatically collects the input data and matches the name of the Bean method, placing the data in the bean.
The following modifies the nextpage.jsp to extract data from the 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 a variable of the class "UserData". Data that is typed by the user is collected in the bean. We can not need "savename.jsp", "getname.html" The goal is "nextpage.jsp", and as long as we add a Jsp:setproperty flag data is also valid. In the next tutorial, we'll use savename.jsp as an error handling and automatically connect to the nextpage.jsp, in other words, it calls the user to correct the wrong data.