• 过滤器及请求封装器实现字符编码


    1、Html代码

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        <form method="post" action="hello">
            名称:<input type="text" name="name"><br>
            年龄:<input type="text" name="age"><br>
            <input type="submit" value="post提交">
        </form>
        <hr>
        <form method="get" action="hello">
            名称:<input type="text" name="name" value=""><br>
            年龄:<input type="text" name="age" value=""><br>
            <input type="submit" value="get提交" name="submit">
        </form>
    </body>
    </html>
    html Code

    2、Servlet代码

    package com.test;
    
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.PrintWriter;
    
    @WebServlet(urlPatterns = "/hello")
    public class MyServlet extends HttpServlet {
    
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
            // 读取参数
            String name = request.getParameter("name");
            String age = request.getParameter("age");
            // 响应客户端
            response.setContentType("text/html");
            PrintWriter writer = response.getWriter();
            writer.print(name + "|" + age);
            writer.close();
        }
    
        public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
            doGet(request, response);
        }
    }
    Java Code

    3、请求封装器

    package com.test;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletRequestWrapper;
    import java.io.UnsupportedEncodingException;
    
    public class MyRequestWrapper extends HttpServletRequestWrapper {
        // 构造方法不可少,且在方法内部必须调用父类的构造方法
        public MyRequestWrapper(HttpServletRequest request) {
            super(request);
        }
        @Override
        public String getParameter(String name) {
            HttpServletRequest request = (HttpServletRequest)getRequest();
            String parameter = request.getParameter(name);
            try {
                return new String(parameter.getBytes("ISO-8859-1"), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            return null;
        }
    }
    Java Code

    4、过滤器

    package com.test;
    
    import javax.servlet.*;
    import javax.servlet.annotation.WebFilter;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    @WebFilter("/*")
    public class EncodeDecodeFilter implements Filter {
        private FilterConfig filterConfig;
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            this.filterConfig = filterConfig;
        }
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
            HttpServletRequest request = (HttpServletRequest) servletRequest;
            HttpServletResponse response = (HttpServletResponse) servletResponse;
            // 首先解决响应乱码
            response.setCharacterEncoding("UTF-8");
            // 解决请求乱码
            // 如果是post请求,把原始请求对象传递进去
            MyRequestWrapper myRequestWrapper = new MyRequestWrapper(request);
            if ("POST".equals(request.getMethod())) {
                request.setCharacterEncoding("UTF-8");
                chain.doFilter(request, response);
                return;
            }
            // 如果是get请求,包装请求对象传递进去
            chain.doFilter(myRequestWrapper, response);
        }
        public void destroy() { }
    }
    Java Code

    6、测试方式

    (1)浏览器输入地址:http://127.0.0.1/index.html

    (2)填写表单时写入中文字符,提交,查看响应

  • 相关阅读:
    面向对象 & sql语句
    MySQL--数据库面试题汇集
    MySQL优化
    JAVA日报
    JAVA日报
    JAVA日报
    JAVA日报
    JAVA日报
    《大道至简》读后感
    JAVA日报
  • 原文地址:https://www.cnblogs.com/Mike_Chang/p/10065821.html
Copyright © 2020-2023  润新知