JSP and JavaBean

Source: Internet
Author: User

The concept of JavaBean

JavaBean is a reusable, cross-platform software component. JavaBean can be divided into two types: one is the JavaBean of the user interface (Ui,user Interface), and the other is that there is no user interface, which is primarily responsible for handling transactions (such as data manipulation, manipulating the database) JavaBean. The JSP typically accesses the latter type of JavaBean.

Advantages of JavaBean

? make HTML separate from the Java program, which makes it easier to maintain code. If all the program code is written in the JSP Web page, it will make the code cumbersome, difficult to maintain

? can reduce the need for Java programming capabilities for developers of JSP web pages

? JSP focuses on generating dynamic Web pages, transaction processing is done by JavaBean, which can take full advantage of the reusability characteristics of JavaBean components, improve the efficiency of the development of the site to write a JavaBean

A standard javabean writing rule is as follows

1.JavaBean is a common (public) class

2.JavaBean has a construction method with no parameters

3.JavaBean through the Setxxx method to set properties, through the GetXXX method to get properties, you can use the IDE tool generate Getters and setters automatically generated.

For example: MyEclipse can right-click the edit area->source->generate Getters and Setters->select All->ok to automatically generate getter and for member variables Setter method

The following is an example of a javabean:

Package Com.bean;public class Person {    private int idcard;    private String name;    public int Getidcard () {        return idcard;    }    public void Setidcard (int idcard) {        idcard = Idcard;    }    Public String GetName () {        return name;    }    public void SetName (String name) {        this.name = name;    }}
Importing the JavaBean class on a JSP page

Import the JavaBean class through the <%@ page import> directive, for example:

Declaring JavaBean objects

The <jsp:useBean> tag is used to declare JavaBean objects, such as:

1.id is the name of the instance of the person class, so you can see it in a different way.

2.scope is a scoped, built-in object with a value of JSP, see JSP built-in objects in detail. The scope is small to large as follows:

>page (default value)

Each time a customer requests access to a JSP page, a JavaBean object is created. The valid range of the JavaBean object is the current JSP Web page that the customer is requesting access to. JavaBean objects end their lifetimes in the following two scenarios:

– The current JSP page that the customer requested access to forward the request to another file via the <forward> tag

– The current JSP page to which the customer requested access is completed and the response is sent back to the client.

>request

Each time a customer requests access to a JSP page, a new JavaBean object is created. The valid range of JavaBean objects is:

– The current JSP page that the customer requested access to

– Share the same customer request page with the current JSP page, which is the <%@ include> directive in the current JSP Web page and other JSP files included with the <forward> tag

– The JavaBean object ends the life cycle when all JSP pages that share the same customer request are executed and the response is sent back to the client.

JavaBean objects can be referenced directly in <JSP:getProperty> and <jsp:setProperty> through the ID of JavaBean, for example:

The JavaBean object is saved as an attribute in the HttpRequest object, the property name is JavaBean ID, the property value is a JavaBean object, and the Httprequest.getattribute object can also be obtained through the JavaBean () method For example:

>session

After the JavaBean object is created, it exists throughout the lifetime of the session, and the JSP file in the same session shares the JavaBean object.

JavaBean objects can be referenced directly in <JSP:getProperty> and <jsp:setProperty> through the ID of JavaBean, for example:

<jsp:getproperty property= "Idcard" name= "Person2"/>

The JavaBean object is saved as an attribute in the HttpSession object, with the property name JavaBean ID, and the property value is the JavaBean object. In addition to referencing the JavaBean object directly through the ID of the JavaBean, you can also obtain the JavaBean object through the Httpsession.getattribute () method, for example:

>application

Once the JavaBean object is created, it exists throughout the life of the Web app, and all JSP files in the Web app can share the same JavaBean object.

The JavaBean object is saved as an attribute in the Application object, the ID of the property name is JavaBean, and the property value is the JavaBean object, except that the JavaBean object can be directly referenced by the JavaBean ID. You can also get JavaBean objects by using the Application.getattribute () method, for example:

Accessing the JavaBean Property

1.JSP provides a label to access the JavaBean property, and if you want to export a property of JavaBean to a Web page, you can use the <jsp:getProperty> tag, for example:

Property:

Name is the name of the bean, specified by the ID in <jsp:useBean>.

The attribute name of the bean specified by the property.

2. If you want to assign a value to a property of JavaBean, you can use the <jsp:setProperty> tag:

The id attribute in the name:<jsp:usebean> tag

The properties of the Property:bean instance.

The value of the Value:bean instance property.

Param: Form Parameters

Grammar:

1) Set the Bean's property to a string or an expression. If it is a string, the string is automatically converted to the type of the Bean's property. If it is an expression, then the type of the value must be the same as the type of the Bean property.

2) Set the value of the corresponding property of the bean through the parameters of the form.

The attributes in the form and the JavaBean properties should have the same name.

Note that the Chinese parameter of the form is garbled and can be added at the top of the JSP page

JSP and JavaBean

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.