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>
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>
相关文章推荐
- 第十四章 自定义JSP标签(书摘)
- jsp自定义标签
- JSP自定义分页标签TAG
- Jsp中的自定义标签由浅到深详细讲解1
- JSP自定义标签开发入门实例
- JSP自定义select标签 字典表数据
- JSP自定义标签开发入门
- JSP自定义标签开发入门
- 整理一份jsp自定义标签以及权限控制标签
- Java - 使用标签库有什么好处?如何自定义JSP标签?
- JSP自定义标签
- JSP第六篇【自定义标签之传统标签】
- 学会怎样使用Jsp 内置标签、jstl标签库及自定义标签
- JSP自定义标签开发入门
- Java jsp 自定义标签
- 自定义标签,时间戳与日期时间转换 jsp页面
- 利用 JSP 2 提供的 SimpleTagSupport 开发自定义标签
- 自定义jsp标签
- 关于jsp页面自定义jstl标签简单项目配置
- 【JavaWeb探究】自定义JSP标签(三)