一个具体的类继承TagSupport
定义的类必须在包之下
打印一句:HelloWord!!
通过上下文获取输出流
package
com.xiaomaha.mytag;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
public class HelloWord extends TagSupport ... {
@Override
public int doStartTag() throws JspException ... {
JspWriter w = pageContext.getOut();
try ... {
w.println( " HelloWord!!! " );
} catch (IOException e) ... {
// TODO Auto-generated catch block
e.printStackTrace();
}
return super .doStartTag();
}
}
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
public class HelloWord extends TagSupport ... {
@Override
public int doStartTag() throws JspException ... {
JspWriter w = pageContext.getOut();
try ... {
w.println( " HelloWord!!! " );
} catch (IOException e) ... {
// TODO Auto-generated catch block
e.printStackTrace();
}
return super .doStartTag();
}
}
定义一个xml文件
<?
xml version="1.0" encoding="UTF-8"
?>
<! DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "web-jsptaglibrary_1_2.dtd" >
< taglib >
< tlib-version > tlib-version </ tlib-version >
< jsp-version > jsp-version </ jsp-version >
< short-name > short-name </ short-name >
< tag >
< name > hello </ name >
< tag-class > com.xiaomaha.mytag.HelloWord </ tag-class >
</ tag >
</ taglib >
<! DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "web-jsptaglibrary_1_2.dtd" >
< taglib >
< tlib-version > tlib-version </ tlib-version >
< jsp-version > jsp-version </ jsp-version >
< short-name > short-name </ short-name >
< tag >
< name > hello </ name >
< tag-class > com.xiaomaha.mytag.HelloWord </ tag-class >
</ tag >
</ taglib >
jsp调用
引用: <%@taglib uri="/WEB-INF/hello.xml" prefix="h"%>
<%
...
@ page language = " java " import = " java.util.* " pageEncoding = " UTF-8 "
%>
<% ...
String path = request.getContextPath();
String basePath = request.getScheme() + " :// " + request.getServerName() + " : " + request.getServerPort() + path + " / " ;
%>
<% ... @taglib uri = " /WEB-INF/hello.xml " prefix = " h " %>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
< html >
< head >
< base href ="<%=basePath%>" >
< title > My JSP 'index.jsp' starting page </ title >
< meta http-equiv ="pragma" content ="no-cache" >
< meta http-equiv ="cache-control" content ="no-cache" >
< meta http-equiv ="expires" content ="0" >
< meta http-equiv ="keywords" content ="keyword1,keyword2,keyword3" >
< meta http-equiv ="description" content ="This is my page" >
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</ head >
< body >
< h:hello ></ h:hello >
</ body >
</ html >
<% ...
String path = request.getContextPath();
String basePath = request.getScheme() + " :// " + request.getServerName() + " : " + request.getServerPort() + path + " / " ;
%>
<% ... @taglib uri = " /WEB-INF/hello.xml " prefix = " h " %>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
< html >
< head >
< base href ="<%=basePath%>" >
< title > My JSP 'index.jsp' starting page </ title >
< meta http-equiv ="pragma" content ="no-cache" >
< meta http-equiv ="cache-control" content ="no-cache" >
< meta http-equiv ="expires" content ="0" >
< meta http-equiv ="keywords" content ="keyword1,keyword2,keyword3" >
< meta http-equiv ="description" content ="This is my page" >
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</ head >
< body >
< h:hello ></ h:hello >
</ body >
</ html >
http:localhost:9999/TestTag