JSP web page tag instance application

Source: Internet
Author: User

Before learning how to compile your JSP page in Java, you need to understand the server-side JSP element called "Action actions)", which executes server-side tasks without writing Java code. The action tag can be used by Advanced page designers. It can also be used by script writers who are not familiar with Java and want to display the values stored in the JavaBeans component. As mentioned above, most labels are based on the component-centric web development model. First, I want to describe some action labels provided by JSP, and then show an example of a JSP page that only uses tags to display information from a JavaBean-No Java code is required.

Load a JavaBean

Remember, the JSP model is inseparable from the JavaBeans. Therefore, most JSP labels assume that you will use the information stored in the bean. Before using a JavaBean, you must call the label <jsp: usebean> to declare that you will use it. Later, you will learn whether the tag will generate a new bean instance in the page. This instance may have been generated in the previous session or application ), depends on the scope lifecycle you declare for this bean ).

<Jsp: usebean> the tag must contain several parameters, which are described as follows:

◆ Classes subordinate to this JavaBean

◆ Name of the JavaBean instance

◆ Scope lifecycle of this JavaBean)

For example:

 
 
  1. ﹤jsp:usebean ID="myBeanInstance" CLASS="com.myPackage.myBeanClass" SCOPE="request"﹥  
  2.  
  3. ……body……  
  4.  
  5. ﹤/jsp:usebean﹥  
  6.  

Here, the <jsp: usebean> tag is the main part called after bean creation, and then an end tag </jsp: usebean>. if the subject is empty, you can also choose the following simple form:

<Jsp: usebean ID = "myBeanInstance" CLASS = "com. myPackage. myBeanClass" SCOPE = "request"/>

In this example,. myPackage. the bean instance defined in the myBeanClass class is named myBeanInstance on this page. It only exists within its lifecycle-an HTTP request for this JSP page. This bean can be used on this page now.

Initialize a JavaBean

Not all JavaBeans can be simply created and used, and some still need to be initialized before use. In the entity section of the <jsp: useBean> and </jsp: useBean> tags, you can use the <jsp: setProperty> tag to initialize bean attributes. You can set the bean attribute to a specified value or a value sent from an HTTP request, for example, from a submitted form.

To initialize the bean property myProperty as a specified value, you can use the following format:

 
 
  1. ﹤jsp:usebean ID="myBeanInstance" CLASS="com.myPackage.myBeanClass" SCOPE="request"﹥  
  2.  
  3. ﹤jsp:setProperty NAME="myBeanInstance" PROPERTY="myProperty" VALUE="123"/﹥  
  4.  
  5. ﹤/jsp:usebean﹥  
  6.  

The following format is used to initialize the same eBan attribute to a value sent from an HTML form element or a URL query string:

 
 
  1. ﹤jsp:usebean ID="myBeanInstance" CLASS="com.myPackage.myBeanClass" SCOPE="request"﹥  
  2.  
  3. ﹤jsp:setProperty NAME="myBeanInstance" PROPERTY="myProperty" 
  4.  
  5. PARAM="myFormElementName"/﹥  
  6.  
  7. ﹤/jsp:usebean﹥  
  8.  

Note: you cannot use both the VALUE and PARAM attribute flag in the <jsp: setProperty> tag.

In addition, you can use <jsp: setProperty> In the <jsp: usebean> behavior entity on the page. Before that, you must use the <jsp: useBean> label to define a bean with an appropriate scope.

Bean Scope

<Jsp: useBean> the SCOPE attribute of the tag has a very simple function: it sets the SCOPE of the bean, there are four possible values. You can use a scoped JavaBeans in your JSP application as needed.

Scope description

◆ A Page object can only be accessed by a client program from its Page.

◆ The request object is accessed by a client program within the lifetime of a customer request.

◆ The session object is accessed by a client from anywhere in the application within the lifetime of the user session.

◆ The application object can be accessed by client programs from any page in the application within the lifetime of the application.

Different object lifecycles will affect how the <jsp: useBean> tag creates or re-obtains bean instances. After the customer's request ends and the output is sent back to the browser, the page bean and request bean will be destroyed. Therefore, the <jsp: useBean> tag must create a new instance for each new request. Even so, when you create a new session bean, the bean instance will be retained until the session lifecycle ends, or until you explicitly destroy it. Therefore, the <jsp: useBean> tag creates a new bean instance when no instance exists in the current session; otherwise, it simply retrieves the current instance. The same rules are also used by the application's JavaBeans unless they are terminated when the application is reloaded or the server is restarted.

