标签:method images attr auto port pack 就会 class login
过滤器的写法如下
package com.gqx.encodeFilter; import java.io.IOException; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; 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 EncodingFilter implements Filter { @Override public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub //转型,为了得到更多request的方法,比如contextPath方法等 final HttpServletRequest request=(HttpServletRequest)req; HttpServletResponse response=(HttpServletResponse)res; /** * 处理公用业务,编码问题 */ //解决Post乱码问题 request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); //解决Get方式提交的乱码问题(如果去继承HttpServletRequest重写,方法太多,不方便),事物代理解决 HttpServletRequest proxy=(HttpServletRequest)Proxy.newProxyInstance(request.getClass().getClassLoader(), //指定当前使用的累加器类 new Class[]{HttpServletRequest.class}, //指定目标实现类的接口类型 new InvocationHandler() { //事物处理器 @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { // TODO Auto-generated method stub //定义方法返回值 Object returnValue=null; //获取方法 String methodName=method.getName(); //判断,对getParameter方的Get提交进行中文处理 if ("getParameter".equals(methodName)) { //args是方法的参数,形如request.getParameter(name)中的name String value=request.getParameter(args[0].toString()); //获取请求参数的值,便于将其转码 /* * 判断是不是GET提交 */ //获取请求方式 String reqMthod=request.getMethod(); //是GET请求,开始处理 if ("GET".equals(reqMthod) && value !=null && !"".equals(value)) { value=new String(value.getBytes("iso-8859-1"),"utf-8"); } return value; }else { //不是request.getParameter()的方法,直接执行 returnValue=method.invoke(request, args); } return returnValue; } }); //放行 chain.doFilter(proxy, response); } @Override public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub } }
接收的servlet如下
package com.gqx.encodeFilter; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class LoginServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name=request.getParameter("user"); System.out.println("用户:"+name); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
web.xml的配置文件如下
<filter> <filter-name>EncodingFilter</filter-name> <filter-class>com.gqx.encodeFilter.EncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>EncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
这样就可以收到中文的字符串
过滤器如下
package com.gqx.discussFilter; import java.io.IOException; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.util.ArrayList; import java.util.List; 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; /** * 无效数据过滤 * @author Jie.Yuan * */ public class DataFilter implements Filter { // 初始化无效数据 private List<String> dirtyData; @Override public void init(FilterConfig filterConfig) throws ServletException { // 模拟几个数据 dirtyData = new ArrayList<String>(); dirtyData.add("NND"); dirtyData.add("滚蛋"); } @Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { // 转型 final HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; // 一、处理公用业务 request.setCharacterEncoding("UTF-8"); // POST提交有效 response.setContentType("text/html;charset=UTF-8"); HttpServletRequest proxy = (HttpServletRequest) Proxy.newProxyInstance( request.getClass().getClassLoader(), // 指定当前使用的累加载器 new Class[]{HttpServletRequest.class}, // 对目标对象实现的接口类型 new InvocationHandler() { // 事件处理器 @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { // 定义方法返回值 Object returnValue = null; // 获取方法名 String methodName = method.getName(); // 判断:对getParameter方法进行GET提交中文处理 if ("getParameter".equals(methodName)) { // 获取请求数据值【 <input type="text" name="userName">】 String value = request.getParameter(args[0].toString()); // 调用目标对象的方法 // 获取提交方式 String methodSubmit = request.getMethod(); // 直接调用目标对象的方法 // 判断如果是GET提交,需要对数据进行处理 (POST提交已经处理过了) if ("GET".equals(methodSubmit)) { if (value != null && !"".equals(value.trim())){ // 处理GET中文 value = new String(value.getBytes("ISO8859-1"),"UTF-8"); } } // 中文数据已经处理完: 下面进行无效数据过滤 //【如何value中出现dirtyData中数据,用****替换】 System.out.println("jj"); for (String data : dirtyData) { System.out.println("dd"); // 判断当前输入数据(value), 是否包含无效数据 if (value.contains(data)){ value = value.replace(data, "*****"); } } // 处理完编码、无效数据后的正确数据 return value; } else { // 执行request对象的其他方法 returnValue = method.invoke(request, args); } return returnValue; } }); // 二、放行 (执行下一个过滤器或者servlet) chain.doFilter(proxy, response); // 传入代理对象 } @Override public void destroy() { } }
servlet如下
package com.gqx.discussFilter; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class DisServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 获取请求数据 String content = request.getParameter("discuss"); // 保存到request System.out.println(content); request.setAttribute("content", "Content:" + content); // 转发 request.getRequestDispatcher("/dis.jsp").forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
一旦输入“NND,你好”就会如下
但是出现了一个bug弄了好长时间都没有解决
一旦我在servlet中加了一句转发
request.getRequestDispatcher("/dis.jsp").forward(request, response);
就会出现错误如下错误
问题尚未发现!!!!!!
标签:method images attr auto port pack 就会 class login
原文地址:http://www.cnblogs.com/helloworldcode/p/6105480.html