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: