<!-login.jsp-->
<%@ taglib uri= "/tlds/taglib.tld" prefix= "Tagclass"%>
The process of the JSP server parsing markup
So when a tag is embedded in a JSP page, how does the JSP server parse the tag? The meanings of each object are as follows:
Client: Represents clients.
jsp-server:jsp server.
Jsp-page:jsp page.
TLD: Tag library description file, defining various attributes and processing files for tags and tags.
Tagclass Tag Handler
When a user accesses a JSP page, the request is sent to the JSP server, the JSP server will be based on this request to call the corresponding page, if the page has a custom tag, JSP service will be based on the page instructions <%@ taglib> To access the information about the handler for the TLD, call the handler's constructor method, start the tag handler, and read the tag's properties and corresponding values. Call the appropriate set method for each property that is not set. When a marker is used for the first time, any of its properties are not set, so the set method is called for each property. After the property is set, the JSP server invokes the handler's doStartTag (), and then calls the Doendtag () method. Finally, the JSP server will continue to process the remaining pages, calling the release () method at the end of the page to clean up all the resources occupied.
TLD file
The TLD (Tld:tag Library descriptor Tag Library descriptor) file, a standard XML-formatted tag definition file, is used to hold information about the tag, and the following is a typical TLD file.
<?xml version= "1.0" encoding= "iso-8859-1"?> <!-xml version and its character set--> <! DOCTYPE taglib Public "-//sun Microsystems, Inc.//dtd JSP Tag Library 1.1//en" "Http://java.sun.com/j2ee/dtds/web-jsptag Library_1_1.dtd > <!-Document type definition--> <taglib> <!-This tag indicates that we started to describe a tag library--> <tlibversion>1.0</ Version of the JSP used by the Tlibversion> <!-Tag library version--> <jspversion>1.1</jspversion> <!---> <shortname >tagclass</shortname> <!-default name--> <tag> <name>login</name> <!-tag name--> < Tagclass> Tagclass.login.login <!-The name of the corresponding class that handles this tag--> </tagclass> <info> <!-The description of this marker--> & Lt;/info> <attribute> <!-The name of the property--> <name>height</name> <!-property that defines the tag--> <require D>true</required> <!-indicates that this attribute is not required--> <rtexprvalue>true</rtexprvalue> <!- Indicates whether this property can be exported with the results of the JSP's program segment--> </attribute> <attribute> <name>width</name>
<required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>
In this TLD file, a tag library with only one tag is defined, and the tag named login invokes an applet to verify the legality of the user. The class that handles this tag is tagclass.login.login. Width and height are the two attributes of this tag. property is a value that is sent as a parameter using the tag characters. We can add a few tags to the example above, or we can have several attributes added to each tag. When we develop tag library, we do not have to start from scratch and write a new TLD ourselves. We can use an integrated development environment, or we can modify the example above.
Taglib directives
So when the JSP server parses a tag, how does it define a tag library? This is the main responsibility of the TAGLIB directive.
Taglib directives
Defines a tag library with a prefix for its custom tags.
JSP syntax
Copy Code code as follows:
<%@ taglib uri= "uritotaglibrary" prefix= "TagPrefix"%>
Example
<%@ taglib uri= "/tlds/taglib.tld" prefix= "Tagclass"%>
Describe
<% @ taglib%> directive declares that the JSP file uses a custom tag and references the tag library.
The prefix of their tags was also specified. You must use the <% @ taglib%> directive before using the custom tag.
Property
Uri= "uritotaglibrary": Uniform Resource Identifier (URI) uniquely name a custom tag based on the prefix of the tag, and the URI can be a relative or absolute path.
Prefix= "TagPrefix": the prefix before the custom tag. As in the above example, the </tagclass:login>
handler for the tag (tag handle)
Let's take an example to see how to implement a tag handle. The first is to look at its class diagram:
Let's take a look at its code again:
package tagclass.login; import javax.servlet.jsp.tagext.TagSupport; import
javax.servlet.jsp.*;
Import java.io.*; public class Login extends TagSupport {public login () {super ():} public int doStartTag () throws Jsptagexception {JSPWR
ITER out = Pagecontext.getout ();
try {out.println ("<applet codebase=applet/login/code=login.class width=200 height=100 > </APPLET>");}
catch (Exception e) {} return skip_body; PUBLICC int Doendtag () throws jsptagexception {return eval_page;} public void Release () {super.release ();} public Voi D setwidth (String language) {this.width = width;} public String getwidth () {return this.width;} public void SetHeight (
String height) {this.height=height;} public string GetHeight () {return this.height;} private string width;
Private String height; }
As we can see from the above, there are several requirements for implementing a simple tag handler: ① adds a class that inherits Java. Servlet.jsp.tagext.TagSupport class. This class provides java. Servlet.jsp.tagext.Tag all the methods required by the interface. In addition, you need to use some basic APIs to enable the JSP container to invoke the tag handlers that we provide ourselves. ② must create a get<attribute> and set<attribute> method for each of the tag attributes, which the JSP container needs to pass parameters with. ③ to create a constructor and a self-destruct for the tag handler. The JSP needs to start the handler using the constructor. The self-destruct is defined in the Realease () method. At the end of the lifecycle of the handler, you need to invoke the resource that the self-destruct frees up. ④ creates two methods named doStartTag () and Doendtag () to perform specific processing and output actions. The two methods are called at the start and end positions of the custom marker. Their return value is the static int defined in the tag interface, which are the following static values:
Skip_body implicitly 0: Skips the code between the start and end tags.
Eval_body_include implied 1: The content of the body is exported to the existing output stream
Skip_page implicitly 5: Ignores the remaining pages.
Eval_page implied 6: Continue to the following page
Of course the marker also has its own drawbacks. Very inconvenient encapsulation process, limited functionality. For some less complex and functional single logical description, the need to pass the parameter requirements are not high, the use of JSP tags, more convenient. For most business logic applications, it is much better to use beans, and it is also appropriate for servlet control.
Attached: The complete code of the example used in the article
JSP code: LOGIN.JSP
<%@ taglib uri= "/tlds/taglib.tld" prefix= "Tagclass"%>
Tag Description Library: Taglib.tld
<?xml version= "1.0" encoding= "iso-8859-1"?> <!
DOCTYPE taglib public
"-//sun Microsystems, Inc.//dtd JSP Tag Library 1.1//en"
"http://java.sun.com/j2ee/dtds/ Web-jsptaglibrary_1_1.dtd ">
<taglib>
<tlibversion>1.0</tlibversion>
< jspversion>1.1</jspversion>
<shortname>tagclass</shortname>
<tag>
< name>login</name>
<tagclass>
tagclass.login.login
</tagclass>
< info>
</info>
<attribute>
<name>height</name>
<required> true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
< attribute>
<name>width</name>
<required>true</required>
< rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
Tag handler: Login.java
Package tagclass.login;
Import Javax.servlet.jsp.tagext.TagSupport;
Import javax.servlet.jsp.*;
Import java.io.*;
public class Login extends TagSupport
{public
login ()
{
super ();
}
public int doStartTag () throws Jsptagexception
{
JspWriter out = Pagecontext.getout ();
Try
{
out.println ("<applet codebase=applet/login/code=login.class width=200 height=100 > </ Applet> ");
}
catch (Exception e)
{
} return
skip_body;
}
PUBLICC int Doendtag () throws Jsptagexception
{return
eval_page;
}
public void release ()
{
super.release ();
}
public void SetWidth (String language)
{
this.width = width;
}
Public String getwidth ()
{return
this.width;
}
public void SetHeight (String height)
{
this.height=height
}
Public String getheight ()
{return
this.height;
}
Private String width;
private String height;
}
Applet:login.java used in the tag handler
Import java.awt.*;
Import java.awt.event.*;
Import java.applet.*; public class Login extends Applet implements ActionListener {private string s_username; private string S_userpassword; pr
Ivate Button B_OK;
Private Button B_register;
Private Label L_username;
Private Label L_userpassword;
Private TextField T_username;
Private TextField T_userpassword;
Private GridLayout g_gridlayout; public void init () {b_ok=new button ("OK"), B_register=new button ("register"), l_username= new Label ("name"); L_USERPASSW
Ord=new Label ("password");
T_username=new TextField ();
T_userpassword=new TextField ();
B_ok.addactionlistener (this);
B_register.addactionlistener (this);
G_gridlayout=new GridLayout (3,2,10,10);
This.setlayout (g_gridlayout);
This.setbackground (Color.Blue);
Add (L_username);
Add (T_username);
Add (L_userpassword);
Add (T_userpassword);
Add (B_OK);
Add (B_register); public void actionperformed (ActionEvent ev) {String s_label=ev.getactioncommand (); if (S_label.equals ("OK")) {T_usern Ame.setteXT ("name");
} if (S_label.equals ("register")) {T_userpassword.settext ("password");}
public void Paint (Graphics g) {}}
I hope this article will help you with your JSP programming.