Use customized labels
Although you can embed Java code on the JSP page and execute it on the server, JSP also supports using custom tags to insert dynamic content, it has a mechanism that allows you to insert your own HTML-like tags into JSP pages. In other words, your JSP page can generate dynamic content using the simple tag syntax for inserting Java code. However, custom tags are not very useful.
Creating a custom tag is much more complicated than using a simple scriptlet in a JSP webpage, because it takes several steps to connect your Java component with the JSP code. However, custom tags are easy to distribute and reuse. Custom tags are supported in JSP creation tools.
In the following example, custom tags are used for JSP pages that generate dynamic content. Note: In this example, we no longer need to introduce Java classes, declare variables, or write any Java code:
Today's Menu
Lunch
Our Special of the Day
<Meal = "lunch" insertcatchofday>
From the preceding statement, we can see that this page is much simpler than the previous scriptlet example, because it does not contain the initialization object and execute the corresponding method. However, JSP web page code is only a part. For each custom tag, it also includes the following three components:
(1) web pages that contain custom tags. For example, the preceding code snippet uses the custom tags of insertCatchOfDay. Before using custom tags, you must specify Taglib Directive on the page to provide the location of the tag library Descriptor (defined for tags. When you execute custom tags, the web page also typically defines one or more tag attributes (for example, "meal" in this example) to determine the dynamic content.
(2) Tag library descriptor. It is an XML file that defines a custom Tag and connects it to the Tag Handler. A Tag library descriptor contains different attributes of a Tag, the name (location) of the relevant Tag Handler, and other information that the JSP engine needs to process custom tags.
(2) Tag Handler. It is a Java class that performs operations jointly with custom labels. For example, in the insertCatchOfDay label above, Tag Handler is the Java class that executes the database query to obtain the corresponding menu item.
We have seen a JSP page using custom tags. Let's take a look at the other two components.
Tag Handler
Tag handler is a Java class similar to servlet. However, the servlet can execute Servlet interfaces and be executed by html get or POST requests. Tag handler can also execute a Tag interface (javax. servlet. jsp. tag) and execute it when the custom Tag is processed by the JSP Engine.
If custom tags contain attributes, tag handler must define these attributes and each get/set method. For example, when defining the tag handler of the custom label above insertCatchOfDay, we must define the "meal" attribute and the get and set methods related to it:
Private String meal = null;
Public void setMeal (String s ){
Meal = s;
}
Public String getMeal (){
Return meal;
}
Tag Library Descriptor
If you are dealing with Java technology all the time and do not know about XML, the tag library descriptor component may seem unfamiliar. But you do not need to worry, because you do not need to learn a new programming language. The tag library descriptor only uses tag syntax similar to HTML to define the names and attributes of custom tags, which is more like defining an object.
The following tag library descriptor defines the insertCatchOfDay tag. Note: This file defines the custom Tag name, attributes, and related Tag Handler classes:
Xml version = "1.0"?>
InsertCatchOfDay
Com. sun. CatchOfDayHandler
Queries menu database for the catch of the day.
Meal
Like the name of the defined attribute, the Tag library descriptor can also define the data type and specify its attribute (whether required or not). Before the Tag Handler is executed, it allows the JSP Engine to perform some error checks. There are other information. For example, to use JSP to create a tool, the Library name and version number can also be included in the tag library.
More examples
In the following example, the first example uses the HTTP request object (HttpServletRequest) on the JSP page) to determine the version of your browser and return the corresponding content from one of the three HTML pages:
Agent = request. getHeader ("User-Agent ");
If (agent. startsWith ("Mozilla/4.0 "){
%>
}
Else if (agent. startsWith ("Mozilla/3.0 "){
%>
}
Else {
%>
}
%>
Note: This page can be accessed directly without declaring or initializing an HTTP request object. Request and Response (HttpServletResponse) objects can be implicitly used in JSP pages. Like servlet, JSP pages can obtain parameter values from HTML forms using request objects.
Here are your current selections:
Selections = request. getParameterValues ("items ");
If (selections! = Null ){
%>
For (int x = 0; x
%>
:
}
%>
}
Else {
%>
(No items selected)
}
%>
In this example, after each parameter value is read, the JavaBean Component queries the required information. Using Bean in JSP web pages can easily return dynamic web content from the database.
Conclusion
If you are looking for a web program that can easily establish a connection to the server's Java components, the assumerver Page is exactly what you need. In addition to ejbs, RMI, JDBC, and JavaBean, separating HTML code and web programs makes it easier to organize and run JSP web pages. In fact, web designers can create JSP pages without the help of Java developers, so you don't have to worry about creating web pages and writing HTML code anymore.
<Full text>