package com.itheima.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;
public class ViewIPTag extends TagSupport {
@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();
}
return super.doStartTag();
}
}
0,移除jsp中的java代码
public class ViewIPTag extends TagSupport {
@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();
}
return super.doStartTag();
}
}
1,控制标签内的文本是否输出
- public class TagDemo extends TagSupport {
-
- @Override
- public int doStartTag() throws JspException {
-
-
- return Tag.EVAL_BODY_INCLUDE;//输出,不输出SKIP_TAGE
- }
-
- }
2,控制标签后面的文本是否输出
- public class TagDemo2 extends TagSupport {
-
- @Override
- public int doEndTag() throws JspException {
-
- return Tag.EVAL_PAGE;
- }
-
- }
3,控制标签体内的文本循环输出
- public class TagDemo3 extends TagSupport {
- static int num = 0;
-
- @Override
- public int doStartTag() throws JspException {
- // TODO Auto-generated method stub
- return Tag.EVAL_BODY_INCLUDE;
- }
-
- @Override
- public int doAfterBody() throws JspException {
- if(++num<5){
- return IterationTag.EVAL_BODY_AGAIN;
- }else{
- return IterationTag.SKIP_BODY;
- }
-
- }
-
- }
4,修改标签体内的文件
- public class TagDemo4 extends SimpleTagSupport {
-
- @Override
- public void doTag() throws JspException, IOException {
- JspFragment jf = this.getJspBody();
- StringWriter writer = new StringWriter();
- jf.invoke(writer);
- String str = writer.getBuffer().toString().toUpperCase();
- this.getJspContext().getOut().write(str);
-
- }
5,防盗链
<itheima:refererDemo site="http://localhost" page="/day11/index.jsp" />
public class refererDemo extends SimpleTagSupport {
private String site;
private String page;
public void setSite(String site) {
this.site = site;
}
public void setPage(String page) {
this.page = page;
}
@Override
public void doTag() throws JspException, IOException {
PageContext context = (PageContext) this.getJspContext();
HttpServletRequest request = (HttpServletRequest) context.getRequest();
HttpServletResponse response = (HttpServletResponse) context.getResponse();
String referer = request.getHeader("referer");
if(referer !=null && referer.startsWith(site)){
//request.getRequestDispatcher("/7.jsp");
}else{
response.sendRedirect(page);
}
}
}
tld文件
- <tag>
- <name>ViewIP</name>
- <tag-class>com.itheima.web.tag.ViewIPTag</tag-class>
- <body-content>empty</body-content>
- </tag>
- <tag>
- <name>TagDemo</name>
- <tag-class>com.itheima.web.tag.TagDemo</tag-class>
- <body-content>JSP</body-content>
- </tag>
- <tag>
- <name>TagDemo3</name>
- <tag-class>com.itheima.web.tag.TagDemo3</tag-class>
- <body-content>JSP</body-content>
- </tag>
- <tag>
- <name>TagDemo4</name>
- <tag-class>com.itheima.web.tag.TagDemo4</tag-class>
- <body-content>scriptless</body-content>
- </tag>
- <tag>
- <name>TagDemo2</name>
- <tag-class>com.itheima.web.tag.TagDemo2</tag-class>
- <body-content>empty</body-content>
- </tag>
- <tag>
<name>refererDemo</name>
<tag-class>com.itheima.web.tag.referer.refererDemo</tag-class>
<body-content>empty</body-content>
<attribute>
<name>site</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>page</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>