简单的自定义标志 只输出hello world

package bq;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;

public class BQ implements Tag {
private PageContext pagecontext;

private Tag tag;

public int doEndTag() throws JspException {
// TODO Auto-generated method stub
try {
pagecontext.getOut().print("hello world");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return tag.EVAL_PAGE;
}

public int doStartTag() throws JspException {
// TODO Auto-generated method stub
return tag.SKIP_BODY;
}

public Tag getParent() {
// TODO Auto-generated method stub
return null;
}

public void release() {
// TODO Auto-generated method stub

}

public void setPageContext(PageContext arg0) {
// TODO Auto-generated method stub
this.pagecontext = arg0;
}

public void setParent(Tag arg0) {
// TODO Auto-generated method stub
this.tag = arg0;

}

}

bq.tld

<?xml version="1.0" encoding="UTF-8"?>
<!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>tagSimple</short-name>
<uri>/hellotag</uri>
<tag>
<name>hello</name>
<tag-class>bq.BQ</tag-class>
<body-content>jsp</body-content>
</tag>
</taglib>

MyJsp.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title></title>
<%@taglib uri="/WEB-INF/bq.tld" prefix="mytag"%>
</head>

<body>
<mytag:hello />
<mytag:hello>nihao</mytag:hello>
</body>
</html>

你可能感兴趣的:(java,Web,jsp,servlet,sun)