If you are an SSJS developer, it is helpful to draw some similarities in SSJS session management. In SSJS, page bean and request object have the same scope; session bean and client object have the same scope; application bean and project object have the same scope. For example, if you store a value in a session bean, then you can access this value from any JSP page by a single user, just as in SSJS, values are stored in the client object. However, JSP provides a more flexible State persistence mechanism than SSJS, because you can define any number of pages, requests, sessions, and application beans. in SSJS, the request, client, and project objects are single.

Note that no object corresponds to the request bean in SSJS. This is because, unlike SSJS, JSP allows multiple pages to be executed in a single customer request. This feature will be further discussed later.

Show Dynamic Content

Once a bean is created, you can use it to generate dynamic content on the JSP page. JSP defines a <jsp: getProperty> tag to display bean attributes. This bean can be defined by the <jsp: useBean> tag on the page, it can also be the session bean or application bean previously defined in the application. <jsp: getProperty> the tag has two parameters: NAME and PROPERTY. NAME indicates the source object previously defined in the <jsp: useBean> label. PROPERTY indicates the PROPERTY value of the object to be displayed. For example:

 
 
  1. ﹤jsp:usebean ID="myBeanInstance" CLASS="com.myPackage.myBeanClass" SCOPE="request"/﹥  
  2.  
  3. ﹤H2﹥ myProp=﹤jsp:getProperty NAME="myBeanInstance" PROPERTY="myProp"﹥ ﹤/H2﹥  
  4.  

As you can see, you can mix HTML tags with JSP tags to dynamically generate and arrange HTML content.

Redirect to an external page

JSP defines a tag <jsp: request>. You can use it to redirect to an external page. There are two options: Specify the FORWARD parameter or the INCLUDE parameter.

Using the FORWARD parameter, You can redirect to a valid URL. This method can effectively stop the processing process of the current page in the case of redirection, but the previous processing is still required. This is similar to the typical redirection used in CGI, SSJS, ASP, and JavaScript.

With the INCLUDE parameter, you can not only redirect to another web page, but also return to the call page after processing in the called page. For example, you can call another JSP page that can dynamically generate HTML to generate HTML code. When returned, these HTML pages will be inserted into the <jsp: request> tag. In fact, the called page does not know that it is being called by another JSP page. It only sees an HTTP request and returns some HTML text as a response.

Remember, you can use the INCLUDE method to access static HTML pages, JSP pages, Java Servlets, SSJS pages, ASP pages, and other resources that respond to HTTP requests, to generate a response that you want to include in your webpage. However, if the resources you access return a complete HTML page containing the <HTML> and <BODY> tags, you may not get the expected results.

A simple example

In example 1, a type is jsp. beans. samples. the example of the bean of SuperSimpleBean, named ssb. since it has set its scope to session, it is available for other user sessions. In other words, after it is created, I can access it by name on any page of the application. I also initialize the counter attribute. Then, the <jsp: getProperty> tag can be used to display the counter value on the HTML page. Given some specific bean property names, the following code is easy for HTML designers to write.

Example 1

 
 
  1. ﹤HTML﹥  
  2.  
  3. ﹤HEAD﹥  
  4.  
  5. ﹤META. NAME="GENERATOR" Content="NetObjects ScriptBuilder 2.01"﹥  
  6.  
  7. ﹤TITLE﹥Counter Page﹤/TITLE﹥  
  8.  
  9. ﹤/HEAD﹥  
  10.  
  11. ﹤BODY﹥  
  12.  
  13. ﹤jsp:useBean ID="ssb" SCOPE="session" CLASS="jsp.beans.samples.SuperSimpleBean"/﹥  
  14.  
  15. ﹤jsp:setProperty NAME="ssb" PROPERTY="counter" VALUE="2"/﹥  
  16.  
  17. ﹤h2﹥Counter: ﹤jsp:getProperty NAME="ssb" PROPERTY="counter"/﹥﹤/h2﹥  
  18.  
  19. ﹤/BODY﹥  
  20.  
  21. ﹤/HTML﹥   
  22.  

Through the above application of tag instances in JSP web pages, do you have a certain understanding?

  1. JSP tag library Parsing
  2. Storage and display of images in databases Based on JSP
  3. Methods for constructing JSP and Javabean development and release Environments
  4. Analysis of JSP Design Mode
  5. What is JSP and its strong weakness?
  6. How Tomcat improves performance on JSP pages

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.