I. Advantages of JavaBean:
**************************************** **************************************** *****
A. No Java syntax is required.
B. Easier object sharing
C. The request parameters and object attributes can be easily matched.
**************************************** **************************************** *****
Ii. Features of JavaBean:
**************************************** **************************************** *****
A. the Bean class must have a zero-parameter (default) constructor. When a JSP element creates a bean, it calls the default constructor. In actual applications, servlets often create beans, and JSP pages only query data from them.
B. bean classes should not have public instance variables (fields): To become beans that can be accessed by JSP, the corresponding classes should use the access method) replace direct access to instance variables.
C. continuous values should be accessed through the getxxx and setxxx Methods: although we can use any method of JSP script or expression encoding class, standard JSP for bean access can only use methods that follow the naming conventions of getxxx/setxxx or isxxx/setxxx.
**************************************** **************************************** *****
Iii. Construction of JavaBean:
**************************************** **************************************** *****
A. <JSP: usebean id = "name" class = "package. Class"/>
This statement indicates: instantiate the class specified by the class and bind the instantiated object to the variable in _ jspservice. The variable name is specified by the ID.
B. Be sure to use the fully qualified class name, including the Class Name of the package name. Whether or not you use <% @ page import... %> This requirement must be met when a package is introduced.
C. jsp: usebean action rule: new objects are added only when beans with the same ID and scope do not exist.
**************************************** **************************************** *****
Iv. Bean installation:
**************************************** **************************************** *****
A. The correct position of a single Bean class is WEB-INF/classes/subdirectorymatchingpackagename
B. the JAR file containing the Bean class should be placed in the WEB-INF/lib directory.
**************************************** **************************************** *****
V. jsp: Use of the beanname and type options in usebean:
**************************************** **************************************** *****
A. beanname: you can point to a class or specify a file containing the serialized bean object.
B. Type: When you want to declare a variable of the type of the actual Bean class, or the bean implementation interface. The Type attribute should be used to control such declarations. In this case, the declared type is the parent class or interface of the type specified in the class, that is, a parent class Object-polymorphism is initialized using the subclass constructor. For example:
<JSP: usebean id = "thread1" class = "mypackage. myclass"
Type = "Java. Lang. runnable">
**************************************** **************************************** *****
Vi. jsp: usebean syntax rules:
**************************************** **************************************** *****
A. the attribute name is case sensitive.
B. single quotation marks and double quotation marks can be used (but one of them must be used)
C. mark the end of the tag as/>, not just>
**************************************** **************************************** *****
VII. Set the JavaBean attribute:
**************************************** **************************************** *****
A. <JSP: setproperty name = "beanname" property = "beanproperty" value = "beanvalue"/>
B. jsp: The Value Attribute of setproperty allows the expression of the value during the request.
**************************************** **************************************** *****
8. associate a single attribute with input parameters:
**************************************** **************************************** *****
A. you can use Param to specify an input parameter instead of the Value Attribute. The Value Field of the specified request parameter is used as the bean attribute value, type conversion from string to basic type and encapsulation class is automatically executed.
B. If no parameter is specified in the request, no action is taken (the system does not pass null to the associated attribute). For example:
<JSP: setproperty name = "bean1" property = "property1" Param = "property1">
C. If the request parameter name is the same as the bean attribute name, you can further simplify this code. In this case, Param can be omitted.
**************************************** **************************************** *****
9. Associate all attributes with request parameters:
**************************************** **************************************** *****
A. jsp allows all attributes to be associated with Request Parameters of the same name. All we need to do is use "*" as the value of the property parameter. Example: <JSP: setproperty name = "entry" property = "*"/>
B. The system looks for the matching bean attributes starting from the request parameters, instead of using the opposite method. Therefore, no action is taken for attributes without matching request parameters. This behavior means that the form bean does not have to be filled at once. On the contrary, partial attributes of the bean can be filled at one submission, and other attributes can be filled in another form. And so on. To use this function, you need to share the bean in multiple pages.
**************************************** **************************************** *****
10. Note:
**************************************** **************************************** *****
A. When the input parameter is missing, no action is taken: NULL is not provided as the attribute value. Therefore, when designing beans
You need to set a recognizable default value for it to determine whether a property has been modified.
B. automatic type conversion cannot prevent illegal values like manual type conversion: when using automatic type conversion, you should consider using the try/catch block on the error handling page or display to process abnormal data
C. Bean attribute names and request parameters are case sensitive: the attribute names and request parameter names must be exactly matched.
**************************************** **************************************** *****
11. Shared Bean
**************************************** **************************************** *****
When using scope, the system first checks whether the bean with the specified name exists at the specified position. Only when the system cannot find the existing bean will the system create a new bean. Scope attribute with four optional values: Page, request, session, Application
**************************************** **************************************** *****
12. Bean scpoe = "page ":
**************************************** **************************************** *****
A. This value indicates that, in addition to binding the bean object to a local variable during processing of the current request, it should also be placed in the pagecontext object.
B. Store this object here. servlet can access it by calling the getattribute method of the predefined variable pagecontext. Because each page and each request has a different pagecontext object,
C. All scope = "page" indicates that the bean is not shared, that is, a new bean is created for each request.
**************************************** **************************************** *****
13. Bean scope = "request ":
**************************************** **************************************** *****
A. This value indicates that, in addition to binding the bean object to a local variable during processing of the current request, it should also be placed in
Httpservletrequest object, which can be accessed through the getattribute method.
B. When using the include or forward methods of JSP: Include, JSP: forward, and requestdispatcher, two JSP pages, JSP pages, and Servlet will share the request object. The bean with scope = "page" cannot share the request object.
**************************************** **************************************** *****
14. Bean scope = "session ":
**************************************** **************************************** *****
A. this value indicates that, in addition to binding the bean object to a local variable, it is also stored in the httpsession object associated with the current request. We can use getattribute to obtain the object stored in httpsession.
B. the bean is stored in the httpsession object. If the session does not expire (such as timeout), the bean attribute can be sustained across requests.
**************************************** **************************************** *****
15. Bean scope = "application ":
**************************************** **************************************** *****
A. This value indicates that, in addition to binding the bean object to a local variable, the bean object must be stored in the servletcontext and obtained by predefining the application variable or calling getservletcontext.
B. servletcontext is shared by multiple servlets and JSP pages in the Web application. The value in servletcontext can be obtained using the getattribute method.
**************************************** **************************************** *****
16. Construct bean based on conditions:
**************************************** **************************************** *****
A. Construct bean according to the condition: <JSP: usebean…> Statements </jsp: usebean>
B. jsp: The statement between the start tag and end tag of usebean is executed only when a new bean is created. If an existing bean is used, it is not executed. Since JSP: usebean calls the default (zero parameter) constructor, we often need to modify its attributes after Bean is created. To simulate the constructor, you should execute these modifications when creating the bean for the first time, instead of accessing the existing (or updated) bean.
C. Multiple pages can contain JSP: setproperty statements between the start tag and end tag of JSP: usebean. These statements are executed only on the first page accessed.
**************************************** **************************************** *****