JSP custom tag entry instance

Source: Internet
Author: User
Tags tld

JSP labels replace Java programs in JSP and can be reused to facilitate web designers who are not familiar with Java programming.


First, create a dynamic web project under eclipse
1. Before </Web-app>, add
<JSP-config>
<Taglib>
<Taglib-Uri>/TLD/helloworld </taglib-Uri>
<Taglib-location>/WEB-INF/TLDs/helloworld. TLD </taglib-location>
</Taglib>
</JSP-config>
2. Create a TLDs directory under the WEB-INF and create helloworld. TLD. helloworld. TLD under TLDs

<? XML version = "1.0" encoding = "ISO-8859-1"?>

<! Doctype taglib public "-// Sun Microsystems, Inc. // dtd jsp tag library 1.2 // en"

Http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd>

<Taglib>
<Tlib-version> 1.0 </tlib-version>
<JSP-version> 1.2 </JSP-version>
<Short-Name> mytag </short-Name>
<Tag>
<Name> helloworld </Name>
<Tag-class> mytag. helloworldtag </Tag-class>
<Body-content> Empty </body-content>
</Tag>
</Taglib>


3. Create the class helloworldtag. javapackage mytag under the SRC mytag package;

Import java. Io. ioexception;

Import javax. servlet. jsp. jsptagexception;
Import javax. servlet. jsp. tagext. tagsupport;

Public class helloworldtag extends tagsupport ...{

Public helloworldtag ()...{

}

Public int dostarttag () throws jsptagexception ...{
Return eval_body_include;
}

Public int doendtag () throws jsptagexception ...{
Try ...{
Pagecontext. getout (). Write ("this is a JSP tag test! ");
} Catch (ioexception ex )...{
Throw new jsptagexception ("error ");
}

Return eval_page;
}

}

Test page hello. jsp <%... @ page Language = "Java" contenttype = "text/html; charset = gb2312"
Pageencoding = "gb2312" %>
<%... @ Page import = "Java. util. *" %>
<%... @ Page import = "jstl. *" %>
<%... @ Taglib prefix = "mytag" uri = "/TLD/helloworld" %>
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> insert title here </title>
</Head>
<Body>

<H1> custom tag: <Br>
<Br>
<Mytag: helloworld> </mytag: helloworld>
<Br>
</Form>
</Body>
</Html>

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.