您的位置:首页 > Web前端 > JavaScript

jsp自定义标签

2014-04-25 00:16 211 查看
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代码
[java] view plaincopy



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,控制标签内的文本是否输出[java] view plaincopy



public class TagDemo extends TagSupport {  
  
    @Override  
    public int doStartTag() throws JspException {  
          
          
        return Tag.EVAL_BODY_INCLUDE;//输出,不输出SKIP_TAGE  
    }  
  
}  

2,控制标签后面的文本是否输出[java] view plaincopy



public class TagDemo2 extends TagSupport {  
  
    @Override  
    public int doEndTag() throws JspException {  
          
        return Tag.EVAL_PAGE;  
    }  
  
}  

3,控制标签体内的文本循环输出[java] view plaincopy



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,修改标签体内的文件[java] view plaincopy



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文件

[html] view
plaincopy





<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>

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: