JavaBean Usage Analysis for JSP Learning

  • 2021-06-28 13:37:08
  • OfStack

This article provides an example of JavaBean usage learned by JSP.Share it for your reference.Specifically as follows:

JavaBean is an Java class that conforms to certain naming methods and design specifications
JavaBean can be used to perform complex computing tasks, encapsulate transaction logic, databases, etc.
JavaBean is divided into visual JavaBean, such as buttons, text boxes, list boxes, etc. and non-visual JavaBean, such as operations, database connections, etc.

Using JavaBean:

<jsp:useBean id="name" scope="page|request|session|application" typeSpec="typename"/>

typeSpec4 values:

class="classname" class represents the class path and class name
class="classname" type="typename" type represents the type of class, which can be this class, parent class, or interface
beanName="beanName" type="typename". beanName stands for the name of JavaBean and is initialized by the java.beans.Beans.instantiate() method in the form of a.b.c
type="typename"

< jsp:setProperty > Property value used to set JavaBean:


<jsp:setProperty name="beanname" property="*">
<jsp:setProperty name="beanname" property="propertyname">
<jsp:setProperty name="beanname" property="propertyname" param="paramname">
<jsp:setProperty name="beanname" property="propertyname" value="beanvalue">

beanname represents the JavaBean instance name, using < jsp:usebean > Introduced
propertyname represents the property name of JavaBean
paramname Specifies the parameter name in the request object
beanvalue is used to set the property value of JavaBean
< jsp:getProperty > Attribute value used to get JavaBean:
< jsp:getProperty name="beanname" property="propertyname" >
Equivalent to getXX function in JavaBean
< jsp:setProperty name="splBean" property="id" value="${param.id}"/ >

Period of declaration for JavaBean:

page Range- > The life cycle can only be accessed in one page, only in one page. When the page is refreshed, the original JavaBean instance will be deleted, resulting in a new JavaBean instance.
request Scope- > It has a lot to do with the request object, access extents include action elements in addition to the entire page < jsp:include > and < jsp:forward > Included pages, that is, both the original page and the included page, can access the originally generated JavaBean instance
session scope: The life cycle is in one connection and the JavaBean can be accessed in one connection (when a user accesses a Web page using a browser, a connection is made once and an session object representing the connection is created).
application Scope: The longest life cycle, JavaBean always runs in the server as long as the Web server does not restart, so the JavaBean instance can be used by any page.

I hope that the description in this paper will be helpful to everyone's JSP program design.


Related articles: