自定义标签实现过滤敏感字符

本文介绍了一个简单的网页应用,通过用户输入的留言内容,利用Java Servlet进行数据处理,并使用自定义标签实现留言内容的过滤与展示。

1.主页

 

<body>

   <form action="NoteServlet" method="post">

     Note:<textarea rows="10" cols="20"name="notes"></textarea>

       <input type="submit">

   </form>

</body>

2.Servlet

public class NoteServlet extends HttpServlet {

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException {

        String notes= request.getParameter("notes");

        System.err.println("向数据库保存了数据。。。"+notes);//模拟保存到数据库

        //response.getWriter().print(notes);

        request.setAttribute("notes",notes);

        request.getRequestDispatcher("/jsps/c.jsp").forward(request,response);

    }

}

3.自定义标签类

 

publicclassOutTag extendsSimpleTagSupport {

    private String value;

    List<String>list= newArrayList<String>();

    publicvoid setValue(String value){

        this.value = value;

        list.add("SB");

        list.add("沙比");

    }

    @Override

    publicvoid doTag() throws JspException,IOException {

        for(String s:list){

            value = value.replaceAll(s, "***");

        }

        getJspContext().getOut().print(value);

    }

}

4.标签的配置c.tld

<uri>/aa</uri>

 <tag>

   <name>out</name>

   <tag-class>czb.tag.OutTag</tag-class>

   <body-content>empty</body-content>

   <attribute>

       <name>value</name>

        <required>true</required>

        <rtexprvalue>true</rtexprvalue>

   </attribute>

  </tag>

5.显示

<%@ taglib uri="/aa"prefix="o"%>

<body>

  留言原内容:${notes}<br/>

  留言被和谐后的内容:<o:out value="${notes}"/>

</body>

6. 显示结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值