------------------siwuxie095
使用过滤器进行编码转换
在
Java Web 开发中,经常会遇到乱码的问题,统一字符编码是
解决乱码问题的非常有效的手段
一个简单的编码转换过滤器实例:
使用过滤器对请求中的参数信息进行编码转换
工程结构目录如下:
后端代码:
LoginServlet.java:
package com.siwuxie095.servlet; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; // LoginServlet 继承自 HttpServlet public class LoginServlet extends HttpServlet { /** * 用于序列化和反序列化的 ID */ private static final //覆盖父类 HttpServlet 的 doGet() 方法 @Override protected System.out.println("===== doGet ====="); //在 doGet() 方法里调用 doPost() 方法 //这样,GET请求和POST请求可以共用一套处理逻辑 doPost(req, resp); } //覆盖父类 HttpServlet 的 doPost() 方法 @Override protected System.out.println("===== doPost ====="); String userName=req.getParameter("uname"); String password=req.getParameter("upwd"); System.out.println("用户名:"+userName); System.out.println("密码:"+password); String forward=null; if (userName.equals("李白")&&password.equals("8888")) { forward="/success.jsp"; }else { forward="/error.jsp"; } RequestDispatcher rd=req.getRequestDispatcher(forward); rd.forward(req, resp); } } |
EncodingFilter.java:
package com.siwuxie095.filter; 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; public class EncodingFilter implements Filter { //声明一个成员变量 private String charEncoding=null; public EncodingFilter() { } public //在部署描述符中设置该应用的默认字符编码集 charEncoding=fConfig.getInitParameter("encoding"); //如果字符编码的名称没有设置 if (charEncoding==null) { throw new ServletException("EncodingFilter中的编码设置为空!!!"); } } public //如果当前应用的默认编码,与请求中的编码值不一致 if (!charEncoding.equals(request.getCharacterEncoding())) { //那么就将请求中的编码设置成当前默认的编码设置 request.setCharacterEncoding(charEncoding); } //将响应的编码设置也改成当前默认的编码设置 response.setCharacterEncoding(charEncoding); chain.doFilter(request, response); } public } } |
前端代码:
login.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" <html> <head> <meta <title>登录页面</title> <script function check(form){ if(document.forms.loginForm.uname.value==""){ alert("请输入用户名!"); document.forms.loginForm.uname.focus(); return false; } if(document.forms.loginForm.upwd.value==""){ alert("请输入密码!"); document.forms.loginForm.upwd.focus(); return false; } } </script> <style type="text/css"> body { color: #000; font-size =14px; margin: 20px, auto; } </style> </head> <body> <!-- 添加表单,url在部署描述符中进行配置,使用post方式来提交 --> <form <table <tr> <td </tr> <tr> <td>用户名:</td> <td><input </tr> <tr> <td>密码:</td> <td><input </tr> <tr> <td <input <input </td> </tr> </table> </form> </body> </html> |
success.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" <html> <head> <meta <title>登录成功提示页面</title> <style type="text/css"> body { color: #000; font-size =14px; margin: 20px, auto; } #message { text-align: center; } </style> </head> <body> <div 登录成功!<br/> 您提交的信息为:<br/> 用户名:<%= request.getParameter("uname") %><br/> 密码:<%= request.getParameter("upwd") %><br/> <a </div> </body> </html> |
error.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" <html> <head> <meta <title>登录失败提示页面</title> <style type="text/css"> body { color: #000; font-size =14px; margin: 20px, auto; } #message { text-align: center; } </style> </head> <body> <div 登录失败!<br/> 您提交的信息为:<br/> 用户名:<%= request.getParameter("uname") %><br/> 密码:<%= request.getParameter("upwd") %><br/> <a </div> </body> </html> |
在部署描述符
web.xml 中注册 servlet 和 filter:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <display-name>MyFilter</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> <servlet> <servlet-name>LoginServlet</servlet-name> <servlet-class>com.siwuxie095.servlet.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/loginServlet</url-pattern> </servlet-mapping> <filter> <filter-name>EncodingFilter</filter-name> <filter-class>com.siwuxie095.filter.EncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>EncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> |
部署描述符
web.xml 在 WEB-INF 目录下,如果没有,手动创建即可
选择工程
MyFilter,右键->Java EE Tools->Generate Deployment Descriptor Stub
访问:localhost:8080/MyFilter/login.jsp,分别输入 李白 和 8888
跳转到:localhost:8080/MyFilter/loginServlet,登录成功
如果没有
编码转换过滤器,则会乱码,且登录失败
【made by siwuxie095】