JSP details-JavaBean technology

Source: Internet
Author: User

JSP details-JavaBean technology
Application of JavaBean

JavaBean is a reusable component written in Java. It can be used in many layers of the system, such as PO, VO, DTO, and POJO. It is widely used.

1. Get attributes of JavaBean

In JavaBean, to prevent external calls to the JavaBean attribute directly, the attribute in JavaBean is usually set to private and the public method is provided to access it.

Example:

Display attributes of JavaBean on the JSP page

(1) create a class named Product, which is a JavaBean that encapsulates the Product attributes

Package com. zgy. javabean;

Public class Product

{

Private String name = "piano"; // Product name

Private float price = 10000; // price

Private int count = 500; // quantity

Private String producePlace = "China"; // Origin

Public String getName ()

{

Return name;

}

Public void setName (String name)

{

This. name = name;

}

Public float getPrice ()

{

Return price;

}

Public void setPrice (float price)

{

This. price = price;

}

Public int getCount ()

{

Return count;

}

Public void setCount (int count)

{

This. count = count;

}

Public String getProducePlace ()

{

Return producePlace;

}

Public void setProducePlace (String producePlace)

{

This. producePlace = producePlace;

}

}

(2) obtain the attributes of JavaBean on the JSP page.

<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>

<%

String path = request. getContextPath ();

String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";

%>

<% @ Page import = "com. zgy. javabean. Product" %>

Product Attribute

  • Product Name:

  • Product Price:

  • Item quantity:

  • Product origin:

2. assign values to attributes of JavaBean

Example:

Or the Product class just now. Now, create SetJavaBeanProperty. jsp, instantiate the Product object on this page, and assign values to its attributes.

<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>

<%

String path = request. getContextPath ();

String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";

%>

My JSP 'setananproperty. jsp 'starting page

  • Product Name:

  • Product Price:

  • Item quantity:

  • Product origin:

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.