JSP Actions Use you can dynamically insert a file, reuse the JavaBeans component, proceed to another page, or generate an HTML for the Java plugin. The action you can use is: (1) jsp: include - contains a file when the page is requested. (2) JSP: Usebean - find or instantiate a JavaBean. (3) JSP: setProperty - Set a JavaBeaN property. (4) JSP: getProperty - inserts the property of JavaBean into the output. (5) JSP: Forward - Let the requester can go forward to a new page. (6) JSP: Plugin - Generates a specific browser code with the Object or Embed tag to Java Plugins. 1, JSP: Include Action This action allows you to include some files on the upcoming page:
Unlike the include Directive, this action will contain the file when the page is requested, and include Directive is included when the JSP page is converted to servlet. In order to improve efficiency, include Action has a little sacrifice, ie, it does not allow the page included with a general JSP code (for example, it is not available to HTTP header), but it has significant flexibility, as JSP below Code, which implements four different fragments into the page below. Every time you change, you only need to modify these four files without changing the primary JSP page.
Whatsnew.jsp
JSP tutorial title>
what "s new at Chinese COMIC SITES TABLE> center>
Here is a summary of outness news stories:
l> body> html> Of course, you can define yourself? Ml file, but if you pay attention:
You should put your files within the news directory in your JSP directory.
JSP: Use of UseBean Action
First, grammar:
} "type =" package.class "} {/> |> Other elements jsp: usebean>}
This action allows you to load a JavaBean into a JSP page. This is a very useful ability because it allows you to use reusable Java classes without sacrificing performance. The simplest syntax is used to specify a bean:
This usually means "instantiate a class object by specifying a class and will be binded to a variable that is specified by the ID." However, just as we see, you can specify a scope property to make the bean not only contact the current page. In this case, it is very useful to get a reference to the existing bean, and only one new thing is created only when there is no the same ID and Scope beans. Now, you already have a bean, you can modify it via JSP: setProperty, or use the scriptlet or a clear call method by using the name specified by ID. When you say "this bean has a" X-type property called Foo "," You really means "this class has a method called getfoo, it returns a certain value of the X type, and another method is called Setfoo, it is parameter in x. "This JSP: setProperty action will introduce in detail in the next unit, but now you can give a clear value, give a property to indicate that this value is inherited from the parameter from the request. It is also possible to simplify the attribute to mark this value from inheritance from the parameter as the attribute name. You can get existing JSP expressions or scripTlet properties by calling applicable getxxx methods, or more common, using JSP: getProperty Action. Note that the class specified for the bean must be under the classpath of the server's rules, rather than reserved the path of the class that is automatically loaded when changing. For example, on the Java Web Server, it must go to the class directory or in a JAR file in the lib directory, instead of in the directory of servlets. Let us look at a very simple example that loads a bean and sets / get a simple string parameter. Beantest.jsp
Reusing JavaBeans in jsp title> head>
message : i> h1> body>
SimpleBean.java
The following is the original code of Bean:
package hall; public class SimpleBean {private String message = "No message specified"; public String getMessage () {return (message);} public void setMessage (String message) {this.message = message;}} run results: page Output: Reusing JavaBeans in JSP
Message: Hello WWW
Second, JSP: Detailed usage of usebean
The simplest way of using Bean is:
To load beans, you need to modify and retrieve the properties of the bean with JSP: SetProperty and JSP: getProperty. Moreover, there are two other options. First, you can use the format of the container, that is:
Body jsp: usebean>
It should be pointed out that the Body section should be performed only when Bean is instantiated, not when it is found and used each time. Beans can be shared, so not all JSP: Usebean statements produce a new bean instance. Second, in addition to ID or Class, there are three properties you can use: scope, type, and beanname. These properties summarize as follows:
Attribute Meaning ID Names a variable, this variable will point to Bean. If there is a bean having the same ID and Scope, it is not new. Class pointed out the full package of Bean. Scope indicates that the Bean can be used before and after it. There are four possible values: Page, Request, Session, and Application. The default is Page, indicating that the bean is available only in current pages (saved in the current PageContext). A value of Request indicates that the bean is only for the current client request (saved in the servletRequest object). The value of the session pointed out that the object is available for all pages in the current HTTPSession life cycle. Finally, the value of Application pointers that the object can be used on all the shared servletsContext. Use JSP: UseBean creates a new bean only when there is no Same id and scope, if you already use it, and ignore the code that starts and ends with JSP: UseBean flag. TYPE indicates the type of variable that will point to the object. This must match the class name or a superclass or an interface implementation class. Remember, the name of the variable is specified by the id attribute. BeanName gives a name, you should be available in the instantiation method of Beans. It allows you to give Type and a beanName and omitted class properties.
Third, JSP: SetProperty Action
grammar:
In front of us, we know that you can use JSP: setProperty to assign a value for a bean attribute. You can use two ways to implement it. First, in JSP: UseBean (instead of) use JSP: SetProperty:
...
In this way, JSP: setProperty will be executed whether there is a bean having the same ID and Scope. Alternatively, JSP: setProperty appears within JSP: UseBean Elements, such as:
... jsp: usebean>
In this case, JSP: SetProperty is only performed when new objects are instantiated.
The following is the available properties of four JSP: SetProperty:
Attribute Usage Name This is a must-select attribute. It pointed out which bean's attribute will be set. JSP: Usebean must appear before JSP: setProperty. Property This is a must-select attribute. Indicates which property you will set. However, there is a special case: if it is worth "*", all names match the request parameters that match the properties of the bean will be passed to the corresponding attribute setting method. Value This is an optional attribute. It specifies the value of the properties set. The value of the string will be automatically converted to Numbers, Boolean, Boolean, Byte, Byte, Char, and Character by the standard Valueof method of the corresponding object or package. For example, the value of the Boolean or Boolean property will transform through the boolean.valueof method, and the value "42" of an int or Integer property will be converted via Integer.Valueof. You can't use the value and param properties at the same time, but both are allowed. PARAM This is an optional attribute. It indicates the parameters of the Request to be inherited. If the current Request does not have such a parameter, what does not do: the system does not pass NULL to the method of setting the properties. Therefore, you can use the default value of Beans. For example, the following program executes "Set the NumberOfItems property to any NumItems Request parameter, if there is such a request parameter, otherwise, nothing." If you use Value and Param at the same time, this is the same as the property name that you set the name of the param to the bean. You can set the value of the Request corresponding to the properties of the value and param from "*" and omitted the value of Value and Param. In this case, the server matches the repeated lookup of the available properties and the request parameter to match the same name.
Four, JSP: getProperty Action
grammar:
This property retrieves the value of the property of the bean and convert it into a string, and then insert it into the output. It has two must-select properties: Name, with the name introduced by JSP: UseBean before, must be inserted attributes