Jsp: useBeanAction usage 1. Syntax: jsp: useBeanrequest | session | application {typepackage. class | beanName {package. class | % expression %} Use of typepackag jsp: useBean Action
I. syntax:
Id = "beanInstanceName"
Scope = "page | request | session | application"
{Class = "package. class" |
Type = "package. class" |
Class = "package. class" type = "package. class" |
BeanName = "{package. class | <% = expression %>}" type = "package. class"
}
{/> |
> Other elements
}
This action enables you to load a JavaBean into a JSP page. This is a very useful capability because it enables you to use reusable JAVA classes without sacrificing performance. The simplest syntax is to specify a bean:
This usually means that "the object of a class is instantiated by specifying a class and binding it to a variable named by id ". However, as we can see, you can specify a scope attribute so that the bean is not only associated with the current page. In this case, it is very useful to obtain a reference to an existing bean. In addition, a new bean is created only when there is no bean with the same id and scope. Now that you have a bean, you can modify it through jsp: setProperty, or use the name specified by the id to use scriptlet or call the method explicitly. When you say "this bean has an attribute of the X type called foo", you really mean "this class has a method called getFoo, which returns a value of the X type, another method is setFoo, which takes X as the parameter." This jsp: setProperty action will be detailed in the next unit, but now you can give a clear value and an attribute to indicate that this value is inherited from the request parameter, you can also just list attributes to indicate that this value should be inherited from parameters with the same name as the attribute name. You can obtain an existing jsp expression or scriptlet attribute by calling the applicable getXxx method or, more commonly, using JSP: getProperty action.
Note that the class specified for bean must be under the class path of the server rule, rather than the path of the class automatically loaded when the change occurs. For example, on Java Web Server, it and its classes must be in the class directory or in a jar file under the lib Directory, rather than in the servlets directory.
Let's take a very simple example. it loads a bean and sets/gets a simple string parameter.
BeanTest. jsp
Reusing JavaBeans in JSP
Property = "message"
Value = "Hello WWW"/>
Message: