The use of JSP and JavaBean

Source: Internet
Author: User

JavaBean is a reusable, cross-platform software component. JavaBean can be divided into two kinds: one is the user interface (UI) JavaBean, and the other is that there is no user interface, mainly responsible for processing transactions (such as data operations, manipulating the database) JavaBean. The JSP typically accesses the latter type of JavaBean.

JSP and JavaBean the advantages of using the combination:

1. Separate the HTML from the Java program so that it is easy to maintain code. If all the program code is written in the JSP Web page, it will be cumbersome to use code, difficult to maintain.

2. Can reduce the development of JSP Web page personnel on the Java programming ability requirements.

3.JSP focuses on generating dynamic Web pages, and transaction processing is done by JavaBean, which makes full use of the reusability features of the JavaBean component to improve the efficiency of the development site.

a standard JavaBean. There are several features:

1.javaBean is a public class

2.javaBean has a construction method with no parameters

3.javaBean sets the property through the Setxxx method, and gets the property through the GetXXX method.

Package Com.anllin.bean;

Public class Person

{

Private String name;

Private int age;

Private String address;

Public String GetName ()

{

return name;

}

Public void setName (String name)

{

this. Name = name;

}

Public int getage ()

{

return age;

}

Public void setage (int age)

{

this. Age = age;

}

Public String getaddress ()

{

return address;

}

Public void setaddress (String address)

{

this. Address = address;

}

}

JSP Visit JavaBean The syntax

1. Import the JavaBean class

<%@ page import="Com.anllin.bean.Person" %>

2. Declaring JavaBean objects

<jsp:usebean id= "Person" class="Com.anllin.bean.Person"></jsp:useBean>

is actually equivalent to

<%

Person person = New person ();

%>

The ID indicates that an instance of a class is generated, that the generic ID cannot be duplicated, that it represents a different object, and that the same object is represented if the same is true, in which case the JSP will error.

3. Accessing the JavaBean property

<jsp:setproperty property="name" name="person" value="Jack"/>

<jsp:getproperty property="name" name="Person"/><br>

is actually equivalent to

<%

Person.setname ("Jack");

Person.getname ();

%>

A complete example:

<%@ page language="java" import="Com.anllin.bean.Person"pageencoding="UTF-8"%>

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

<title>my JSP ' javabean.jsp ' starting page</title>

<body>

<jsp:usebean id= "Person" class="Com.anllin.bean.Person"></jsp:useBean>

<jsp:setproperty property="name" name="person" value="Jack"/>

<jsp:setproperty property="age" name="person" value="/>"

<jsp:setproperty property="Address" name="person" value="Beijing"/>

<jsp:getproperty property="name" name="Person"/><br>

<jsp:getproperty property=' age ' name=' person '/><br>

<jsp:getproperty property="Address" name="Person"/><br>

</body>

Set Bean the property value

1. You can dynamically assign values to a bean's properties in <jsp:setProperty> by using the param parameter.

<jsp:setproperty property= "Age" name="person" param="Sage"/><br>

Sage represents a parameter name, not a property

In the browser address bar, enter

Http://localhost:8080/test/javabean.jsp?sage=30

You can get the value of the Age property 30

Javabean Range of Survival

The Scope property determines the extent of the existence of the JavaBean object. The optional values are:

1.page (default value)

2.request

3.session

4.application

<jsp:usebean id="person" class="Com.anllin.bean.Person" scope="page"></jsp: Usebean>

Javabean on the page when in range

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. The following two cases of JavaBean objects end their lifetimes:

1. The client requests access to the current JSP page through the <forward> tag to forward the request to another file

2. The client requests access to the current JSP page execution and sends back the response to the client.

Javabean in the request within range

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

1. Client requests access to the current JSP Web page.

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

3. When all JSP pages that share the same customer request are executed and respond to the client, the JavaBean object ends the life cycle.

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

jvabean.jsp

<jsp:usebean id="person" class="Com.anllin.bean.Person"scope="Request"></jsp :usebean>

<jsp:forward page="javabean2.jsp"></jsp:forward>

javabean2.jsp

<%@ page import="Com.anllin.bean.Person" %>

<%

Person person = (person) request.getattribute ("person");

Out.print (Person.getname ());

%>

Javabean in Session within range

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

The 2.javabean object is saved as an attribute in the HttpSession object, the property name is JavaBean ID, and the property value is 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:

<jsp:usebean id="person" class="Com.anllin.bean.Person"scope="session"></jsp :usebean>

Person person = (person) session.getattribute ("person");

Out.print (Person.getname ());

Javabean in Application within range

Once a 1.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 2.javabean object is stored as an attribute in the Application object, with the property name JavaBean ID, and the property value JavaBean object, except that the object can be directly referenced by the ID of the JavaBean. JavaBean objects can also be obtained through the JavaBean Application.getattribute () method, for example:

<jsp:usebean id="person" class="Com.anllin.bean.Person"scope= "application" >< /jsp:usebean>

Person person = (person) application.getattribute ("person");

Out.print (Person.getname ());

If there is a person class, there is a property name

Why should we provide a getname () method and a SetName () method instead of Getccnma?

Because only by adhering to this specification, you can invoke these two methods by reflection.

Focus:

Differentiate between page,request,session,application and their life cycles.

The use of 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.