Using JavaBean instance in JSP

Source: Internet
Author: User
Tags valid

This article mainly introduces the use of JavaBean in JSP examples, while introducing the relevant attributes, the need for friends can refer to the

The code in the test.jsp is shown below

The code is as follows:

News:

: Represents an instance that defines a Java bean, the name of the instance is test, and the name of the class is Simlebean.

: Represents the property name of the set instance test, whose value is "Hello JSP".

: Represents a property message that sets the instance test with the value "Fuck JSP."

: Represents the value of the property name of the output instance test in the page.

: The value of the property message that shows the output instance test in the page.

The code for the Java bean used in the test.jsp file is Simplebean.java as follows:

The code is as follows:

Package test;

public class Simplebean {

private String name;

Private String message;

Private String Conn;

Public String GetName () {

return this.name;

}

public void SetName (String name) {

THIS.name = name;

}

Public String GetMessage ()

{

return this.message;

}

public void Setmessage (String message)

{

this.message = message;

}

Public String Getcoon ()

{

return this.conn;

}

public void Setcoon (String coon)

{

This.conn = this.message+ "" +this.name;

}

}

1,

JavaBean is a special type of Java class, and the main difference from a normal Java class is that it contains two special methods: Set ...  (Method of setting property), get ... (The method that takes the property value).

Action is used to find or instantiate the JavaBeans component used by a JSP page.

Logic control, database operations in the program can be placed in the JavaBeans component, and then called in the JSP file, which can increase the clarity of the program and the reusability of the program.

The syntax is as follows:

The code is as follows:

Scope= "page | Request | Session | Application "

Class= "PACKAGE.CLAAA"

Type= "PACKAGE.CLAAA"

Other elements

/>

Property:

1) id= "Beaninstancename"

The variable used to bind the bean instance.

2) class|type= "PACKAGE.CLAAA"

Indicates which class The bean is instantiated specifically for.

3) scope= "page | Request | Session | Application "

Represents the valid range for this bean.

Page: The valid range for an instance is the current page.

Request: The valid scope of an instance is within the lifecycle of a single customer request.

Session: The valid scope of an instance is within the life cycle of the entire user conversation.

Application: The valid scope of an instance is within the lifecycle of an application.

2,

The meaning of this action is to use the corresponding set () method in the bean to set the value of one or more properties, either explicitly by the Value property or by taking advantage of the corresponding parameters in the request object.

Assuming that a bean has a string type of property myproperty that can be used by a JSP file, it must have a public method Setmyproperty (string value), which is the method used by the action.

The syntax is as follows:

The code is as follows:

<jsp:setproperty name= "Beaninstancename"

property= "*" | property = "PropertyName"

Value= "Specific Value"

/>

property= "*" | property = "PropertyName"

Value= "Specific Value"

/>

Property:

1) name= "Beaninstancename"

This attribute is required to indicate to which bean instance the following action is performed, and this value must correspond to the ID defined in the action, including the case must be consistent.

2) property= "*" | property = "PropertyName"

This property is required to indicate which property to set. If the value of the property is "*", the entire value that the user enters in the visible JSP page is stored in the matching bean property. The matching method is that the Bean's property name must be the same as the name of the input box.

3) value= "Specific value"

It is used to specify the value of the Bean's properties.

3,

This action extracts the value of the specified bean property, converts it to a string, and then outputs it. The action is actually a call to the Bean's Get () method.

One of the beans mentioned above has a string type of property myproperty that can be used by the JSP file, and it must have a public method Getmyproperty () that returns the type string. That's the way it is used.

The syntax is as follows:

The code is as follows:

<jsp:getproperty name= "Beaninstancename" property= "PropertyName"/>

Property:

1) name= "Beaninstancename"

This attribute is required to indicate to which bean instance the following action is performed, and this value must correspond to the ID defined in the action, including the case must be consistent.

2) property= "*" | property = "PropertyName"

This property is required to indicate which property to get.

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.