jsp自定义标签

来源:互联网 发布:c语言可视化编程ide 编辑:程序博客网 时间:2024/05/17 06:15

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 {@Overridepublic 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在CODE上查看代码片派生到我的代码片
  1. public class ViewIPTag extends TagSupport {  
  2.   
  3.     @Override  
  4.     public int doStartTag() throws JspException {  
  5.         HttpServletRequest request = (HttpServletRequest) this.pageContext.getRequest();  
  6.         JspWriter out = this.pageContext.getOut();  
  7.           
  8.         String ip = request.getRemoteAddr();  
  9.         try {  
  10.             out.print(ip);  
  11.         } catch (IOException e) {  
  12.             throw new RuntimeException();  
  13.         }  
  14.         return super.doStartTag();  
  15.     }  
  16.       
  17. }  


1,控制标签内的文本是否输出

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class TagDemo extends TagSupport {  
  2.   
  3.     @Override  
  4.     public int doStartTag() throws JspException {  
  5.           
  6.           
  7.         return Tag.EVAL_BODY_INCLUDE;//输出,不输出SKIP_TAGE  
  8.     }  
  9.   
  10. }  


2,控制标签后面的文本是否输出

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class TagDemo2 extends TagSupport {  
  2.   
  3.     @Override  
  4.     public int doEndTag() throws JspException {  
  5.           
  6.         return Tag.EVAL_PAGE;  
  7.     }  
  8.   
  9. }  


3,控制标签体内的文本循环输出

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class TagDemo3 extends TagSupport {  
  2.     static int num = 0;  
  3.       
  4.     @Override  
  5.     public int doStartTag() throws JspException {  
  6.         // TODO Auto-generated method stub  
  7.         return Tag.EVAL_BODY_INCLUDE;  
  8.     }  
  9.   
  10.     @Override  
  11.     public int doAfterBody() throws JspException {  
  12.         if(++num<5){  
  13.             return IterationTag.EVAL_BODY_AGAIN;  
  14.         }else{  
  15.             return IterationTag.SKIP_BODY;  
  16.         }  
  17.           
  18.     }  
  19.   
  20. }  


4,修改标签体内的文件

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class TagDemo4 extends SimpleTagSupport {  
  2.   
  3.     @Override  
  4.     public void doTag() throws JspException, IOException {  
  5.         JspFragment jf = this.getJspBody();  
  6.         StringWriter writer = new StringWriter();  
  7.         jf.invoke(writer);  
  8.         String str = writer.getBuffer().toString().toUpperCase();  
  9.         this.getJspContext().getOut().write(str);  
  10.           
  11.     }  
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;}@Overridepublic 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在CODE上查看代码片派生到我的代码片
  1. <tag>  
  2.   
  3.     <name>ViewIP</name>  
  4.     <tag-class>com.itheima.web.tag.ViewIPTag</tag-class>  
  5.     <body-content>empty</body-content>  
  6. </tag>  
  7. <tag>  
  8.   
  9.     <name>TagDemo</name>  
  10.     <tag-class>com.itheima.web.tag.TagDemo</tag-class>  
  11.     <body-content>JSP</body-content>  
  12. </tag>  
  13. <tag>  
  14.   
  15.     <name>TagDemo3</name>  
  16.     <tag-class>com.itheima.web.tag.TagDemo3</tag-class>  
  17.     <body-content>JSP</body-content>  
  18. </tag>  
  19. <tag>  
  20.   
  21.     <name>TagDemo4</name>  
  22.     <tag-class>com.itheima.web.tag.TagDemo4</tag-class>  
  23.     <body-content>scriptless</body-content>  
  24. </tag>  
  25. <tag>  
  26.   
  27.     <name>TagDemo2</name>  
  28.     <tag-class>com.itheima.web.tag.TagDemo2</tag-class>  
  29.     <body-content>empty</body-content>  
  30. </tag> 
  31. <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>
  32.  

0 0
原创粉丝点击