A simple example of JSP calling JavaBean

Source: Internet
Author: User

1<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "2pageencoding= "UTF-8"%>3<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >456<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">7<title> main Functions </title>89<body>Ten<!--import JavaBean class-- One<%@ pageImport= "Com.weeklyreport.www.TsetBean"%> A -<jsp:include page= "date.jsp" flush= "Flush" ></jsp:include> -<!--declaring JavaBean objects-- the<jsp:usebean id= "Test"class= "Com.weeklyreport.www.TsetBean" ></jsp:useBean> -<!--Access JavaBean Properties-- -<jsp:setproperty property= "message" name= "test" value= "Chen Jiena"/> -  +<p> Output Information </p> -<jsp:getproperty name= "test" property= "message"/> +</body> A

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.

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.

A simple example of JSP calling 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.