Use JavaBean instance in JSP to introduce _JSP programming

Source: Internet
Author: User

The code in the test.jsp is shown below

Copy Code code as follows:

<%@ page language = "Java import=" test. Simplebean; "%>

Testing </title> using Java beans in <title>jsp
<body>
<jsp:usebean id= "test" class= "test." Simplebean "/>
<jsp:setproperty name= "test" property= "name" value= "Hello jsp"/>
<jsp:setproperty name= test property= message value= jsp/>
<jsp:setproperty name= test property= value= jsp/>

<p> News:
<jsp:getproperty name= "test" property= "name"/>
<br>
<jsp:getproperty name= "test" property= "message"/>
<br>
<jsp:getproperty name= "test" property= "coon"/>
</body>

<jsp:usebean id= "Test"/>: Represents an instance that defines a Java bean, the name of the instance is test, and the name of the class is Simlebean.
<jsp:setproperty name= "test" property= "name" value= "Hello jsp"/>: Represents the property name of the set instance test, whose value is "Hello jsp".
<jsp:setproperty name= "test" property= "message" value= "Fuck jsp"/>: Represents a property message setting instance test with the value "Fuck jsp".
<jsp:getproperty name= "test" property= "name"/>: Represents the value of the property name of the output instance test on the page.
<jsp:getproperty name= "test" property= "message"/>: The value of the property message that shows the output instance test on the page.

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

Copy Code code 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, <jsp:useBean>

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).
<jsp:useBean> actions are used to find or instantiate a 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:

Copy Code code as follows:

<jsp:usebean id= "Beaninstancename"
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, <jsp:setProperty>

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.
Suppose 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),<jsp:setproperty> This is the way the action is used.

The syntax is as follows:

Copy Code code as follows:

<jsp:setproperty name= "Beaninstancename"
property= "*" | property = "PropertyName"
Value= "Specific Value"
/>

Property:

1) name= "Beaninstancename"
This attribute is required to indicate which bean instance is performing the following action, and this value must correspond to the ID defined in action <jsp:useBean>, 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, <jsp:getProperty>

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 <jsp:getProperty> uses it.

The syntax is as follows:

Copy Code code as follows:

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

Property:

1) name= "Beaninstancename"
This attribute is required to indicate which bean instance is performing the following action, and this value must correspond to the ID defined in action <jsp:useBean>, 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.