Use of custom markers in JSP
Summary
There is a mechanism in JSP that allows you to insert a mark similar to HTML in the JSP page. This article describes the basic concepts and compositions of JSP custom tags, and how to develop and apply JSP custom tags.
Keyword
JSP, XML, TLD, Marking
What is a tag?
We can edit our web page using HTML language:
Hello World
Title>
HEAD>
Hello World
Body>
Html>
Here we call head>,
<% @ Taglib Uri = "/ TLDS / Taglib.tld" prefix = "tagclass"%>
hEAD>
tagclass: login>
body>
html>
In the above example tagclass: login> is a JSP custom marker. Widtht, Height is the properties of this tag. <% @ Taglib URI = "/ TLDS / Taglib.tld" prefix = "tagclass"%> is a tag library definition instruction, which we will discuss later. The tag is customized in JSP, which is essentially in the form of a markup, a separate functionality Java class. The use of tags reduces Java code directly embedded in JSP page, which is convenient for the layout of the page and facilitates the reuse of the code, and improves the efficiency of development.
JSP server resolves the process of tagging
So how does the JSP server parsed this tag when a tag is embedded in the JSP page? Let's take a look at it together:
The meaning of each object in the figure is as follows:
Client: Indicates the client.
JSP-Server: JSP server.
JSP-PAGE: JSP page.
TLD: Tag library description file, define various attributes and processing files of tags and tags.
Tagclass tag handler
When a user accesses a JSP page, this request is sent to the JSP server, and the JSP server calls the corresponding page according to this request. If there is a custom tag in this page, the JSP service will be based on page instructions <% @ taglib > To access TLD to get information about the handler, then call the constructor method of the handler, start the tag handler, and read the attributes of the tag and the corresponding value. Call the corresponding set method for each of the properties. When the marker is used for the first time, it will not be set, so the set method is called for each attribute. After the property is set, the JSP server calls the dostartTAG () of the handler (), and then call the DOENDTAG () method. Finally, the JSP server will continue to process the remaining pages, call the Release () method to clean up all the resources occupied at the end of the page. TLD file
TLD (TLD: TAG LIBRARY Descriptor Tag Library Descriptor) File, the standard XML format tag definition file, is used to store information about the logger, and below is a typical TLD file.
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">
Tagclass.login.login
tagclass>
info>
attribute>
attribute>
tag>
taglib>
In this TLD file, only one tagger library is defined, which calls an applet to verify the legality of the user. Treating this tag is tagclass.login.login. Width, Height is two properties of this tag. The attribute is the value sent as a parameter when using the tag. We can add several tags in the examples, or you can add several properties for each tag. When we develop marker libraries, it is not necessarily to start from scratch, write a new TLD. We can use an integrated environment or modify the above example. Taglib instruction
Then how do it define a tag library when the JSP server is parsing a tagger? This is the main responsibility of the Taglib Directive.
Taglib instruction
Define a tag library and its custom tag.
JSP syntax
<% @ Taglib Uri = "Uritotaglibrary" prefix = "tagprefix"%>
Example <% @ Taglib URI = "/ TLDS / Taglib.tld" prefix = "tagclass"%>
hEAD>
tagclass: login>
body>
html>
description
<% @ taglib%> The directive declares that this JSP file uses a custom tag, and references the tag library.
It also specifies the prefix of their tags. You must use <% @ taglib%> instruction before using custom tags.
Attributes
URI = "URITOTAGLIBRARY": UNIFORM Resource Identifier (URI) The unique naming of the custom tag according to the prefix of the tag, the URI can be a relative or absolute path. Prefix = "tagprefix": Prefixed before the custom tag. As in the above example, tagclass: login>
Tag Handle's Tag Handle
We still take an example to see how to implement a Tag Handle. The first is to look at its class diagram:
Let's take another look at it:
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.getut ();
Try
{
Out.println ("
}
Catch (Exception E)
{
}
Return Skip_body;
}
Publicc Int Doretag () 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)
{
THISHEIGHT = HEIGHT;
}
Public string getHeight ()
{
Return this.height;
}
PRIVATE STRING WIDTH;
PRIVATE STRING HEIGHT;
}
From the above we can see that there are several requirements for achieving a simple tag handler: 1 Add a class to inherit the java.Servlet.jsp.tagext.tagsupport class. This class provides all methods required by java.servlet.jsp.tagext.tag interface. In addition, some basic APIs need to be used to allow JSP containers to call our own marker handlers. 2 You must create a get
Skip_body implies 0: skip the code between the start and end tags.
Eval_body_include implicit 1: output the content of the body to the existing output stream
Skip_page hidden 5: Ignore the remaining pages.
EVAL_PAGE implies 6: Continue to perform the following page
Of course, the marker also has its own shortcomings. It is very inconvenient package process, limited function. For some non-complex and functional logical descriptions, the parameters required to pass are not high, and the JSP tag is used to easily. For most commercial logic applications, it is also better to use Beans, and it is also suitable for servlet control.
Appendix: Complete code for the example used in the article
JSP code: login.jsp
<% @ Taglib Uri = "/ TLDS / Taglib.tld" prefix = "tagclass"%>
hEAD>
tagclass: login>
body>
html>
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">
Tagclass.login.login
tagclass>
info>
attribute>
attribute>
tag>
taglib>
Markup 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.getut ();
Try
{
Out.println ("
}
Catch (Exception E)
{
}
Return Skip_body;
}
Publicc Int Doretag () 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)
{
THISHEIGHT = 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;
Private 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_userpassword = 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_Username.Settext ("Name");
}
IF (s_label.equals ("register")))
{
t_userpassword.settext ("password");
}
}
Public void Paint (Graphics G)
{
}
}