Application of JavaBean Component in JSP

Source: Internet
Author: User

In JSP pages, the main structure in 3 can be used to build and operate the JavaBean Component.

I. Bean technology Basics

1) JSP: usebean

<JSP: usebean id = "beanname" class = "package. Class"/>

Build a new bean. For example:

<JSP: usebean id = "book1" class = "coreservlets. Book"/>

Equivalent to the following JSP statement ----

<% Coreservlets. Book book1 = new coreservlets. Book (); %>

2) JSP: getproperty

<JSP: getproperty name = "beanname" property = "propertyname"/>

Read or output bean attribute values. For example:

<JSP: getproperty name = "book1" property = "title"/>

Equivalent ---

<% = Book1.gettitle () %>

3) JSP: setproperty

<JSP: setproperty name = "beanname" property = "propertyname" value = "propertyvalue"/>

Modify bean attributes. For example:

<JSP: setproperty name = "book1" property = "title" value = "core servlets and assumerver pages"/>

Equivalent ----

<% Book1.settitle ("core servlets and assumerver pages"); %>

Using these three methods to operate beans is beneficial for web designers who are not familiar with Java programming.

Ii. Shared Bean

So far, for bean objects created with JSP: usebean, we treat it as a local variable in the _ jspservice method (_ jspservice is called by the servlet service method generated on the page ),That is, the bean is bound to a local variable (not shared ). However, this is not the only action. We can use the scope option. The system checks that the specified position is sufficient for the bean with the specified name. Only when the system cannot find the existing bean, to create a new bean. This behavior allows the servlet to process complex things in the following way: Create beans and store them in a location (request, painting, or servlet context) in three standard shared locations ), then, the request is forwarded to a page on several appropriate JSP pages to provide results suitable for the request data.

The scope attribute has four optional values: Page (default), request, session, and application.

1) <JSP: usebean... scope = "page"/>

Default value, which can be omitted. In addition to binding the bean object to a local variable during processing the current request, the bean object must also be placed in the pagecontex object. Because each page is different from the pagecontex object of each request, that is, the bean is not shared.


2) <JSP: usebean... scope = "request"/>

In addition to binding the bean object to a local variable during processing of the current request, it is also placed in the httpservletrequest object. Except for non-shared beans, when using the include and forward methods of JSP: Include, JSP: Forward, requestdispatcher, two JSP pages or JSP pages and Servlet will share the request object.

3) <JSP: usebean... scope = "session"/>

It indicates that during the processing of the current request, in addition to binding the bean object to a local variable, it must also be placed in the httpsession object associated with the current request. Therefore, this scope is used, JSP pages can easily perform painting tracking.

4) <JSP: usebean... scope = "application"/>

In addition to binding the bean object to a local variable during processing of the current request, it also needs to be placed in

In servletcontex, servletcontex is shared by multiple servlets and JSPs in the Web application.

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.