使用标签输出客户机Ip
1编写一个实现了tag接口的类
public class ViewIpTag extends TagSupport {
public int doStartTag() throws JspException {
HttpServletRequest request= (HttpServletRequest) this.pageContext.getRequest();
JspWriter out=this.pageContext.getOut();
String ip=request.getRemoteAddr();
try {
out.println(ip);
} catch (IOException e) {
throw new RuntimeException();
}
return super.doStartTag();
}
}
2在tld文件中对标签处理器类进行描述(tld文件的位置:web-inf下)
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>JSTL 1.1 core library</description>
<display-name>JSTL core</display-name>
<tlib-version>1.1</tlib-version>
<short-name>itcast</short-name>
<uri>http://www.itcast.cn</uri>
<tag>
<name>viewip</name>
<tag-class>com.incast.web.tag.ViewIpTag</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
3在jsp页面中导入和使用自定义标签
<%@taglib uri="http://www.itcast.cn" prefix="itcast"%>
您的ip地址是: <itcast:viewip/>