1、编写一个实现tag接口的标签处理器类
import java.io.ioexception;
import javax.servlet.http.httpservletrequest;
import javax.servlet.jsp.jspexception;
import javax.servlet.jsp.jspwriter;
import javax.servlet.jsp.pagecontext;
import javax.servlet.jsp.tagext.tag;
public class viewiptag implements tag {
private pagecontext pagecontext;
public int dostarttag() throws jspexception {
httpservletrequest request = (httpservletrequest) pagecontext.getrequest();//获取页面servlet中 request 和out 对象
jspwriter out = pagecontext.getout();
string ip = request.getremoteaddr(); //获取用户ip地址
try {
out.write(ip);
} catch (ioexception e) {
throw new runtimeexception(e);
}
return 0;
}
public int doendtag() throws jspexception {
return 0;
}
public tag getparent() {
return null;
}
public void release() {
}
public void setpagecontext(pagecontext arg0) {
this.pagecontext = arg0;//pagecontext获取用户request out 等对象
}
public void setparent(tag arg0) {
}
}
<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/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>a tag library exercising simpletag handlers.</description>
<tlib-version>1.0</tlib-version>
<short-name>simpletaglibrary</short-name>
<uri>/itcast</uri>
<tag>
<name>viewip</name> <!-- 为标签处理器类配一个标签名 -->
<tag-class>cn.itcast.web.tag.viewiptag</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
3、在jsp页面中导入并使用自定义标签
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
<head>
<title>输出客户机的ip</title>
</head>
<body>
您的ip是:<itcast:viewip/>
</body>
</html>
如对本文有疑问, 点击进行留言回复!!
SpringBoot引用阿里easyexcel,Excel导出返回浏览器下载
HashMap、Hashtable、ConcurrentHashMap三者间的异同
解决RecycleView 中Item包含Edittext时,滑动view复用导致数据错乱的问题
多线程、同步工作原理、死锁案例、Lock接口、线程的生命周期的讲解及实现
网友评论