给你一个最简单且完整的例子,来自《Professional JSP Tag Libraries》
// DateTag.java
package com.liad.jsptaglibs.date;import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;/**
 * A custom tag that displays the current date.
 *
 * @author    Simon Brown
 */
public class DateTag extends TagSupport {  /**
   * Called when the starting tag is encountered.
   */
  public int doStartTag() throws JspException {    // get the locale of the client and create an appropriate date format
    Locale loc = pageContext.getRequest().getLocale();
    DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, loc);    try {
      // and write out the formatted date to the page
      pageContext.getOut().write(df.format(new Date()));
    } catch (java.io.IOException ioe) {
      throw new JspTagException(ioe.getMessage());
    }    // and skip evaluating the body of the tag (as there shouldn't be one)
    return SKIP_PAGE;
  }}simple.tld
==========
<?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>simple</short-name>
  <description>Examples for the Simple Tags chapter</description>
  
  <tag> 
    <name>date</name> 
    <tag-class>com.liad.jsptaglibs.date.DateTag</tag-class>
    <body-content>empty</body-content>
    <description>Inserts the current date into the page</description> 
  </tag>
  
</taglib>web.xml
=======
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN' 'http://java.sun.com/dtd/web-app_2_3.dtd'><web-app>
  <display-name>Professional JSP Tag Libraries</display-name>
  <taglib>
    <taglib-uri>/simpleTaglib</taglib-uri>
    <taglib-location>/WEB-INF/tlds/simple.tld</taglib-location>
  </taglib>
</web-app>datetag.jsp
===========
<html>  <head>
    <title>Professional JSP Tag Libraries</title>
  </head>  <%@ taglib uri="/simpleTaglib" prefix="simple" %>  <body>    <h1>The date is <simple:date/></h1>  </body></html>用dos命令tree /f获得的目录结构是
tagtest
│  datetag.jsp

└─WEB-INF
    │  web.xml
    │
    ├─classes
    │  └─com
    │      └─liad
    │          └─jsptaglibs
    │              └─date
    │                      DateTag.class
    │
    └─tlds
            simple.tld你的问题可能是没有配置好web.xml

