JSP custom tags Getting Started instance

Source: Internet
Author: User
Tags config tld


JSP tags replace Java programs in JSP and can be reused to facilitate web designers who are unfamiliar with Java programming.

First create a new Dynamic Web project under Eclipse


1, join
before </web-app>

<jsp-config>


<taglib>


<taglib-uri>/tld/helloworld</taglib-uri>


<taglib-location>/WEB-INF/tlds/helloworld.tld</taglib-location>


</taglib>


</jsp-config>


2. Under Web-inf, create a new TLDs directory, under TLDs new helloworld.tld.helloworld.tld content for





<?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. New Class Helloworldtag.java
under MyTag package of SRC







package mytag;


   }


    


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





import java.io.IOException;





import javax.servlet.jsp.JspTagException;


import Javax.servlet.jsp.tagext.TagSupport;





public class Helloworldtag extends TagSupport ... { public helloworldtag () ...


<%...@ page language= "java" contenttype= "text/html; charset=gb2312"


< pageencoding= "gb2312"%>


<%...@ page import= "java.util.*"%>


<%...@ page import= "jstl.*%> " <%...@ taglib prefix= "MyTag" uri= "/tld/helloworld"%>< /c8>


<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >








<meta http-equiv= "Content-type" content= "text/html; charset=gb2312" >


<title>insert title here</title>





<body>





<br>


<br>


<mytag:helloworld></mytag:helloworld>


<br>


</form>


</body>








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.