JavaWeb 之 Servlet中文乱码问题

出现原因

前端后端编码不一致

浏览器默认使用UTF-8码表进行编码 ,Servlet使用ISO-8859-1码表进行编码

传输和接收方编码不一致导致乱码的产生

Request乱码

Request请求分为post和get,分别有不同的解决方案

Request乱码 - POST请求
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//给请求中的参数设置编码格式
		request.setCharacterEncoding("UTF-8");
		
		String parameter1 = request.getParameter("parameter1");
		String parameter2 = request.getParameter("parameter2");
		System.out.println(parameter1);
		System.out.println(parameter2);
}

作用:设置从 request 中取得的值,用来指定对浏览器发送来的数据进行重新编码时,使用的编码。

使用注意事项:

  • 在执行 setCharacterEncoding()之前,不能执行任何 getParameter()操作
  • 通过 setCharacterEncoding 设置的编码方式只对 POST 方式提交的表单有效,对 GET 方式无效
Request乱码 - GET请求(Tomcat7.X版本)

解决方案1

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String parameter1 = request.getParameter("parameter1");
		parameter1 = new String(parameter1.getBytes("ISO-8859-1"),"UTF-8");
		String parameter2 = request.getParameter("parameter2");
		parameter2 = new String(parameter2.getBytes("ISO-8859-1"),"UTF-8");
		
		System.out.println(parameter1);
		System.out.println(parameter2);
}

解决方案2

在Tomcat根目录/conf/server.xml中设置编码格式

<Connector URIEncoding="UTF-8" port="8080" protocol="HTTP/1.1" 			
           connectionTimeout="20000" redirectPort="8443" 
/>

那么在启动服务器的时候就会读取到URIEncoding的属性配置,然后再调用自己的一个setURIEncoding方法完成设置,形参的值为我们设置的UTF-8

protected String URIEncoding = null;
public void setURIEncoding(String URIEncoding) {
         this.URIEncoding = URIEncoding;
         setProperty("URIEncoding", URIEncoding);
}
Request乱码 - GET请求(Tomcat8.X版本)

Tomcat8.x的服务器在接收GET请求时,即使参数中有中文,也不会出现乱码,作者在底层设计上的一些改动

Tomcat的连接器组件(Connector) ,Connector是Tomcat中的一个重要的组件,它负责监听Tomcat收到的请求信息,并将这些请求信息传递给Servlet规范中所定义的Request,然后将转换后的请求交给Engine组件去处理,最后将Engine返回的Response返回给客户端 。源码中我们可以看到, URIEncoding的默认值为UTF-8,所以在Tomcat8.x中,即使GET请求包含了中文的数据,也不会出现乱码了

public class Connector extends LifecycleMBeanBase  {
    
    private Charset uriCharset = StandardCharsets.UTF_8;
   
    //查询Tomcat根目录/conf/catalina.properties配置文件中的属性
	public static final boolean RECYCLE_FACADES =
Boolean.parseBoolean(System.getProperty("org.apache.catalina.connector.RECYCLE_FACADES", "false"));   
    
    public Connector() {
        this(null);
    }
    
    public Connector(String protocol) {
        setProtocol(protocol);
        // Instantiate protocol handler
        ProtocolHandler p = null;
        try {
            Class<?> clazz = Class.forName(protocolHandlerClassName);
            p = (ProtocolHandler) clazz.getConstructor().newInstance();
        } catch (Exception e) {
            log.error(sm.getString(
                    "coyoteConnector.protocolHandlerInstantiationFailed"), e);
        } finally {
            this.protocolHandler = p;
        }

        if (Globals.STRICT_SERVLET_COMPLIANCE) {
            uriCharset = StandardCharsets.ISO_8859_1;
        } else {
            uriCharset = StandardCharsets.UTF_8;
        }
    }
}

Response乱码

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	doPost(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//设置响应内容的编码格式
	response.setContentType("text/html;charset=UTF-8");
		
	response.getWriter().println("千锋吴彦祖");
}

跳转到中文页面路径乱码

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	doPost(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	response.sendRedirect(URLEncoder.encode("详情页面.html", "UTF-8"));
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值