In Java programs or JSP programs, there are a lot of code snippets can be reused, such as the operation of the database, the user's effectiveness check and some of the implementation of certain functions. In order to solve this problem well and improve the efficiency of development, Sun has introduced JavaBean, in short, JavaBean This is a Java class that implements a feature and is intended for re-use. After using JavaBean in JSP, the separation between HTML and Java code can be realized, making JSP easier to develop and maintain. So JavaBean became one of the necessary tools for JSP programmers. In addition, JavaBean and JavaBeans are the same concept.
JavaBean Definition: is a special class in Java that can encapsulate multiple objects into an object (bean). The feature is serializable, providing a parameterless constructor that provides getter methods and setter methods to access the properties of the object. The "Bean" in the name is the usual term for reusable software components for Java.
JavaBean Specification:
(a) There is a public non-parametric constructor
(b) Properties can be accessed by get,set, is (can override get, use on Boolean properties) methods, or other methods that follow a specific naming convention
(c) Serializable
JavaBean Code:
1 Packageplayer;2 3 Public classPersonbeanImplementsjava.io.Serializable {4 5 /**6 * Name Attribute (note case)7 */8 PrivateString name =NULL;9 Ten Private Booleandeceased =false; One A /**no parameter constructor (no parameters)*/ - PublicPersonbean () { - } the - /** - * The Getter method for the name attribute - */ + PublicString GetName () { - returnname; + } A at /** - * Setter method for Name property - * @paramvalue - */ - Public voidSetName (FinalString value) { -Name =value; in } - to /** + * Getter method for deceased attributes - * Different forms of Getter methods for Boolean attributes (is used here instead of get) the */ * Public Booleanisdeceased () { $ returndeceased;Panax Notoginseng } - the /** + * Setter method for deceased property A * @paramvalue the */ + Public voidSetdeceased (Final Booleanvalue) { -deceased =value; $ } $}
Use the JavaBean code in the Java class:
1 Importplayer. Personbean;2 3 /**4 * <code>TestPersonBean</code> class5 */6 Public classTestpersonbean {7 /**8 * Main function of the Personbean class test method9 * @paramARGSTen */ One Public Static voidMain (string[] args) { APersonbean person =NewPersonbean (); - -Person.setname ("Zhang San"); thePerson.setdeceased (false); - - //output: "Zhang San [alive]" - System.out.print (Person.getname ()); +System.out.println (person.isdeceased ()? "[Deceased]": "[Alive]"); - } +}
there are two ways to use JavaBean in JSPs:
(a) embed Java code in JSP to access JavaBean
<%@ Page Import="player. Personbean" %><%Testpersonbean Testbean=NewTestpersonbean ();%><% Stringname=req.getparameter ("name");%><%testbean.setname (name)%><%=Testbean.getname ()%>
(b) use of <jsp:useBean> tags in jsp to access JavaBean
<Jsp:usebeanID= "Person"class= "Player." Personbean "Scope= "page"/><Jsp:setpropertyname= "Person" Property="*"/> <HTML> <Body>Name:<Jsp:getpropertyname= "Person" Property= "Name"/><BR/>deceased or not? <Jsp:getpropertyname= "Person" Property= "Deceased"/><BR/> <BR/> <formname= "Beantest"Method= "POST"Action= "testpersonbean.jsp">Enter your name:<inputtype= "text"name= "Name"size= " the"><BR/>Select options:<Selectname= "Deceased"> <optionvalue= "false">Stay alive.</option> <optionvalue= "true">Late</option> </Select> <inputtype= "Submit"value= "Test this JavaBean"> </form> </Body></HTML>
The <jsp:useBean> tag can declare a javabean in the JSP and use it. Once declared, the JavaBean object becomes a script variable that can be accessed through script elements or other custom tags. The syntax format for the <jsp:useBean> tag is as follows:
<id= "Bean ' s name" scope= "Bean ' s scope" typeSpec/ >
Use the <jsp:getProperty/> tag in the <jsp:useBean> tag body to invoke the Getter method, use the <jsp:setProperty/> tag to invoke the setter method, The syntax format is as follows:
<Jsp:usebeanID= "id"class= "Bean ' s Class"Scope= "Bean ' s Scope"> <Jsp:setpropertyname= "Bean ' s ID" Property= "Property name"value= "value"/> <Jsp:getpropertyname= "Bean ' s ID" Property= "Property name"/> ...........</Jsp:usebean>
The Name property refers to the bean's ID attribute. The property attribute refers to the getter or setter method that you want to invoke.
The scope property of the JavaBean is used in the JSP program:
(a) When scope=application, we browse date.jsp, which shows the system time. But no matter how we refresh, reopen the browser, or even change the machine, it shows the time is always unchanged, is the original time (that is, the bean has been created when the system time), because scope=application, so the bean instance in memory only one copy, At this point, the output does not change as long as the Web service is not restarted.
(b) When scope=session, browsing date.jsp, the display will not change when refreshed. But when we reopen a browser, a new session, the system creates an instance of the bean again and gets the current system time, which will get the correct time. Again, the new open page (the new session) is refreshed again, and the display does not change.
(c) when scope=page/request (they differ only in that they contain static files, there is no difference here), constantly refreshing the page continuously gets the current system time.
The JavaBean of JSP technology