Brief analysis of JavaBean

Source: Internet
Author: User

I. Overview

The JavaBean component is essentially a Java class, except that the encoding of this class follows some conventions. Users can use JavaBean to package functionality, processing, values, database access, and any other objects that can be created with Java code, and other developers can go through internal JSP pages, Servlets, other JavaBean, Applets or applications to use these objects. Users can assume that JavaBean provides a way to copy and paste anytime, anywhere, without worrying about any changes. This article will briefly summarize the knowledge points and basic usages involved in JavaBean.

Second, what is JavaBean?

JavaBean is a reusable component written in the Java language, essentially a class that has the following characteristics:

    • It is a class of public type, the property is private type, exposed to other programs through public method
    • Must have an argument-free constructor
    • Provides the Setxxx () method and the GetXXX () method for external programs to set and get JavaBean properties

A javabean consists mainly of three parts:

    1. Property: is an abstract representation of the internal state of a JavaBean component
    2. Method: Is the usual Java method, by default all methods can be called externally
    3. Event: The primary way for a bean to communicate information with other software organizations is to send and receive events

The following code is a typical javabean:

Package Com.test;public class person{    private String name= "AAA";    private int age;    Public String getName ()    {        return name;    }    public void SetName (String name)    {        this.name = name;    }    public int getage ()    {        return age;    }    public void Setage (int.)    {        this.age = age;    }    }

JavaBean can be seen as a black box, a software device that only needs to know its function without having to control its internal structure. The black box only describes and defines its external features and interfaces to other parts, such as buttons, windows, colors, shapes, handles, and so on. By using the system as a communication network associated with a black box, we can ignore the system details inside the black box, thus effectively controlling the overall performance of the system.

Third, the use of JavaBean

In JSP, you can access JavaBean as you would with a normal class, instantiate an object of a class in a SCRIPT element, invoke the object's method, and provide three action elements to access the JavaBean.

<jsp:usebean>: Used to instantiate a JavaBean or locate an existing JavaBean and assign a reference to the instance to a variable.

<%@ page language= "java" pageencoding= "UTF-8"%>

<jsp:setproperty>: Used with <jsp:userBean> to set properties for JavaBean.

<%@ page language= "java" pageencoding= "UTF-8"%>

You can also specify the name of the parameter in the Request object by Param:

<Jsp:setproperty Property= "Name"name= "Person"param= "Name"/>

Enter Http://localhost:8080/day_0623/1.jsp?name=songji in the browser and the page will display the value of the property name: Songji.

<JSP:GETPROPERTY>: Used to access the properties of a bean and convert the value of the property into a string and then send it to the output stream

<%@ page language= "java" pageencoding= "UTF-8"%>

View servlet files for JSP conversions (file directory: \workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp2\work\catalina\localhost\ DAY_0623\ORG\APACHE\JSP)

      Com.test.Person person = null;      Synchronized (_jspx_page_context) {person        = (Com.test.Person) _jspx_page_context.getattribute (' person ', Pagecontext.page_scope);        if (person = = null) {person          = new Com.test.Person ();          _jspx_page_context.setattribute ("person", person, pagecontext.page_scope);        }      }      Out.write ("\ r \ n");      Out.write ("\t\t\r\n");      Out.write ("\t\t");      Org.apache.jasper.runtime.JspRuntimeLibrary.introspecthelper (_jspx_page_context.findattribute ("person"), "name" , "Wangjian", NULL, NULL, FALSE);      Out.write ("\ r \ n");      Out.write ("\t\t\r\n");      Out.write ("\t\t");      Out.write (Org.apache.jasper.runtime.JspRuntimeLibrary.toString (((Com.test.Person) _jspx_page_ Context.findattribute ("person")). GetName ()));      Out.write ("\ r \ n");      Out.write ("\t</body>\r\n");

As you can see, <jsp:getproperty property= "name" name= "person"/> which is converted to the Code Red font section above, invokes the GetName method of the person class, and if the property does not define the method, it will error.

Brief analysis of JavaBean

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.