This label is the most important label in my opinion.
Firstly, the characteristics of Java Bean are introduced.
It can be used on any machine with JVM without recompiling.
1. Write rules for JavaBean s:
(1) If the name of the member variable of a class is xxx, two methods can be defined in the class to change or obtain the value of the member variable:
getXxx() to get properties and setXxx () to set properties, pay attention to case.
(2) For member variables of boolean type, is is is allowed to replace get and set.
(3) Method attributes in a class must be Public.
(4) If there is a constructor in the class, then the constructor is public and has no parameters.
Example: A simple bean
Circle.java:
package bean;
public class Circle {
int radius;
public Circle(){
radius = 1;
}
public int getRadius(){
return radius;
}
public void setRadius(int newRadius){
radius = newRadius;
}
public double circleArea(){
return Math.PI * radius * radius;
}
public double circlLength(){
return 2.0 * Math.PI * radius;
}
}
2. Use beans
The useBean tag is used when using beans in the following format:
<jsp: useBean id= - "Name given to beans" class= "Create beans'class" scope= "bean valid range"> </jsp:useBean>
The scope attribute values need to be noted:
1.Scope takes pages: that is, when customers use beans, they occupy different space for each customer. In short, beans are not shared and users can not interfere with each other. Invalid when the user leaves the access page.
2.Scope takes session: that is, the bean is shared, and all users will operate on the same bean when accessing it, interfering with each other. Invalid when the client closes the browser.
3.Scope fetches request: valid only when the user accesses, and not shared. When the customer requests the trust, it will be invalid after the corresponding completion.
4.Scope fetches application: the shared bean is invalid when the server closes.
Finally, to use bean s, you must import their classes, using commands:
<@page import="className">
Example: Circle is a class file name
useBeans.jsp :
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="Circle"%>
<HTML>
<BODY bgcolor=cyan><Font size=1>
<jsp:useBean id="girl" class="Circle" scope="page" >
</jsp:useBean>
<%-- Through the above JSP Labels, customers get a scope of page ,The name is uuuuuuuuuuuu girl Of beans --%>
<% // Set the radius of the circle:
girl.setRadius(100);
%>
<P> The radius of a circle is:
<%=girl.getRadius()%>
<P> The circumference of a circle is:
<%=girl.circlLength()%>
<P> The area of a circle is:
<%=girl.circleArea()%>
</BODY>
</HTML>
3 Get and modify the properties of beans
getProperty and setProperty complete the command.
The formats are as follows:
<jsp: getProperty name="bean name"property="bean property"/> jsp:setProperty name = "bean name" property = "bean properties"/>
A comprehensive example:
student.jsp :
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="Student"%>
<HTML>
<BODY bgcolor=cyan><Font size=1>
<jsp:useBean id="zhang" class="Student" scope="page" >
</jsp:useBean>
<jsp:setProperty name= "zhang" property="name" value=" Zhang Xiaosan " />
<P> The name is:
<jsp:getProperty name= "zhang" property="name" />
<jsp:setProperty name= "zhang" property="number" value="1999001" />
<P> The student number is:
<jsp:getProperty name= "zhang" property="number" />
<% double height=1.70;
%>
<jsp:setProperty name= "zhang" property="height" value="<%=height+0.05%>" />
<P> Height is:
<jsp:getProperty name= "zhang" property="height" />
//rice
<jsp:setProperty name= "zhang" property="weight" value="67.65" />
<P> Weight:
<jsp:getProperty name= "zhang" property="weight" />
//Kg.
</FONT>
</BODY>
</HTML>