The JavaBean of JSP

Source: Internet
Author: User

First, the definition

1 JavaBean is a Java class that follows a specific notation   , and it usually has the following characteristics:

    • This Java class must have an argument-free constructor
    • property must be privatized.
    • The properties of privatization must be exposed to other programs by means of the public type Getset () method, and the name of the method must follow certain naming conventions.

Second, use JavaBean in Java

JSP technology provides three action elements about the JavaBean component, the JSP tags, which are:

    • <jsp:useBean> Tags: used to find or instantiate a JavaBean component in a JSP page.
    • <jsp:setProperty> Tags: used to set the properties of a JavaBean component in a JSP page.
    • <jsp:getProperty> Tags: used to get the properties of a JavaBean component in a JSP page.

  2.1 <jsp:useBean> Tags  

The <jsp:useBean> tag is used to find the JavaBean object of the specified name within the specified domain scope, and if present, returns a reference to the JavaBean object directly. Instantiates a new JavaBean object if it does not exist and stores it in the specified domain scope with the specified name.
Common syntax:
<jsp:usebean id= "Beanname" class= "Package.class " scope= "page|request|session|application" />
TheIDproperty is used to specify the reference name of the JavaBean instance object and the name it stores in the domain scope.
The "class" attribute is used to specify the full class name of the JavaBean (that is, it must have a package name).
The scopeproperty specifies the domain scope stored by the JavaBean instance object, which can only be one of four values, such as page, request, session, and application, and its default value is page.

1<jsp:usebean id="my" class="Jspprojec. person"Scope="Request"></jsp:useBean>2<%3My.setname ("John Doe");4My.setsex ('male');5Session.setattribute ("my", my);//references are saved to session6 7%>8 9<jsp:forward page="index1.jsp"></jsp:forward>
index1.jsp:
1 <body>    2     <%3person p= (person     ) Session.getattribute (" my ");  // gets the reference, at which point the reference is // required casts for type Object 4      out . Print (P.getname ()); 5      out . Print (P.getsex ()); 6     %>7 </body>        

  

  2.2 <jsp:setProperty> tags

The <jsp:setProperty> tab is used to set and access the properties of the JavaBean object.

    Syntax Format one:
    <jsp:setproperty name= "Beanname" property= "PropertyName" value= "string string"/>

  Syntax format two:
    <jsp:setproperty name= "Beanname" property= "PropertyName" value= "<%= expression%>"/>

  Syntax format three:
    <jsp:setproperty name= "Beanname" property= "PropertyName" param= "ParameterName"/>

  Syntax format four:
    <jsp:setproperty name= "Beanname" property= "*"/>
The Name property is used to specify the names of the JavaBean objects.
The property attribute is used to specify the attribute name of the JavaBean instance object.
The Value property is used to specify values for a property of the JavaBean object, which can be either a string or an expression. As a string, the value is automatically converted to the corresponding type of the JavaBean property, and if the value of value is an expression, the expression must evaluate to the same type as the JavaBean property you want to set.
The Param property is used to set a property value of an JavaBean instance object to a request parameter value, which is also automatically converted to the type of the JavaBean property to set.

  2.2.1 Setting the property value of the person object using the Jsp:setproperty tag

1<jsp:usebean id="my" class="Jspprojec. person"Scope="Request"></jsp:useBean>2 3<jsp:setproperty property="name"Name="my"Value="Harry"/>4<jsp:setproperty property="Sex"Name="my"Value="N"/>5 6<%7Session.setattribute ("my", my);8%>9 Ten<jsp:forward page="index1.jsp"></jsp:forward>

Create an object for person my , with the "jsp:setproperty" tag to the object name="my" property= the "name" property setting value is Value=" Harry ";

    2.2.2 Assigning a value to a bean's properties using the request parameter

<jsp:usebean id= "My" class= "jspprojec. Person "scope=" request "></jsp:useBean>

<jsp:setproperty property= "name" Name= "my" param= "name"/>

<%
Session.setattribute ("My", my);
%>

<jsp:forward page= "index1.jsp" ></jsp:forward>
index1.jsp:
1 <%2     person p= (person) session.getattribute ("my"); 3      out . Print (P.getname ()); 4      out . Print (P.getsex ()); 5     %>

When we enter HTTP://LOCALHOST:8080/JSPPROJEC/INDEX.JSP?NAME=ZJAMGSM in the browser, we get the name value assigned to the Name property of the My object.

    2.2.3 uses all request parameters to assign a value to the Bean's properties

1<jsp:usebean id="my" class="Jspprojec. person"Scope="Request"></jsp:useBean>2 3 <jsp:setproperty property= "*" name= "my"/>4 5<%6Session.setattribute ("my", my);7%>8 9<jsp:forward page="index1.jsp"></jsp:forward>
index1.jsp:
1 <%2     person p= (person) session.getattribute ("my"); 3      out . Print (P.getname ()); 4      out . Print (P.getsex ()); 5     %>

When we enter http://localhost:8080/jspprojec/index.jsp?name=zjamgsm&sex=f in the browser, we get all the properties from the Address bar.

  2.3 <jsp:getProperty> Tags 

The <jsp:getProperty> tag is used to read the properties of the JavaBean object, which is called the Getter method of the JavaBean object, and then the Read property value is converted into a string and inserted into the response body of the output.
Grammar:
<jsp:getproperty name= "Beaninstancename" property= "PropertyName"/>
The Name property is used to specify the names of the JavaBean instance objects whose values should be the same as the ID property values of the <jsp:useBean> tag.
The property attribute is used to specify the attribute name of the JavaBean instance object.
If the value of one of the properties of an JavaBean instance object is null, the result of using the <jsp:getProperty> label to output the property will be a string with the content "null".

1<jsp:usebean id="my" class="Jspprojec. person"Scope="Request"></jsp:useBean>2<jsp:setproperty property="name"Name="my"Value="Zhangsanfeng"/>3<jsp:getproperty property="name"Name="my"/>

The JavaBean of JSP

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.