JavaBean and JSP

Source: Internet
Author: User

In this section we summarize the relationship between JavaBean and jsp.

1. JavaBean

JavaBean is a Java class that follows a specific notation, and it usually has the following characteristics:
1) this Java class must have an parameterless constructor;
2) the attribute must be privatized;
3) the properties of privatization must be exposed to other programs through public type methods, and the naming of methods must follow certain naming conventions.
JavaBean is typically used to encapsulate data in a Java EE development, and for JavaBean components that follow the above notation, other programs can instantiate JavaBean objects through reflection technology, and learn the properties of JavaBean by reflecting the methods that obey the naming conventions. It then calls its properties to save the Data.

2. Using the JavaBean code in the JSP

JSP technology provides three action elements about the JavaBean component, the JSP tag:
<jsp:useBean> Tags : Used to look up a javabean component in a JSP Page.
<jsp:setProperty> Tags : Used to set the properties of a JavaBean component in a JSP Page.
<jsp:getProperty> Tags : Used to get the properties of a JavaBean component in a JSP Page.

Let me summarize the use of these three tags:

2.1 <jsp:useBean> Tags

The <jsp:useBean> label is used to find the JavaBean object of the specified name within the specified domain Scope. Returns a reference to the JavaBean object directly if it exists, instantiates a new JavaBean object if it does not exist, and saves it to the specified domain scope with the specified Name.
Common Syntax:

<id= "beanname"  class= "package.class"  scope= " Page|request|session|application "/></span>  

The ID property is used to specify the reference name of the JavaBean instance object and its name stored in the domain scope. The class attribute is used to specify the full class name of the JavaBean (that is, it must have a package name). The scope property is used to specify the domain scope stored by the JavaBean instance object, which can only be one of four values, such as Page\request\session and application, and the default is Page.

2.2 <jsp:setProperty>

The <jsp:setProperty> tab is used to set and access the properties of the JavaBean Object.
Common Syntax:

 <  jsp:setproperty  name    Property  = "propertyname"   value  =" {string|<%               =expression%>} "  | Property  = "propertyname"   [param  = "parametername"                 = "*"  />  

The Name property is used to specify the names of the JavaBean Objects. The property attribute is used to specify the attribute name of the JavaBean instance Object. The Value property is used to specify a property value for the JavaBean object, which can be a string or an expression: when it is a string, the value is automatically converted to the corresponding type of the JavaBean property, SetProperty can automatically convert the string to eight basic data types , but for a complex type that cannot be converted, such as date, or an expression, the expression must evaluate to the same type as the JavaBean property that you want to set.
The Param property is used to set a property value of an JavaBean instance object to a request parameter (specified in the address), and the property value is automatically converted to the type of the JavaBean property to be Set. * Used to get all request parameters to set the property Value.

3. <jsp:getProperty>

The <jsp:getProperty> tag is used to read the properties of the JavaBean object, The getter method of the JavaBean object is called, and the Read property value is converted to a string and inserted into the response body of the Output.
Common Syntax:

<name= "beaninstancename"  Property= "propertyname"/> 

The Name property is used to specify the names of the JavaBean instance objects whose values should be the same as the ID property values of the <jsp:usebean label. The property attribute is used to specify the attribute name of the JavaBean instance Object. If a property value of the JavaBean instance object is null, the resulting property value result will be a string with the content "null"

Let's look at a simple example to visualize the use of these three tags:

<%@page Import="Javabean.person"%>  <%@ Page Language="Java"Import="java.util.*"pageencoding="UTF-8"%>    <!DOCTYPE HTML public "-//w3c//dtd HTML 4.01 transitional//en">  <HTML>    <Head>          <title>Use of JSP tags</title>    </Head>    <Body>             <!--the Class Property must specify that the full class name label content of the Bean is executed only when the bean is instantiated, that is, when the bean is found in the domain (not instantiated), does not execute -      <%          //Pagecontext.setattribute (" person", Newperson ()); %>         <Jsp:usebeanID= "person"class= "javabean.person"Scope= "page">Label Content</Jsp:usebean>                          <Jsp:setproperty property= "name"value= "eson_15"name= "person"/>              <!--jsp:setproperty Tag when working, it automatically converts a string into eight basic data types, such as int type but cannot be converted automatically for complex types, such as date type -        <Jsp:setproperty property= "age"value= " a"name= "person"/>       <!--This can also be: <jsp:setproperty property= "age" value= "<%=12%>" name= "person"/> -               <Jsp:setproperty property= "birthday"value= "<%= new Date ()%>"name= "person"/>       <!--value if "1990-9-19" does not work, Jsp:setproperty cannot automatically convert time -                  <Jsp:setproperty property= "name"param= "name"name= "person"/>       <!--gets the request parameter of the request page Name:http://localhost:8080/day09/1.jsp?name=<span style= "font-family:microsoft YaHei;" >eson_15</span> -              <Jsp:setproperty property="*"name= "person"/>       <!--assigns a value to the Bean's property with all the request parameters http://localhost:8080/day09/1.jsp?name=eson_15&password=123&age=12 -                 <%System.out.println (person.getname ());          System.out.println (person.getage ());        System.out.println (person.getpassword ()); %>                <Jsp:getproperty property= "name"name= "person"/>         </Body>  </HTML>  

JSP and JavaBean is relatively simple, so much to introduce it ~ if there are errors, welcome message ~

JavaBean and JSP

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.