Servlet & JSP (13)

Source: Internet
Author: User

This article discusses JavaBean. In the previous servlet & JSP events (12), we wrote a simple message board, but there are some problems in this small project. For example, we include a large number of Java code in the JSP page, which is fine for small projects, but for large projects, if the page editor accidentally deletes some java code, an error occurs. Java programmers will face a lot of HTML code and will also have an impact on development. To separate the HTML code and Java code on the page, JavaBean came into being. It separates the display logic from the business logic, allowing front-end and back-end developers to focus on their own work. In JSP, the Java class responsible for completing the business logic is called JavaBean.

JavaBean

In fact, it is essentially a Java class, but it only follows some coding conventions. On the JSP page, you can instantiate an object of the JavaBean class just like a normal class. Of course, you can also use the action elements in JSP to access JavaBean. A standard JavaBean has the following features:

1) It is a public class
2) It has a default constructor, that is, a constructor without parameters.
3) it provides the setxxx () and getxxx () methods for external programs to set or obtain the attributes of JavaBean.
4) it implements the serializable or externalizable interface to support serialization.

JavaBean has four attributes: simple property, indexed property, bound property, and constrained property ). A simple attribute is an attribute that accepts a single value. An index attribute is the attribute used to obtain and set an array. The binding attribute and constraint attribute are mainly used for graphic interface programming.

Use Javabean in JSP

In JSP, you can access JavaBean like a normal class. JSP provides three action elements <JSP: usebean>, <JSP: setproperty>, and <JSP: getproperty> to access JavaBean.

<JSP: usebean> it is used to instantiate a JavaBean instance, locate an existing JavaBean instance, and assign the instance reference to a variable. The syntax is as follows:

<jsp:useBean id="name" scope="page|request|session|application" typeSpec/>

The typesepc is defined as follows:

class="classname"|class="classname" type="typename"|type="typename" beanName="classname"

<JSP: usebean> attributes are described as follows:

1) ID is used to identify the name of the JavaBean instance. It is also the name of the declared script variable and initialized as a reference of the JavaBean instance. Follow the Java variable naming rules.
2) Scope specifies a range. Within this range, an instance reference is available. The default value is page.
3) class is used to specify the complete qualified class name of the JavaBean object.
4) beanname is used to specify the bean name. This name is provided to the instantiate () method of the Java. Beans. Beans class to instantiate a JavaBean.
5) type specifies the type of the defined script variable, which can enable the Bean class itself, its parent class, or the interface implemented by the JavaBean class.

The execution process is: the JSP Container searches for the JavaBean object of the specified ID within the range specified by the <JSP: usebean> element. If the JavaBean object is found and the type attribute is specified, the JSP Container will try to convert the object to the specified type. If the object fails, a classcastexception is thrown. If the specified JavaBean object is not found, an instantiationexception is thrown. If this object is not found, but the class attribute is specified in the element, a new object is created using this class and the reference of this object is assigned to the variable with the name specified by the property ID, and save the object to the range specified by the attribute scope. If this object is not found, but the beanname attribute is specified in the element, the instantiate () method of the beans class is called. If the method is successfully executed, assign the reference of the newly created object to the variable named by the attribute ID, and save the object to the range specified by the attribute scope.

<JSP: setproperty> and <JSP: usebean> are used together to set simple attributes and index attributes of JavaBean. The syntax is as follows:

<jsp:setProperty name="beanname" prop_expr />

The definition of prop_expr is as follows:

property="*"|property="propertyname"|property="propertyname" param="parametername"|property="propertyname" value="propertyvalue"

<JSP: setproperty> attributes are described as follows:

1) Name: name of the JavaBean instance. It must be a name defined by the ID attribute in the <JSP: usebean> element.
2) property: the name of the property to be set. If the value is "*", the tag searches for all Request Parameters in the request object to see if the parameter name is the same as the bean attribute name. If yes, the tag matches.
3) Param: Specifies the parameter name in the request object. If the name of the request parameter is different from that of the bean attribute, you can use Param to specify the parameter name.
4) value: Specifies the value to be assigned to the JavaBean attribute. It cannot appear together with Param.

<JSP: getproperty>

It is used to access a JavaBean attribute, convert the attribute value into a string, and then send it to the output stream. If the attribute is an object, the tostring () method of the object is called. The syntax is as follows:

<jsp:getProperty name="beanname" property="propertyname" />

The meaning of each attribute is as follows:

1) Name: the name of the JavaBean instance. You can obtain the attributes from this instance.
2) Property, the name of the property to be obtained.

Two Models

When Using JSP to develop a web application, you can choose between two architecture models: Model 1 and Model 2. The two models are described below.

Model 1

This model uses JSP + JavaBean to separate page display from business logic processing. jsp implements page display and JavaBean implements business logic. In this model, the JSP page responds to the request and returns the processing result to the user. All the data is processed by JavaBean, and JSP displays the page.

Although this model separates page display from business logic, you still need to write process control and call JavaBean code on the JSP page. When the business logic to be processed is very complex, will make the program very complex. This model cannot meet the needs of large-scale applications, but for small applications, this model is simple and can meet the needs well.

Model 2

In Model 1, the process control code and some logic processing code are embedded in the JSP page. If you extract the code into a single role (Controller ), this web structure becomes Model 2. Model 2 conforms to the MVC Chuck mode. The MVC model is also mentioned in servlet & JSP (2. In Model 2, the Controller role is implemented by Servlet, the view role is implemented by JSP pages, and the model role is implemented by JavaBean. The servlet accepts the request and is responsible for instantiating the JavaBean object to process the business logic. The JSP page is not prepared to carry the data's JavaBean object, and then distributes the request to the appropriate JSP page to generate a response. As a model, JavaBean has two main types: one is used to encapsulate business logic and the other is used to carry data.

Reprinted please indicate the source: http://blog.csdn.net/iAm333

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.