3. Interaction with XML through tag library JSP
This is what I mentioned earlier, but because it is too important, I must also mention it in this article. The tag library can define the custom tags that appear on the JSP page as XML-like elements, and can associate specific Java code with each tag. For example, suppose you can access a weather condition database and you need to output the current weather condition. Then, you can insert JDBC program code in JSP to directly query the database (although this is not a good choice) and encapsulate the code into a JavaBean, or wrap it into a tag library. With the last choice, the program code on your JSP page looks like:
<% @ Taglib uri = "the TLD file" prefix = "foo" %>
Current weather is <foo: Weather/>
Note that no trace of Java code is found in the above Code. As a page designer, you use a familiar syntax like <foo: weather/>, which looks very similar to any other tag. Insert the HTML string that contains the current weather condition in the page.
The Tag Library has an associated XML Descriptor file named Tag Library Descriptor (Tag Library Descriptor, TLD ). Each tag in the TLD file has an entry, including its name, version, and other optional information. On the JSP page, you can use the "<% @ _ taglib prefix =" foo "%>" command to specify the TLD file. Attribute "prefix" is used to specify a prefix. It is used to reference any tag using a specific library on the JSP page. Why do we need to use the tag <foo: Weather/> instead of just <Weather/>. The exact location of TLD files depends on the application server in use.
A tag library tag can replace the corresponding Java program code to complete the program logic. Each tag is equivalent to a Java class with the same name. This class must implement the TagSupport interface, including the method for capturing event triggers as the JSP Engine for processing this page. When it encounters this mark for the first time, the engine will call the doStartTag () method. This method can be left empty or the application logic can be executed only when necessary. When this method returns SKIP_BODY, the engine skips the TAG body. When it returns EVAL_BODY_INCLUDE, the engine processes this mark and its sub-mark. Similarly, the JSP Engine calls the doEndTag () method after analyzing the end tag. The doAfterBody () method allows you to execute an action after the engine processes the element body, but it must be before the doEndTag () method works. The following is a sample program code for the Weather class that implements Weather conditions:
Import javax. servlet. jsp. tagext .*;
Import java. io .*;
Public class Weather extends TagSupport {
Public int doStartTag (){
Try {
JspWriter out = pageContext. getOut ();
Out. print ("sunny and cloudy mixed"
"Rain and sunshine .");
} Catch (IOException e ){
System. out. println ("Error" e );
}
Return (SKIP_BODY );
}
}
When the engine encounters a "<somePrefix: Weather/>" tag, it searches for a class with the same name in the tag library. If the doStartTag () method is implemented (in this example), it will be called. This causes the string to contain information that adapts to the displayed weather conditions. Because the method returns the SKIP_BODY, the JSP reader moves to the end of the tag.
The simplest way to use JSP and tag libraries is to use the Apache Tomcat engine. This engine also serves as a reference implementation for Servlet and JSP application interfaces.
When the tag library is used, the JSP page looks very like an XML file. When a JSP page is processed, the engine executes the program code associated with the tag (in fact, the JSP Engine is called to translate the JSP page into a servlet and then compile the servlet. Methods associated with the tag library are included in the servlet .), A person familiar with XML can design and experiment with various page la s without changing any Java program code. Of course, the degree of separation between code and data is mainly dependent on the design of tag library elements.
Assumerver Pages (JSP) and XML are two crucial components of Sun's J2EE. JSP is an effective tool for creating application server programs, and the customer can be a browser, a device, or other applications.