Jsp: useBean usage

Source: Internet
Author: User

The simplest syntax for jsp: useBean actions is:

<Jsp: useBean id = "guessBiz" class = "biz. GuessBiz" scope = "session"/>

The meaning of this line of code is: "Create an instance of the class specified by the class property, and then bind it to the variable whose name is given by the id property ".

In this case, the jsp: useBean action creates an object instance only when the Bean with the same id and scope does not exist.

You can either directly provide a value through the value attribute of the jsp: setProperty action, or use the object variable named by the id attribute in the Scriptlet, you can call this object to explicitly modify its attributes (for example, <% guessBiz. setName ("name"); %>). You can also use the param attribute to declare that the Bean property value comes from the specified request parameter, the Bean attribute can also be listed to indicate that its value should come from the variable with the same name in the request parameter.

 

Attribute usage:
Id: Name the variable that references the Bean. If a Bean instance with the same id and scope can be found, the jsp: useBean Action uses an existing Bean instance instead of creating a new instance.
Class specifies the full package name of Bean.
Scope specifies the context in which the Bean is available. One of the following values can be taken down: page, request, session, and application.
The default value is page, indicating that the Bean is only available on the current page (stored in the PageContext of the current page ).
Request indicates that the Bean is valid in the current customer request (stored in the ServletRequest object ).
Session indicates that the Bean is valid for all pages in the current HttpSession.
Finally, if the value is application, the Bean is valid for all pages with the same ServletContext.
Scope is important because jsp: useBean instances new objects only when there are no objects with the same id and scope; if the existing objects with the same id and scope are used, the existing objects are used directly. In this case, any content between the start and end tags of jsp: useBean will be ignored.

Type specifies the type of the variable that references this object. It must be the name of the Bean class, the super class name, or one of the interfaces implemented by this class. Remember that the variable name is specified by the id attribute.
BeanName specifies the Bean name. If the type and beanName attributes are provided, the class attributes can be omitted.

 

Example: this is an example of a guess.

Input. jsp

Code

 

 

Web. xml

 

Code

 

 

ActionServlet. java

 

Code
Related Article

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.