Definition of JavaBean, use in JSP, and internal operations

Source: Internet
Author: User

Definition of a JavaBean

JavaBean is a reusable and cross-platform software component. JavaBean can be divided into two types: one is a an with a User Interface (UI, User Interface); the other is that there is no User Interface, mainly responsible for processing transactions (such as data operations, operate the database. JSP usually accesses the latter type of JavaBean.

Features

A standard JavaBean has the following features:

  • JavaBean is a public (public) class.
  • JavaBean has a constructor without parameters.
  • JavaBean sets attributes using the setXXX method and obtains attributes using the getXXX method.
Access JavaBean from JSP

 

3.1 import JavaBean class
  • Use the <% @ page import> command to import the JavaBean class, for example, <% @ page import = "mypack. CounterBean" %>
3.2 declare the object of JavaBean
    Declare a JavaBean object and use the <jsp: useBean> label. For example, <jsp: useBean id = "myBean" class = "mypack. CounterBean" scope = "session"/>
  • <Jsp: useBean> attributes in the tag:
    • Id is to confirm the Bean variable in the defined range so that this variable name can be used in subsequent programs to distinguish different beans. This variable name is case sensitive, it must comply with the scripting language.
    • Class specifies the class to which the bean is created.
    • Scope specifies the bean survival range.
3.3 Access JavaBean attributes
    If you want to assign a value to a property of JavaBean, you can use the <jsp: setProperty> tag, for example: <jsp: setProperty name = "myBean" property = "count" value = "0"/> Set id to myBean (name value) the count value of Bean is set to "0" (value ).
    • JSP syntax format: <jsp: setProperty name = "beanInstanceName" {property = "*" | property = "propertyName" [param = "parameterName"] | property = "propertyName" value = "{string | <% = expression % >}"}/>
      • Name = "beanName" indicates the name (id) of the Bean instance created in "<jsp: useBean> ).
      • Property = "*" stores all the values entered by the user in jsp and is used to match the attributes in the Bean (in this case, the parameter name in jsp must be consistent with the attribute name in bean ).
      • Property = "propertyName" [param = "parameterName"] a parameter value is used to specify an attribute value in the Bean, which is generally obtained from the request object. Property specifies the Bean property name And param specifies the parameter name in the request.
      • Property = "propertyName" value = "{string | <% = expression %>}" Use the specified value to set Bean properties. This value can be a string or an expression. If it is a string, it will be converted to the Bean attribute type. If it is an expression, its type must be consistent with the type of the attribute value to be set. The param and value parameters cannot be used in the same <jsp: setProperty>.
  • To export a property of JavaBean to a webpage, you can use the <jsp: getProperty> label, for example: <jsp: getProperty name = "myBean" property = "count"/> obtains the value of the count attribute of the bean whose id is myBean (name value.
    • JSP syntax format: <jsp: getProperty name = "beanInstanceName" property = "propertyName"/>
      • Name = "beanInstanceName" bean name, which is specified by the id in <jsp: useBean>.
      • Property = "propertyName" specifies the Bean property name.
3.4 bean survival range:

The scope attribute determines the scope of the existence of the JavaBean object.

Optional scope values include:

    Page (default): each time a customer requests to access a JSP page, a JavaBean object is created. The valid range of the JavaBean object is the current JSP page requested by the customer. The life cycle of a JavaBean object ends in the following two cases:
    • The current JSP webpage requested by the customer forwards the request to another file using the <forward> flag
    • The current JSP page requested by the customer has been executed and returned to the client for response.
  • Request: each time the customer requests to access the JSP page, a new JavaBean object will be created.
    • The valid range of the JavaBean object is:
      • The current JSP page requested by the customer
      • Shares the page requested by the same customer with the current JSP page, that is, the <% @ include> command in the current JSP page and other JSP files marked by <forward>
      • When all the JSP pages that share the same client request are executed and echo to the client, the lifecycle of the JavaBean object ends.
    • The JavaBean object is saved as an attribute in the HttpRequest object. The attribute name is the id of the JavaBean object and the attribute value is a JavaBean object. Therefore, you can use HttpRequest. getAttribute () method to obtain the JavaBean object, for example, CounterBean obj = (CounterBean) request. getAttribute ("myBean ");
  • Session: After a JavaBean object is created, it exists throughout the lifecycle of the Session. JSP files in the same Session share this JavaBean object.
    • The JavaBean object is saved as a property in the HttpSession object. The property name is the id of the JavaBean object, and the property value is the JavaBean object. In addition to the id of the JavaBean object, you can also use HttpSession. getAttribute () method to obtain the JavaBean object, for example, CounterBean obj = (CounterBean) session. getAttribute ("myBean ");
  • Application: After a JavaBean object is created, it exists throughout the lifecycle of the Web application. All JSP files in the Web application can share the same JavaBean object.
    • The JavaBean object is saved as an attribute in the application object. The attribute name is the id of the JavaBean object. The attribute value is a JavaBean object. In addition to the id of the JavaBean object, you can also use the application. getAttribute () method to obtain the JavaBean object, for example, CounterBean obj = (CounterBean) application. getAttribute ("myBean ");

 

Four Javabean introspection operations

In java reflection, all classes are abstracted into a Class, so that we can dynamically load classes through the configuration file during the program running. However, there are some prerequisites for Using Reflection: When we call a constructor with parameters, we must first know the type of parameters passed in by the constructor; when calling Method, you must first know the type of the parameter passed in by the member function; when calling field, you must first know the type of the member variable. However, in some cases, the name of the member attribute in a class is invisible to the outside world. At this time, we can only obtain the array field of its member type [], the exact variable cannot be located without knowing the exact variable name. However, he provides the read (get) and write (set) methods exposed to this attribute. Although the method name and the object operated by the method sometimes have a large relationship, however, we cannot guarantee that this relationship must be established. In addition, this method only provides get and set methods, and does not provide variable names. Therefore, java abstracts this method into a javabean class and performs operations on the javabean class, get the variable name through the name after the get and set functions. Through this name, we can find the exact value of a variable. The operations on javabean are completed through introspector.

 

4.1

 

4.2

 

 

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.