Introduction to JSP Action

Source: Internet
Author: User

Use of JSP Actions

JSP actions allows you to dynamically insert a file, reuse the JavaBeans component, forward to another page, or generate an HTML for the Java Plug-in. Available actions:
(1) JSP: include -- include a file when the page is requested.
(2) JSP: useBean -- locate or instantiate a JavaBean.
(3) JSP: setProperty -- set a JavaBean attribute.
(4) JSP: getProperty -- Insert the attributes of JavaBean to the output.
(5) JSP: forward -- enables the requester to go to a new page.
(6) JSP: plugin -- use the OBJECT or EMBED label to generate specific browser code for Java plugins.

1. JSP Action include

This action allows you to include some files on the page to be generated

Unlike include direve ve, this action contains the file only when the page is requested, while include directive contains the file when the JSP page is converted to servlet. To improve efficiency, include action makes a small sacrifice, that is, it does not allow the contained page to contain general JSP code, such as, HTTP header cannot be set), however, it has significant flexibility. The following JSP code inserts four different fragments into the following page. Each time the title changes, you only need to modify the four files without changing the main JSP page.

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 used 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. The original bean code is as follows:

2. JSP: detailed usage of useBean

The simplest way to use beans is:

To load beans, JSP: setProperty and JSP: getProperty must be used to modify and retrieve bean attributes. There are also two options. First, you can use the container format

It should be noted that the Body part should be executed only when the bean is first instantiated, rather than every time it is found and used. Beans can be shared. Therefore, not all JSP: useBean statements generate a new bean instance. Besides id or class, you can use the following attributes: scope, type, and beanName. Specifies the frontend and backend relationships on which beans can be used. There are four possible values: page, request, session, and application. The default value is page, indicating that bean can only be saved in the current PageContext on the current page ). A value in the request indicates that the bean is only used for the requests of the current client to be stored in the ServletRequest object ). The Session value indicates that the object is available to all pages during the lifecycle of the current HttpSession. Finally, the value of application indicates that the object can be used for all pages that share ServletsContext. Use JSP: useBean to create a new bean only when there is no bean with the same id and scope. If so, use it and ignore the code starting and ending with the JSP: useBean flag.

Type indicates the type of the variable that will point to the object. This must match the class name, a superclass, or an interface that implements the class. Remember, the variable name is specified by the id attribute.

BeanName gives the bean a name, which should be provided in the Beans instantiation method. It allows you to give a type and a beanName, and omit class attributes.

3. JSP Action setProperty

This is a required attribute. It specifies which bean property will be set. JSP: usebean must appear before JSP: setProperty.

Property is a required property. Which attribute will be set. However, there is a special case: If "*" is used as the value, it means that all request parameters whose names match the bean property will be passed to the corresponding Property setting method.

Value is an optional attribute. It specifies the value of the property to be set. The value of a string is automatically converted to numbers, boolean, Boolean, byte, Byte, char, and Character by using the standard valueOf method of the corresponding object or package. For example, the value of the boolean OR Boolean attribute "true" is converted using the Boolean. valueOf method, and the value of an int or Integer attribute "42" is converted using Integer. valueOf. You cannot use both the value and param attributes, but neither of them is allowed.

Param is an optional attribute. It specifies the request parameters that the bean attributes should inherit. If the current request does not have such a parameter, nothing will be done: the system does not pass null to the attribute setting method. Therefore, you can use the bean default value. For example, the following program executes "set the numberOfItems attribute to the value of any numItems request parameter. If such a request parameter exists, otherwise nothing will be done ."

If you set both value and param by default, this is the same as setting param name to bean property name. You can set the value of name to "*" and omit value and param to automatically use the request attribute corresponding to the bean attribute. In this case, the server repeatedly searches for available attributes and request parameters to match those with the same name.

  1. Analysis of JSP server
  2. Monitor JVM available memory in JSP
  3. JSP Init and Destory functions improve work efficiency
  4. Basic JSP compiler syntax
  5. Analysis on compiling JSP tags

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.