Getting Started with customizing JSP tags

Source: Internet
Author: User
Tags tld

1. Write a Java class that implements the tag interface

 packagetagdemo;Importjavax.servlet.http.HttpServletRequest;Importjavax.servlet.jsp.JspException;Importjavax.servlet.jsp.JspWriter;Importjavax.servlet.jsp.PageContext;Importjavax.servlet.jsp.tagext.Tag;Importjava.io.IOException; public classViewiptagImplementsTag {PrivatePageContext pagecontext; @Override public voidSetpagecontext (pagecontext Pagecontext) { this. PageContext =pagecontext; } @Override public voidsetParent (tag tag) {} @Override publicTag getParent () {return NULL; } @Override public intdoStartTag ()throwsjspexception {System.out.println ("call doStartTag method"); HttpServletRequest Request=(httpservletrequest) pagecontext.getrequest (); JspWriter writer=Pagecontext.getout (); String IP=request.getremoteaddr (); Try{writer.write (ip); } Catch(ioexception E) {e.printstacktrace (); }        return0; } @Override public intDoendtag ()throwsjspexception {System.out.println ("call Doendtag method"); return0; } @Override public voidrelease () {System.out.println ("call Release method"); }}
Viewiptag

2, write the TLD tag library file, the TLD file must be placed in the Web-inf folder or its sub-folder, this file is used to describe the label processor class

<?XML version= "1.0" encoding= "UTF-8"?><taglibxmlns= "http://java.sun.com/xml/ns/j2ee"Xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"version= "2.0">    <Description>Custom labels</Description>    <tlib-version>1.0</tlib-version>    <Short-name>SDT</Short-name>    <URI>/sefdefinetag</URI>    <Tag>        <Description>Output Client Address</Description>        <name>Viewiptag</name>        <Tag-class>Tagdemo.viewiptag</Tag-class>        <body-content>Empty</body-content>    </Tag></taglib>
TLD

Taglib Tag Library Label overview

Description: describing information to the tag library

Tlib-version: Tag Library version

Short-name: Tag Library Name

Uri: the tag library uniquely identifies that the JSP is referring to the tag through a uri, and the URI must be preceded/prefaced, as long as the name is unique, as in the following reference <% @taglib prefix= "sdt" uri= "/sefdefinetag"%>

Tag:taglib Tag Library can contain more than one custom label, each tag corresponds to a custom label

Description: description of the current label

Name: define a name that references the tag processor class by this name in the JSP

Tag-class: corresponding Label Processor class

Body-content: Defining the label body

3. Test in JSP file

<%@ PageContentType= "text/html;charset=utf-8"language= "java" %><% @taglibprefix= "sdt"URI= "/sefdefinetag" %><HTML><Head>    <title></title></Head><Body>IP:<Sdt:viewiptag/></Body></HTML>
JSP

JSP can be introduced into the tag library, using prefix can set the tag library alias, label use is the < tag library alias: tag name >

Getting Started with customizing JSP tags

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.