JSP Actions use the XML syntax format tag to control the behavior of the Servlet Engine. Using JSP actions can be dynamically inserted into files, reuse the JavaBean components, redirect the user to another page, generate HTML code for the Java plugin. JSP actions include: JSP: include: Introducing a file when the page is requested. JSP: Usebean: Find or instantiate a JavaBean. JSP: setProperty: Sets the properties of JavaBean. JSP: getProperty: Outputs the properties of a JavaBean. JSP: Forward: Turn the request to a new page. JSP: Plugin: Generate Object or Embed tags based on the browser type. 13.1 JSP: Include Action This action Inserts the specified file into the page being generated. Its syntax is as follows: I have already introduced the include directive, which is introduced when the JSP file is converted to servlet, and the JSP: include the number of thections here is different. The time inserted into the file is when the page is requested. JSP: The file introduction time of the incrude action determines its efficiency to be slightly worse, and the reference file cannot contain some JSP code (for example, the HTTP header can not be set), but its flexibility is much better. For example, the following JSP page plugs 4 news summary into a "what's new?" Page.
When you change the news summary, you only need to change these four files, and the primary JSP page can not be modified: whatsnew.jsp
here is a summary of oes Most Recent News Stories:
ol > Body> html> 13.2 JSP: UseBean action JSP: UseBean action to load a JavaBean that will be used in the JSP page. This feature is very useful because it allows us to play both the Java component reuse, and avoid the loss of JSP distinguish between servlets. JSP: The simple syntax of UseBean action is: Create an instance of the class specified by the Class property, then put it Binds to its name by the number of variables given by the id attribute. " However, just as you will see, define a scope property allows the bean to associate more pages. At this point, JSP: UseBean action only creates a new object instance without the same ID and Scope, and it is necessary to get a reference to the existing bean.
After getting a bean instance, the attribute to modify the bean can be performed by JSP: SetProperty action, or the object variable named ID attribute can be used in the Scriptlet, and the properties are explicitly modified by calling the object. This makes us remember that when we say "a bean has a type X" foo ", it means" this class has a getfoo method with a return value type X, and there is a setfoo method to X-type value Parametric ". Details of the JSP: SetProperty action are discussed later. But now you must know, we can provide one value directly through the value attribute of JSP: setProperty action, or by declare the property value from the specified request parameter through the PARAM property, you can also list the BEAN property indicating that it should be The same name variable from the request parameter. Reading the bean property in the JSP expression or Scriptlet is implemented by calling the corresponding getxxx method, or more generally, using the JSP: getProperty action. Note that class files containing beans should be placed in the server formally stored in the Directory of the Java class, rather than retaining the directory of the class that can be loaded automatically after the modification. For example, for Java Web Server, the classes used by Beans and all beans should be placed in the classs directory, or when the package into the JAR file, put the lib directory, but should not put it under servlets. Here is a very simple example, its function is to load a bean, then set / read its Message property. Beantest.jsp
Reusing JavaBeans in jsp title> head>
Reusing JavaBeans in JSP Table> center>
Message: i > H1> body> html> SimpleBean.java beantest page is used in SimpleBean.
SimpleBean code is as follows: package hall; public class SimpleBean {private String message = "No message specified"; public String getMessage () {return (message);} public void setMessage (String message) {this.message = message;}} 13.3 About JSP: Further Description of UseBean Use the easiest way to use Bean to load bean: then by JSP: SetProperty and JSP: GetProperty Modify and extract the properties of the bean. However, there must be two points. First, we can also instantiate bean: body > body jsp: usebean>, only when the first instantiation bean is performed, if It is not to execute the Body section using the existing Bean instance. As will be introduced below, JSP: UseBean does not always mean create a new bean instance. Second, in addition to ID and CLASS, JSP: USEBean has three other properties, namely: scope, type, beanname. The following table briefly explains the usage of these attributes. Attribute Usage ID Names the variable that references the bean. If you can find the identical bean instance of ID and Scope, JSP: UseBean action will use an existing bean instance instead of creating a new instance. Class Specifies the full package name of the bean. Scope Specifies which of which is available in the context, one of the four values below: Page, Request, Session, and Application. The default value is Page, indicating that the bean is only available within the current page (saved within the current page). Request means that the bean is valid within the current customer request (saved within the servletRequest object). Session indicates that the bean is valid for all pages in the current HTTPSession. Finally, if application is valued, the bean is valid for all pages with the same servletContext. Scope is important because JSP: UseBean instantizes new objects only when there is no object with the same ID and Scope; if the ID and Scope are the same object, use existing objects directly, this Anything between the JSP: USEBean starts tag and end tags will be ignored. TYPE specifies the type of variable that references the object, it must be the name of the bean class, a super class name, one of the interface names implemented by this class. Remember that the name of the variable is specified by the id attribute. BeanName Specifies the name of the bean. If the Type property and the beanName property are provided, the Class property is allowed to omit. 13.4 JSP: SETPROPERTY Action JSP: SetProperty is used to set the properties of the instantiated Bean object, there are two usage.
First, you can use JSP: setProperty outside the JSP: UseBean element, as shown below: ... ...... "Property = "SomeProperty" ... /> At this time, regardless of the JSP: UseBean finds an existing bean or a new BEAN instance, JSP: setProperty will execute. The second usage is to put JSP: setProperty into the JSP: UseBean element, as shown below: ... jsp: usebean> This JSP: SetProperty will only be executed when a new bean instance is created, and JSP: setProperty is not performed if you use an existing instance. JSP: setProperty action has the following four properties: Property Description Name Name property is required. It means which bean is to set the property. The Property property is required. It indicates which property to set. There is a special usage: if the value of the Property is "*", the request parameters that match all the names and bean property names will be passed to the corresponding attribute set method. The Value Value property is optional. This property is used to specify the value of the bean property. String data automatically converts into numbers through standard Valueof methods in the target class, Boolean, Boolean, Byte, Byte, Char, Character. For example, the property value of the Boolean and Boolean types (such as "True") converted through the attribute value of the Boolean.ValueOf conversion, the INT, and Integer types (such as "42") via Integer.Valueof. Value and Param cannot be used at the same time, but can be used any one. Param Param is optional. It specifies which request parameter as the value of the bean property. If there is no parameters of the current request, what is not done, the system does not pass NULL to the set method of the bean property. Therefore, you can let the bean provide the default attribute value, and only modify the default attribute value when the request parameter is clearly specified. For example, the following code segment indicates that if there is a NumItems request parameter, set the value of the NumberOfItems property to the value of the request parameter NumItems; otherwise, nothing. If Value and Param are omitted, its effect is equivalent to providing a param and its value is equal to the value of Property. Further, this kind of thought of automatic assignment of automatic assignment by using the request parameter and attribute name, you can also specify "*" in the Property (name of the bean property), then omit value and param.
At this point, the server will view all bean properties and request parameters, if the names are the same, automatically assign. Below is an example of using JavaBean calculations. If there is a Numdigits parameter in the request, the value is passed to the Numdigits property of the bean; Numprimes is similar. JSPprimes.jsp
Using JavaBean Title> head> head> < Center>
Using JAVABean Table> center>
Some Digit Primes: body> html> Note: NumberedPrimes code is slightly. 13.5 JSP: GetProperty action JSP: getProperty action extracts the value of the specified bean property, converts into a string, and then output. JSP: getProperty has two essential properties, namely: name, represents the name of the bean; Property, indicates which attribute to extract. Here is an example, more examples can be found in the forebel.