In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to use JSP web page tags, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Before you learn how to write your JSP page in Java, you need to understand the server-side JSP element called "actions", which performs server-side tasks without requiring us to write Java code. The action tag can be used by advanced page designers, as well as by script writers who may not be familiar with Java and want to display values stored in JavaBeans components. As mentioned earlier, most tags are based on the component-centric web development model. First, I'll describe some of the action tags 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 JavaBeans, so most JSP tags assume that you will use the information stored in bean. Before you can use a JavaBean, you must call the tag < jsp:usebean > to declare that you will use it. You will learn later whether this tag will generate a new bean instance in the page (which may have been generated in the previous session or application), depending on the scope (lifecycle) you declare for the bean.
The < jsp:usebean > tag should contain several parameters that state:
◆ the class to which the JavaBean belongs
◆ the name of the JavaBean instance
◆ the scope of the JavaBean (lifecycle)
For example:
< jsp:usebean ID= "myBeanInstance" CLASS= "com.myPackage.myBeanClass" SCOPE= "request" >. Body... < / jsp:usebean >
Here the < jsp:usebean > tag is followed by a body that is called after the bean is created, followed by a closing 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" / >
This sample program generates an instance of bean defined in the com.myPackage.myBeanClass class, named myBeanInstance; in the page, which exists only within its life cycle-- an HTTP request for this JSP page. 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 they are used. In the body sections of the < jsp:useBean > and < / jsp:useBean > tags, you can initialize the attributes of the bean with the < jsp:setProperty > tag. You can set the property of bean to a specified value or a value passed from a HTTP request, such as from a submitted form.
To initialize the property myProperty of bean to a specified value, you can use the following format:
< jsp:usebean ID= "myBeanInstance" CLASS= "com.myPackage.myBeanClass" SCOPE= "request" > < jsp:setProperty NAME= "myBeanInstance" PROPERTY= "myProperty" VALUE= "123" / > < / jsp:usebean >
The following format is used to initialize the same eBan attribute to a value passed from an HTML form element or an URL query string:
< jsp:usebean ID= myBeanInstance "CLASS=" com.myPackage.myBeanClass "SCOPE=" request "> < jsp:setProperty NAME=" myBeanInstance "PROPERTY=" myProperty "PARAM=" myFormElementName "/ > < / jsp:usebean >
Note that you cannot use both the VALUE and PARAM attribute flags in a < jsp:setProperty > tag.
In addition, when using < jsp:setProperty > in the body of the < jsp:usebean > behavior, you can use it alone within the page. Before that, a bean with appropriate scope must be defined with the < jsp:useBean > tag.
Scope of Bean
The SCOPE attribute of the < jsp:useBean > tag performs a simple function: it sets the scope of the associated bean and has four possible values. You can use scoped JavaBeans in your JSP application depending on the situation.
Scope description
The ◆ Page object can only be accessed by a client program from the page on which it resides.
The ◆ request object is accessed by a client program during the lifetime of a customer request.
The ◆ session object is accessed by a client program from anywhere in the application for the lifetime of the entire user session.
◆ application objects can be accessed by client programs from any page within the application during the lifetime of the application.
Different object lifecycles affect how the < jsp:useBean > tag creates or retrieves bean instances. When the customer request ends and the output is sent back to the browser, the page bean and request bean are destroyed. So the < jsp:useBean > tag must create a new instance for each new request. However, when you create a new session bean, the instance of the bean remains until the end of the session's lifetime, or until you explicitly destroy it. So the < jsp:useBean > tag creates a new instance of bean when there is no instance in the current session; otherwise, it simply retrieves the current instance. The same rules apply to the JavaBeans of an application unless they are terminated when the application reloads or the server restarts.
If you are a SSJS developer, it is helpful to learn some similarities in SSJS session management. In SSJS, page bean and request objects have the same scope; session bean and client objects have the same scope; and application bean and project objects have the same scope. For example, if you store the value in a session bean, you can then access the value from any JSP page by a single user, just as you would store the value in a client object in SSJS. 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, request, client, and project objects are all single.
Notice that there is no corresponding object for request bean in SSJS. This is because, unlike SSJS, JSP allows multiple pages to be executed within a customer request. This feature will be discussed more later.
Display dynamic content
Once you have created a bean, you can use it to generate dynamic content in the JSP page. JSP defines a < jsp:getProperty > tag to display the attributes of the bean. The bean can be defined within the page with the < jsp:useBean > tag, or it can be session bean or application bean previously defined in the application. The < jsp:getProperty > tag has two parameters: NAME and PROPERTY.NAME indicate the source object previously defined in the < jsp:useBean > tag, and PROPERTY represents the attribute value of the object to be displayed. For example:
< jsp:usebean ID= "myBeanInstance" CLASS= "com.myPackage.myBeanClass" SCOPE= "request" / > < H2 > myProp= < jsp:getProperty NAME= "myBeanInstance" PROPERTY= "myProp" > < / H2 >
As you can see, you can mix HTML tags with JSP tags to generate and arrange HTML content dynamically.
Redirect to an external page
JSP defines a tag < jsp:request > that you can use to redirect to an external page. There are two ways to choose: specify the FORWARD parameter or specify the INCLUDE parameter.
Using the FORWARD parameter, you can redirect to a valid URL. This method can effectively abort the processing of the current page at the place where the redirection occurs, but the processing needs to be done before that. This is very similar to the typical redirection used in CGI, SSJS, ASP, and JavaScript.
Using the INCLUDE parameter, you can not only redirect to another page, but also return to the calling 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 > tag in the calling page. In fact, the called page does not know that it is being called by another JSP page, it just sees a HTTP request and returns some HTML text in response.
Remember, 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 page. Note, however, that if the resource you visit returns a complete HTML page with < HTML > and < BODY > tags, you probably won't get the results you want.
A simple example.
In example 1, we give an example of a bean of type jsp.beans.samples.SuperSimpleBean, named ssb. Because its scope has been 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 use the < jsp:getProperty > tag to display the value of counter in the HTML page. Given some specific bean property names, the following code is easy for HTML designers to write.
Example 1
< HTML > < HEAD > < META. NAME= "GENERATOR" Content= "NetObjects ScriptBuilder 2.01" > < TITLE > Counter Page < / TITLE > < / HEAD > < jsp:useBean ID= "ssb" SCOPE= "session" CLASS= "jsp.beans.samples.SuperSimpleBean" / > < jsp:setProperty NAME= "ssb" PROPERTY= "counter" VALUE= "2" / > < h3 > Counter: < jsp:getProperty NAME= "ssb" PROPERTY= "counter" / > < / h3 > < / BODY > < / HTML > Thank you for reading this article carefully. I hope the article "how to use JSP tags" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.