过滤器--解决全站中文乱码问题
程序员文章站
2022-07-01 12:05:51
...
全站乱码案例:
* 利用Servlet的doGet()和doPost()方法中,可以解决中文乱码:
- * doGet()
- String value = request.getParameter("");
- value = new String(value.getBytes("ISO8859-1"),"utf-8");
- response.setContentType("text/html;charset=utf-8");
- * doPost()
- request.setCharacterEncoding("utf-8");
- response.setContentType("text/html;charset=utf-8");
* doGet() String value = request.getParameter(""); value = new String(value.getBytes("ISO8859-1"),"utf-8"); response.setContentType("text/html;charset=utf-8"); * doPost() request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8");
* 以上问题:
* 实际开发时,Web应用程序的逻辑比较复杂(多个Servlet).
* 上述的解决方案,仅能解决当前Servlet的中文乱码问题.
* 如果使用上述方案,解决中文乱码问题:代码重复性太多.
EncodingFilter:
- package app.java.filter.demo1;
- 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;
- /**
- * 当前Filter用于解决全站中文乱码问题.
- * @author JYL
- */
- public class EncodingFilter implements Filter {
- public void destroy() {
- }
- public void doFilter(ServletRequest req, ServletResponse resp,
- FilterChain chain) throws IOException, ServletException {
- //1 进行Request与Response对象的类型强转
- HttpServletRequest request = (HttpServletRequest)req;
- HttpServletResponse response = (HttpServletResponse)resp;
- //2 解决中文乱码问题:1)请求的中文乱码(GET\POST);2)响应的中文乱码.
- request.setCharacterEncoding("utf-8");
- response.setContentType("text/html;charset=utf-8");
- //3 创建自定义的Request对象
- MyRequest myRequest = new MyRequest(request);
- //3 放行
- chain.doFilter(myRequest, response);
- }
- public void init(FilterConfig arg0) throws ServletException {
- }
- }
package app.java.filter.demo1; 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; /** * 当前Filter用于解决全站中文乱码问题. * @author JYL */ public class EncodingFilter implements Filter { public void destroy() { } public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { //1 进行Request与Response对象的类型强转 HttpServletRequest request = (HttpServletRequest)req; HttpServletResponse response = (HttpServletResponse)resp; //2 解决中文乱码问题:1)请求的中文乱码(GET\POST);2)响应的中文乱码. request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); //3 创建自定义的Request对象 MyRequest myRequest = new MyRequest(request); //3 放行 chain.doFilter(myRequest, response); } public void init(FilterConfig arg0) throws ServletException { } }
MyRequest:
- package app.java.filter.demo1;
- import java.io.UnsupportedEncodingException;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletRequestWrapper;
- /**
- * 自定义的Request对象
- * @author JYL
- */
- public class MyRequest extends HttpServletRequestWrapper {
- public MyRequest(HttpServletRequest request) {
- super(request);
- }
- @Override
- public String getParameter(String name) {
- String value = super.getParameter(name);
- if(super.getMethod().equalsIgnoreCase("GET")){
- try {
- value = new String(value.getBytes("ISO-8859-1"),"utf-8");
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- }
- return value;
- }
- }
package app.java.filter.demo1; import java.io.UnsupportedEncodingException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; /** * 自定义的Request对象 * @author JYL */ public class MyRequest extends HttpServletRequestWrapper { public MyRequest(HttpServletRequest request) { super(request); } @Override public String getParameter(String name) { String value = super.getParameter(name); if(super.getMethod().equalsIgnoreCase("GET")){ try { value = new String(value.getBytes("ISO-8859-1"),"utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } return value; } }
TestServlet:
- package app.java.filter.demo1;
- 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 TestServlet extends HttpServlet {
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- String username = request.getParameter("username");
- String password = request.getParameter("password");
- System.out.println(username+" : "+password);
- response.getWriter().println("中文解决了,真好!!!!");
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- String username = request.getParameter("username");
- String password = request.getParameter("password");
- System.out.println(username+" : "+password);
- response.getWriter().println("中文解决了,真好!!!!");
- }
- }
package app.java.filter.demo1; 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 TestServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); System.out.println(username+" : "+password); response.getWriter().println("中文解决了,真好!!!!"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); System.out.println(username+" : "+password); response.getWriter().println("中文解决了,真好!!!!"); } }
xml:
- <filter>
- <filter-name>EncodingFilter</filter-name>
- <filter-class>demo1.EncodingFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>EncodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
<filter> <filter-name>EncodingFilter</filter-name> <filter-class>demo1.EncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>EncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
jsp:
- <form action="test" method="post">
- 用户名:<input type="text" name="username"><br>
- 密码:<input type="password" name="password"><br>
- <input type="submit" value="提交">
- </form>
- <a href="test?username=张无忌&password=123">GET方式发送的请求</a>
<form action="test" method="post"> 用户名:<input type="text" name="username"><br> 密码:<input type="password" name="password"><br> <input type="submit" value="提交"> </form> <a href="test?username=张无忌&password=123">GET方式发送的请求</a>
另一种:
- package com.itheima.filter;
- import java.io.IOException;
- import java.io.UnsupportedEncodingException;
- import java.util.Map;
- 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;
- public class MyFilter implements Filter {
- public void init(FilterConfig filterConfig) throws ServletException {
- }
- public void doFilter(ServletRequest request, ServletResponse response,
- FilterChain chain) throws IOException, ServletException {
- HttpServletRequest req = (HttpServletRequest) request;
- // 解决post方式
- // req.setCharacterEncoding("UTF-8");
- req = new MyRequest(req);
- chain.doFilter(req, response);
- }
- public void destroy() {
- }
- }
- // 实现与被包装对象相同的接口
- // 定义一个与被包装类相对象的引用
- // 定义一个构造方法,把被包装对象传过来
- // 对于不需要改写方法,直接调用
- // 对于无需要改写方法,写自己的方法体
- class MyRequest extends HttpServletRequestWrapper {
- HttpServletRequest request;
- public MyRequest(HttpServletRequest request) {
- super(request);
- this.request = request;
- }
- /*
- * @Override public String getParameter(String name) { name =
- * request.getParameter(name);//乱码 try { return new
- * String(name.getBytes("iso-8859-1"),"UTF-8"); } catch
- * (UnsupportedEncodingException e) { e.printStackTrace(); } return null; }
- */
- @Override
- public String getParameter(String name) {
- Map<String, String[]> map = getParameterMap();
- return map.get(name)[0];
- }
- @Override
- public String[] getParameterValues(String name) {
- Map<String, String[]> map = getParameterMap();
- return map.get(name);
- }
- private boolean flag = true;
- @Override
- public Map<String, String[]> getParameterMap() {
- Map<String, String[]> map = request.getParameterMap();// 乱码
- if (flag) {
- for (Map.Entry<String, String[]> m : map.entrySet()) {
- String[] values = m.getValue();
- for (int i = 0; i < values.length; i++) {
- try {
- values[i] = new String(
- values[i].getBytes("iso-8859-1"), "UTF-8");
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- }
- }
- flag = false;
- }
- return map;
- }
- }
package com.itheima.filter; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.Map; 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; public class MyFilter implements Filter { public void init(FilterConfig filterConfig) throws ServletException { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; // 解决post方式 // req.setCharacterEncoding("UTF-8"); req = new MyRequest(req); chain.doFilter(req, response); } public void destroy() { } } // 实现与被包装对象相同的接口 // 定义一个与被包装类相对象的引用 // 定义一个构造方法,把被包装对象传过来 // 对于不需要改写方法,直接调用 // 对于无需要改写方法,写自己的方法体 class MyRequest extends HttpServletRequestWrapper { HttpServletRequest request; public MyRequest(HttpServletRequest request) { super(request); this.request = request; } /* * @Override public String getParameter(String name) { name = * request.getParameter(name);//乱码 try { return new * String(name.getBytes("iso-8859-1"),"UTF-8"); } catch * (UnsupportedEncodingException e) { e.printStackTrace(); } return null; } */ @Override public String getParameter(String name) { Map<String, String[]> map = getParameterMap(); return map.get(name)[0]; } @Override public String[] getParameterValues(String name) { Map<String, String[]> map = getParameterMap(); return map.get(name); } private boolean flag = true; @Override public Map<String, String[]> getParameterMap() { Map<String, String[]> map = request.getParameterMap();// 乱码 if (flag) { for (Map.Entry<String, String[]> m : map.entrySet()) { String[] values = m.getValue(); for (int i = 0; i < values.length; i++) { try { values[i] = new String( values[i].getBytes("iso-8859-1"), "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } } flag = false; } return map; } }
上一篇: 过滤器--解决全站中文乱码问题
下一篇: 饭店搞笑经典服务员守则