Your own simple JSP tag : Tag « JSP « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » JSP » TagScreenshots 
Your own simple JSP tag


//java2sYourOwnJSPTag.war

// add to web.xml
    <taglib>
        <taglib-uri>http://java.sun.com/jstl/java2s</taglib-uri>
        <taglib-location>/WEB-INF/java2s.tld</taglib-location>
    </taglib>
// create new java2s.tld file in WEB-INF directory
//File:java2s.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>Java2s Simple Tags</short-name>

  <tag>
    <name>HelloWorldTag</name>
    <tag-class>com.java2s.HelloWorldTag</tag-class>
    <body-content>empty</body-content>
  </tag>
</taglib>

// create HelloWorldTag.java under WEB-INF\classes\com\java2s\
// include jsp-api.jar into your CLASSPATH
// and compile it
package com.java2s;

import javax.servlet.http.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.util.*;

public class HelloWorldTag extends TagSupport
{
    public int doStartTag() throws JspException
    {
        try
        {
            JspWriter out = pageContext.getOut();
            HttpServletResponse response = (HttpServletResponse)pageContext.getResponse();
            out.write("Hello world from www.java2s.com!");
        }
        catch(Exception e)
        {   
            throw new JspException(e.getMessage());
        }
        return EVAL_PAGE;
    }
}


// create JSP file: HelloWorldTag.jsp
<%@ taglib uri='WEB-INF/java2s.tld' prefix='hw' %>
<!-- the HellowWorld must be consistent with the tag name in *.tld-->
<html>
  <body>
    <hw:HelloWorldTag />
  </body>
</html>

// start tomcat and load the JSP file in the browser


           
       
Related examples in the same category
1.Create your own tag: a custom tag body
2.A custom tag that has neither attributes nor body content.
3.A custom tag: empty with attributes
4.A custom tag: scripting variable
5.Write your own tag
6.Logo Tag
7.A custom tag: empty
8.Tag lifecycle with Attribute
9.A custom tag: iteration
10.JSP Simple Tags
11.JSP tag: advanced tagsJSP tag: advanced tags
12.JSP classic tagsJSP classic tags
13.JSP Tag Libraries and JSTLJSP Tag Libraries and JSTL
14.JSP Directives: HTML tag
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.