• 通过过滤器和增强request对象解决get提交请求服务器端乱码。


    1.表单用get方式提交

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!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=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
        <form action="/WEB24/encodingServlet" method="get">
            <input type="text" name="username">
            <input type="submit" value="提交">
        </form>
    </body>
    </html>

    2.创建一个servlet用于获取表单数据 并输出

    package com.cc.servlet;
    
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Servlet implementation class EncodingServlet
     */
    @WebServlet("/encodingServlet")
    public class EncodingServlet extends HttpServlet {
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String parameter = request.getParameter("username");
            System.out.println(parameter);
        
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doGet(request, response);
        }
    
    }

    3.创建过滤器,并增强request对象解决乱码

    package com.cc.filter;
    
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    
    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 EncodingFilter implements Filter{
    
    
    
        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
                throws IOException, ServletException {
            // TODO Auto-generated method stub
            //request.setCharacterEncoding("UTF-8");
            
            //被增强的对象
            HttpServletRequest req = (HttpServletRequest) request;
            EnhanceRequest enhanceRequest = new EnhanceRequest(req);
            
            chain.doFilter(enhanceRequest, response);
            
        }
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            // TODO Auto-generated method stub
            
        }
        @Override
        public void destroy() {
            // TODO Auto-generated method stub
            
        }
        
        class EnhanceRequest extends HttpServletRequestWrapper{
            private HttpServletRequest request;
            
            public EnhanceRequest(HttpServletRequest request){
                super(request);
                this.request=request;
            }
            
            //对getParameter增强
            public String getParameter(String name){
                String parameter=request.getParameter(name);//乱码
                
                try {
                    parameter=new String(parameter.getBytes("iso8859-1"),"UTF-8");
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                return parameter;
            }
        }
        
    }

    4.编写web.xml

    <filter>
        <filter-name>encodeFilter</filter-name>
        <filter-class>cn.cc.filter.EncodingFilter </filter-class> 

    </filter>

    <filter-mapping>

      <filter-name>encodeFilter</filter-name>

       <url-pattern>/*</url-pattern>

    </filter-mapping>

    5.控制台分别输出    处理前和处理后的输出

  • 相关阅读:
    百度网盘破解
    openstack2 kvm
    Openstack1 云计算与虚拟化概念
    Rsync + Sersync 实现数据增量同步
    Ansible 详解2-Playbook使用
    Ansible 详解
    Python mysql sql基本操作
    COBBLER无人值守安装
    ELK 环境搭建4-Kafka + zookeeper
    此坑待填 离散化思想和凸包 UVA
  • 原文地址:https://www.cnblogs.com/benjamin77/p/9238035.html
Copyright © 2020-2023  润新知