Before learning how to write your JSP page in Java, you need to understand the server-side JSP element called behavior (js|), which performs server-side tasks without the need for us to write Java code. Action tags can be used by Advanced page designers, and can be used by script writers who may not be familiar with Java and want to display values stored in JavaBeans components. As mentioned earlier, most of the tags are based on a component-centric web development model. First, I'm going to describe some of the action tags that the JSP provides, and then show an example of a JSP page that simply uses a tag to display information from a javabean--without writing Java code.
Load a JavaBean
Remember that the JSP model is inseparable from JavaBeans, so most JSP tags assume that you will use the information stored in the bean. Before you use a JavaBean, you must call the label <jsp:usebean> to declare that you will use it. Later on you will learn whether this tag will generate a new bean instance within the page (the instance may have been generated in the previous session or application), depending on the scope (lifecycle) that you declared for the bean.
<jsp:usebean> label to include several parameters, which are described separately:
Here <jsp:usebean> tag is a section of the body that is invoked after the bean is created, followed by an end tag </jsp:usebean>. If the subject is empty, you can also choose the following simple form:
The sample program generates an instance of the bean defined in the Com.myPackage.myBeanClass class, named Mybeaninstance in the page, that exists only within an HTTP request to this JSP page for its life cycle. This bean can now be used by the page.
Initialize a JavaBean
Not all JavaBeans can be simply created and used, and some need to be initialized before use. In the main part of the <jsp:useBean> and </jsp:useBean> tags, you can initialize the Bean's properties with <jsp:setProperty> tags. You can set the Bean's properties to a specified value or a value that is passed from an HTTP request, such as from a submitted form.
To initialize the Bean's properties myproperty to a specified value, you can use the following format:
Note that you cannot use both the value and Param attribute flags in one <jsp:setProperty> label.
In addition, when using <jsp:setProperty> in the main body of <jsp:usebean> behavior, you can use it alone on the page. Before that, you must define a bean with the appropriate scope with the <jsp:useBean> label.
Scope of the Bean
<jsp:useBean> the scope property of the label completes a very simple function: it sets the scope of the relevant bean, with four possible values, as shown in the following table (table 1). You can use scoped JavaBeans in your JSP application depending on the situation.
Table 1. The scope of the JSP and the corresponding maintenance
Scoping description
The Page object can only be accessed by a client program from the page it is on.
The request object is accessed by a client program within the lifetime of a customer request.
The session object is accessed by a client program from anywhere in the application during the lifetime of the entire user conversation.
The Application object can be accessed by the client program within the application from any page within the lifetime of the application.
Different object lifetimes affect how the <jsp:useBean> tag creates or retrieves a bean instance. When the client request finishes and the output is returned to the browser, the page bean and the request bean are destroyed. So <jsp:useBean> tags must create a new instance for each new request. However, when you create a new session bean, the instance of the bean retains the end of the session's lifetime, or until you explicitly destroy it. The <jsp:useBean> tag creates a new instance of the bean when the instance does not exist in the current session, otherwise it simply gets the current instance back. The same rules apply to the JavaBeans of the application unless they terminate when the application is loaded or the server restarts.
If you are a SSJS developer, it is helpful to learn some similarities between SSJS session management. In Ssjs, the page bean is the same scope as the request object, the session bean is the same as the client object scope, and the application bean is the same as the project object scope. For example, if you store a value in a session bean, you can then access the value from any JSP page, just as you would in SSJS to have the value in the client object. However, JSP provides a more flexible state retention mechanism than SSJS because you can define any number of page, request, session, and application beans. In Ssjs, the request, client, and project objects are single.
Note that there are no objects corresponding to the request Bean in the SSJS. This is because unlike SSJS, JSP allows multiple pages to be executed within a single customer request. Further discussion of this feature will be made later.
Show dynamic Content
Once you create a bean, you can use it to generate dynamic content in your JSP pages. The JSP defines a <jsp:getProperty> label to display the properties of the bean, which can be defined in a page with a <jsp:useBean> tag or as a session previously defined in the application Bean or application bean. The <jsp:getProperty> label has two parameters: Name and property. Name indicates the source object previously defined in the <jsp:useBean> label, which represents the property value of the object to display. For example:
As you see, you can use HTML tags in combination with JSP tags to dynamically generate and arrange HTML content.
Redirect to an external page
JSP defines a label <jsp:request>, which you can use to redirect to an external page, and there are two options: Specify the forward parameter or specify the include parameter.
Using the forward parameter, you can redirect to a valid URL. This method effectively aborts the processing of the current page at the time of redirection, but the process before that is done. This is very similar to the typical redirection used in CGI, Ssjs, ASP, and JavaScript.
With the include parameter, you can not only redirect to another page, but also return to the call page when you have finished processing within the called page. For example, you call another JSP page that dynamically generates HTML to generate HTML code, and when returned, the HTML is inserted into the <jsp:request> tab of the calling page. In fact, the called page doesn't know it's being called by another JSP page, it just sees an HTTP request and returns some HTML text as a response.
Keep in mind that you can use the Include method to access any resource that responds to HTTP requests, such as static HTML pages, JSP pages, Java servlets, Ssjs pages, and ASP pages, to generate a response that you want to include in your Web page. But note that if the resource you are accessing returns a full HTML page containing the <HTML> and <BODY> tags, you probably won't get the results you want.
A simple example
An example of a bean of type Jsp.beans.samples.SuperSimpleBean is given in Example 1, named SSB. Because its scope is set to session, it is available for the rest of the user session. In other words, after it is created, I can access it by name in any page of the application. I also initialized its properties to counter. You can then display the counter value in the HTML page with the <jsp:getProperty> tag. Given some specific bean property names, the following code is easy to write for the HTML Designer.
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.