package com.ceshi; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import javax.servlet.http.HttpServletResponse; public class CharFilter implements Filter{ private ArrayList<String> list = new ArrayList<String>(); @Override public void destroy() { // TODO 自动生成的方法存根 } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest)request; HttpServletResponse rep=(HttpServletResponse)response; MyRequest mreq = new MyRequest(req); chain.doFilter(mreq, rep); } @Override public void init(FilterConfig cfg) throws ServletException { String val=cfg.getInitParameter("dirtywords"); String[] ss=val.split(","); list.addAll(Arrays.asList(ss)); } class MyRequest extends HttpServletRequestWrapper{ private HttpServletRequest request; public MyRequest(HttpServletRequest request) { super(request); this.request=request; } @Override public String getParameter(String name) { String txt=this.request.getParameter("txt"); for(String s:list){ txt=txt.replaceAll(s, "**"); } return txt; } } }
package com.ceshi; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class TestFilter implements Filter{ @Override public void destroy() { // TODO 自动生成的方法存根 } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // TODO 自动生成的方法存根 //HttpServletRequest req=(HttpServletRequest)request; //HttpServletResponse rep = (HttpServletResponse)response; request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); //response.getWriter().write("kaishi"); chain.doFilter(request, response); //response.getWriter().write("jieshu"); } @Override public void init(FilterConfig arg0) throws ServletException { // TODO 自动生成的方法存根 } }
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <filter> <filter-name>testfilter</filter-name> <filter-class>com.ceshi.TestFilter</filter-class> </filter> <filter-mapping> <filter-name>testfilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>CharFilter</filter-name> <filter-class>com.ceshi.CharFilter</filter-class> <init-param> <param-name>dirtywords</param-name> <param-value>tmd,cnm,nmb</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <display-name>Test0213pm</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
NewFile.jsp界面:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <form action="txt.jsp" method="post"> <textarea rows="10" cols="20" name="txt"> asdasdadasdtmdnmbasdasdnmb </textarea> <input type="submit" value="tijiao"/> </form> </body> </html>
txt.jsp界面:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> ${param.txt } </body> </html>
实现效果如下:
时间: 2024-10-13 23:05:50