HelloWorld.java
package beans; import javax.servlet.http.*; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import java.util.*; public class HelloWorld extends TagSupport { public int doStartTag() throws JspException { try { JspWriter out = pageContext.getOut(); HttpServletResponse response = (HttpServletResponse)pageContext.getResponse(); out.write("Hello world!"); } catch(Exception e) { throw new JspException(e.getMessage()); } return EVAL_PAGE; } }
simple.tld
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"> <!-- a tab library descriptor --> <taglib xmlns="http://java.sun.com/JSP/TagLibraryDescriptor"> <tlib-version>1.0</tlib-version> <jsp-version>1.2</jsp-version> <short-name>Simple Tags</short-name> <tag> <name>HelloWorld</name> <tag-class>beans.HelloWorld</tag-class> <body-content>empty</body-content> </tag> </taglib>
web.xml
<?xml version="1.0" encoding="iso-8859-1"?> <!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> <taglib> <taglib-uri> http://www.java2s.com/custom </taglib-uri> <taglib-location> /WEB-INF/tags/simple.tld </taglib-location> </taglib> <context-param> <param-name>javax.servlet.jsp.jstl.fmt.timeZone</param-name> <param-value>US/Central</param-value> </context-param> <context-param> <param-name>database-driver</param-name> <param-value>org.gjt.mm.mysql.Driver</param-value> </context-param> <context-param> <param-name>database-url</param-name> <param-value> jdbc:mysql://localhost/forum?user=forumuser</param-value> </context-param> <taglib> <taglib-uri>http://java.sun.com/jstl/fmt</taglib-uri> <taglib-location>/WEB-INF/fmt.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/fmt-rt</taglib-uri> <taglib-location>/WEB-INF/fmt-rt.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/core</taglib-uri> <taglib-location>/WEB-INF/c.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/core-rt</taglib-uri> <taglib-location>/WEB-INF/c-rt.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/sql</taglib-uri> <taglib-location>/WEB-INF/sql.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/sql-rt</taglib-uri> <taglib-location>/WEB-INF/sql-rt.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/x</taglib-uri> <taglib-location>/WEB-INF/x.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/x-rt</taglib-uri> <taglib-location>/WEB-INF/x-rt.tld</taglib-location> </taglib> <taglib> <taglib-uri> http://java.jeffheaton.com/taglib/jstl/forum</taglib-uri> <taglib-location>/WEB-INF/forum.tld</taglib-location> </taglib> <taglib> <taglib-uri>/tlt</taglib-uri> <taglib-location>/WEB-INF/taglib.tld</taglib-location> </taglib> </web-app>
Jsp page with custom tag
<%@ taglib uri='http://www.java2s.com/custom' prefix='hw' %> <html> <body> <hw:HelloWorld /> </body> </html>
23.58.Custom Tag | ||||
23.58.1. | Custom Tag Support | ![]() | ||
23.58.2. | Custom Tag For Outputing Message To Console | ![]() | ||
23.58.3. | Custom Tag For Iteration | ![]() | ||
23.58.4. | Jars For Custom Tag Development | ![]() | ||
23.58.5. | Custom Tag With Property | ![]() | ||
23.58.6. | Custom Tag Web XML Tag Lib | ![]() | ||
23.58.7. | Custom Tag To Insert Text | ![]() | ||
23.58.8. | Custom Tag To Get Parameter From PageContext | ![]() | ||
23.58.9. | Extends Tag Support DoEndTag | ![]() | ||
23.58.10. | Reference Tag Using Tag Dir | ![]() | ||
23.58.11. | Repeat Tag | ![]() | ||
23.58.12. | Custom Tag For Shuffling Color | ![]() | ||
23.58.13. | Custom Tag For Display message | ![]() | ||
23.58.14. | Custom Tag Without TLD | ![]() | ||
23.58.15. | Custom Tag With Attributes | ![]() | ||
23.58.16. | Custom Tag Web URI Definition | ![]() | ||
23.58.17. | Custom Tag Using Beans | ![]() | ||
23.58.18. | Custom Tag Setup Environment | ![]() | ||
23.58.19. | Custom Tag Set Bean Value | ![]() | ||
23.58.20. | Custom Tag Dynamic Attributes | ![]() | ||
23.58.21. | Custom Tag As Template | ![]() | ||
23.58.22. | Custom Tag Accepting Fragments | ![]() |