A simple example of a JSP custom tag

Source: Internet
Author: User
Tags tld

Using custom tags in JSP can achieve this purpose. In fact, all the frameworks we know are basically implemented in the form of custom tags.
By using custom tags, We can display complex logic on pages with simple tags. Next we will implement a very simple custom tag. We hope to use this simple instance to bring you into the field of custom tags. The custom tag we are going to define implements the function of displaying the publishing right information on the JSP page, and we will upgrade it in future blog posts.
First, open the editor and create the following Java Code : Copy code The Code is as follows: Package com. yanzhijun;
Import java. Io .*;
Import javax. servlet. jsp .*;
Import javax. servlet. jsp. tagext .*;
Public class copyrighttag extends tagsupport
{
Public int doendtag ()
{
Try
{
String copypre = "All rights reserved by Yan Zhijun & copy2008 ";
String info = new string (copypre. getbytes (), "iso8859_1 ");
Pagecontext. getout (). println (Info );
}
Catch (ioexception e ){}
Return eval_page;
}
}

After the above Code is edited, compile and generate the class file. And put the compiled bytecode file together with the package name under the WEB-INF \ Classes directory of the Web application that is preparing to use the current tag. For example, if the web application is under the directory named test, copy all the com directories generated during the above code compilation to test \ WEB-INF \ Classes.
Note: If the above Code is not compiled in the integrated environment, for example, you can directly compile the code by executing the javac command in the command line, you need to manually add the javax package to the environment variable classpath. servlet. location of JSP, for Tomcat, the location of this package is the jsp-api.jar In the lib directory under the tomcat installation directory.
Then save the following XML format file to the file named testlib. TLD, the file testlib. TLD is stored under the directory WEB-INF \ TLDs. Copy code The Code is as follows: <? 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> yzj </shortname>
<Tag>
<Name> copyright </Name>
<Tagclass> com. yanzhijun. copyrighttag </tagclass>
<Bodycontent> Empty </bodycontent>
<Attribute/>
</Tag>
</Taglib>

So far, we have completed a custom tag, and then we can use it in the JSP file. For example, the following JSP file is available:Copy codeThe Code is as follows: <% @ taglib uri = "WEB-INF/TLDs/testlib. TLD" prefix = "yzj" %>
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> simple example of custom tags </title>
</Head>
<Body>
<P> here is the body content </P>
<Yzj: Copyright/>
</Body>
</Html>

Access the JSP file in the browser and you can see that the information "Yan Zhijun Copyright 2008" is displayed at the bottom of the page. This is exactly what we define as a custom tag.
Through the above process, you have implemented custom labels and tested them. For details about its principles, mechanisms, and precautions, please wait for the following blog post.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.