解决方案 »

  1.   

    我回去尝试下哦,如果真的可以解决,马上结贴.现在这个问题到不常出现了,可是在ie里显示
    ype Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception org.apache.jasper.JasperException: Unable to compile class for JSPAn error occurred at line: -1 in the jsp file: nullGenerated servlet error:
        [javac] Since fork is true, ignoring compiler setting.
        [javac] Compiling 1 source file
        [javac] Since fork is true, ignoring compiler setting.
        [javac] D:\softwares\Tomcat 4.1\work\Standalone\localhost\_\TagTest_jsp.java:80: cannot resolve symbol
        [javac] symbol  : class HelloTag 
        [javac] location: class org.apache.jsp.TagTest_jsp
        [javac]     HelloTag _jspx_th_test_hello_0 = (HelloTag) _jspx_tagPool_test_hello_name.get(HelloTag.class);
        [javac]     ^
        [javac] D:\softwares\Tomcat 4.1\work\Standalone\localhost\_\TagTest_jsp.java:80: cannot resolve symbol
        [javac] symbol  : class HelloTag 
        [javac] location: class org.apache.jsp.TagTest_jsp
        [javac]     HelloTag _jspx_th_test_hello_0 = (HelloTag) _jspx_tagPool_test_hello_name.get(HelloTag.class);
        [javac]                                       ^
        [javac] D:\softwares\Tomcat 4.1\work\Standalone\localhost\_\TagTest_jsp.java:80: cannot resolve symbol
        [javac] symbol  : class HelloTag 
        [javac] location: class org.apache.jsp.TagTest_jsp
        [javac]     HelloTag _jspx_th_test_hello_0 = (HelloTag) _jspx_tagPool_test_hello_name.get(HelloTag.class);
        [javac]                                                                                   ^
        [javac] 3 errors
    这是为什么啊.
      

  2.   

    你需要在
    /Tomcat 4.1/webapps/ROOT/WEB-INF/web.xml
    里加入以下定义
    <taglib>
      <taglib-uri>/mylib</taglib-uri>
      <taglib-location>/WEB-INF/tlds/mylib.tld</taglib-location>
    </taglib>
    然后在jsp里就可以
    <%@taglib uri="/mylib" prefix="test"%>
    来使用自定义标记库了
      

  3.   

    不行啊,诸位,按照大家说的我在web.xml中增加了那两句话,可是居然连Tomcat都出错了(我可以保证是那两句话引起的,因为如果去掉的话tomcat又正常了)而出错是在运行startup.bat的时候发生的错误如下:
    Apache Tomcat/4.1.24
    2003-10-16 4:57:50 org.apache.commons.digester.Digester error
    严重: Parse Error at line 26 column 11: The content of element type "web-app" must match "(icon?,display-name?,description?,distributable?,context-param*,filter
    *,filter-mapping*,listener*,servlet*,servlet-mapping*,session-config?,mime-mappi
    ng*,welcome-file-list?,error-page*,taglib*,resource-env-ref*,resource-ref*,secur
    ity-constraint*,login-config?,security-role*,env-entry*,ejb-ref*,ejb-local-ref*)
    ".
    org.xml.sax.SAXParseException: The content of element type "web-app" must match
    "(icon?,display-name?,description?,distributable?,context-param*,filter*,filter-
    mapping*,listener*,servlet*,servlet-mapping*,session-config?,mime-mapping*,welco
    me-file-list?,error-page*,taglib*,resource-env-ref*,resource-ref*,security-const
    raint*,login-config?,security-role*,env-entry*,ejb-ref*,ejb-local-ref*)".
            at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Un
    known Source)
    我的web.xml内容如下:
    web.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
    <web-app>  <servlet>
        <servlet-name>helloworld</servlet-name>
        <servlet-class>simple.HelloWorld</servlet-class>
      </servlet>
      <servlet>
        <servlet-name>simple</servlet-name>
        <servlet-class>simple.Simple</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>helloworld</servlet-name>
        <url-pattern>/helloworld</url-pattern>
      </servlet-mapping>
      <servlet-mapping>
        <servlet-name>simple</servlet-name>
        <url-pattern>/simple</url-pattern>
      </servlet-mapping>
      <display-name>final encounter</display-name>
      <taglib>
         <taglib-uri>/mylib</taglib-uri>
         <taglib-location>/WEB-INF/tlds/mylib.tld</taglib-location>
       </taglib>
    </web-app>
    后来我索性把前面的那些servlet有关的全部去掉就剩下<web-app><taglib>这两组标签了,这时候tomcat是没有错了,可是我无法访问http://localhost:8080/index.jsp错误如下:
    The requested resource (/index.jsp) is not available. 平时可是对的啊,这究竟是为什么呢?
      

  4.   

    你的
    <display-name>final encounter</display-name>
    要紧跟在<web-app>后面<web-app>里面的元素其实是按照字母顺序排列的,详细请看http://java.sun.com/dtd/web-app_2_3.dtd
      

  5.   

    那taglib应该放在那个<servlet-mapping>对之后咯?
      

  6.   

    不行啊:( tomcat还是不能起来
    <web-app>
    <display-name>final encounter</display-name>
      <servlet>
        <servlet-name>helloworld</servlet-name>
        <servlet-class>simple.HelloWorld</servlet-class>
      </servlet>
      <servlet>
        <servlet-name>simple</servlet-name>
        <servlet-class>simple.Simple</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>helloworld</servlet-name>
        <url-pattern>/helloworld</url-pattern>
      </servlet-mapping>
      <servlet-mapping>
        <servlet-name>simple</servlet-name>
        <url-pattern>/simple</url-pattern>
      </servlet-mapping>
      <taglib>
         <taglib-uri>/mylib</taglib-uri>
         <taglib-location>/WEB-INF/tlds/mylib.tld</taglib-location>
       </taglib>
    </web-app>
    我现在是按照字母顺序了啊,怎么还是不行呢