因為在JSP中不要使用一行Java代碼,所以說必須使用標簽來移除顯示的Java代碼,下面羅列一下標簽的開發過程。
1.首先編寫一個實現tag接口的java類
package com.bird.web.tag;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
/**
* 顯示來訪者IP的自定義標簽
* @author Bird
*
*/
public class ViewIPTag extends TagSupport{
private static final long serialVersionUID = 1L;
@Override
public int doStartTag() throws JspException {
HttpServletRequest request = (HttpServletRequest) this.pageContext.getRequest();
JspWriter out = this.pageContext.getOut();
String ip = request.getRemoteAddr();
try {
out.print(ip);
} catch (IOException e) {
throw new RuntimeException(e);
}
return super.doStartTag();
}
}
package com.bird.web.tag;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
/**
* 顯示來訪者IP的自定義標簽
* @author Bird
*
*/
public class ViewIPTag extends TagSupport{
private static final long serialVersionUID = 1L;
@Override
public int doStartTag() throws JspException {
HttpServletRequest request = (HttpServletRequest) this.pageContext.getRequest();
JspWriter out = this.pageContext.getOut();
String ip = request.getRemoteAddr();
try {
out.print(ip);
} catch (IOException e) {
throw new RuntimeException(e);
}
return super.doStartTag();
}
}
2.在tld文件裡面對標簽處理器類進行描述,tld文件的位置在web-inf下面可以在tomcat的webapps下面抄一下
<?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/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>Bird</short-name>
<uri>http://www.bird.net</uri>
<tag>
<name>viewIP</name>
<tag-class>com.bird.web.tag.ViewIPTag</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
<?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/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>Bird</short-name>
<uri>http://www.bird.net</uri>
<tag>
<name>viewIP</name>
<tag-class>com.bird.web.tag.ViewIPTag</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
4.在界面裡面使用tablib命令到導入和使用標簽就可以了,
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<%@taglib uri="http://www.bird.net" prefix="bird" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>一個簡單的自定義標簽</title>
</head>
<body>
您訪問的IP為: <bird:viewIP/>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<%@taglib uri="http://www.BkJia.com" prefix="bird" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>一個簡單的自定義標簽</title>
</head>
<body>
您訪問的IP為: <bird:viewIP/>
</body>
</html>
作者 小憤青