在我们的日常开发中,乱码问题,还是比较经常遇到的,有时候是浏览器端提交的数据到后台乱码了,有时候是后台响应的数据到前台浏览器端展现出现乱码了。下面我们将通过几个简单的例子来说明乱码的由来和解决方式。

一、前台提交的数据到后端乱码了

1.前台采用post方式提交,后端乱码

①jsp提交数据页面代码:

<%@ 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="encode.do" method="post">
用户名:<input type="text" name="username" /> <input type="submit" value="提交" />
</form>
</body>
</html>

②后端接受参数的代码

package com.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;
@WebServlet("/encode.do")
public class EncodingServlet extends HttpServlet {
private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 获取前台提交的数据
String username = request.getParameter("username");
// 打印输出
System.out.println(username);
}
}

③运行结果

当我们从前台输入中文数据的时候,就会出现乱码了"??????"

④乱码原因:jsp页面的<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>说明了页面的编码是UTF-8和<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">这句话告诉浏览器以UTF-8的方式打开我们的jsp页面,当我们在页面的输入框中填写中文表单数据并提交的时候,会先去查询utf-8的码表对应中文在utf-8的编码值,因为tomcat服务器的默认编码是ISO-8859-1,当我们从后端通过request.getParameter("username")去获取参数的时候,就会拿着我们刚才utf-8的编码值去ISO-8859-1查询对应的字符,而在ISO-8859-1的码表没有这个编码指,所以就出现了???乱码了。

⑤解决乱码的方式:因为浏览器提交的数据是以utf-8进行编码的,所以我们可以通过改变request对象查询的码表改成和浏览器端的编码一致即可解决乱码问题。具体代码如下所示

package com.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; @WebServlet("/encode.do")
public class EncodingServlet extends HttpServlet {
private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 设置request的编码和前台页面的编码保持一致utf-8
request.setCharacterEncoding("utf-8");
// 获取前台提交的数据
String username = request.getParameter("username");
// 打印输出
System.out.println(username);
}
}

2.前台采用get方式提交,后端乱码(哪怕修改了request的编码和页面的编码保持一致,也不行)

①jsp提交数据页面代码:

<%@ 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="encode.do" method="get">
用户名:<input type="text" name="username" /> <input type="submit" value="提交" />
</form>
</body>
</html>

②后端接受参数的代码

package com.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; @WebServlet("/encode.do")
public class EncodingServlet extends HttpServlet {
private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 获取前台提交的数据
String username = request.getParameter("username");
// 打印输出
System.out.println(username);
} }

③运行结果

当我们从前台输入中文数据的时候,就会出现乱码了"??????"

④乱码原因:jsp页面的<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>说明了页面的编码是UTF-8和<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">这句话告诉浏览器以UTF-8的方式打开我们的jsp页面,当我们在页面的输入框中填写中文表单数据并提交的时候,会先去查询utf-8的码表对应中文在utf-8的编码值,因为tomcat服务器的默认编码是ISO-8859-1,当我们从后端通过request.getParameter("username")去获取参数的时候,就会拿着我们刚才utf-8的编码值去ISO-8859-1查询对应的字符,而在ISO-8859-1的码表没有这个编码指,所以就出现了???乱码了。

⑤解决乱码的方式:因为浏览器提交的数据是以utf-8进行编码的,但是我们通过改变request对象查询的码表改成和浏览器端的编码一致依然不能解决乱码问题,这时候我们可以修改tomcat的server.xml中的编码保持和前端页面保持一致即可,就是在Connector标签上加上URIEncoding="前端页面的编码",代码如下:

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

3.一个既能够解决get方式乱码,又能解决post方式乱码的方式如下,就是反向编码,获取原来的编码值,在用这个编码值查询正确的码表即可获取正确的字符了。

package com.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; @WebServlet("/encode.do")
public class EncodingServlet extends HttpServlet {
private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 获取前台提交的数据
String username = request.getParameter("username");
//对获取的数据进行方向编码,获取原来的编码值,在查询新码表
username = new String(username.getBytes("ISO-8859-1"),"UTF-8");
// 打印输出
System.out.println(username);
}
}

4.总结:

①对于get的方式的乱码我们一般都是通过修改tomcat的server.xml的方式来进行处理的。

②对于post方式的乱码,我们一般都是采用request.setCharacterEncoding的方式来处理的。

二、后端提交的数据到前端乱码了

1.jsp提交请求页面

<%@ 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="/JavaWebDemo/encode.do" method="post">
<input type="submit" value="get提交" />
</form>
<form action="/JavaWebDemo/encode.do" method="get">
<input type="submit" value="post提交" />
</form>
</body>
</html>

2.后端响应,并返回数据代码

package com.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; @WebServlet("/encode.do")
public class EncodingServlet extends HttpServlet {
private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 返回数据
response.getWriter().print("还是放松放松放松放松");
} protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 返回数据
response.getWriter().print("还是放松放松放松放松");
} }

3.运行结果

结果返现,返回的数据乱码了。

4.乱码问题分析:我们后端返回数据是通过response对象获取相关的流对象,然后将数据返回,但是后端的数据采用的编码方式ISO-8859-1,而前台页面我们没有指定使用什么编码,他默认会采用本地机器的浏览器使用编码去打开,自然会报错。

5乱码结果方法:我们可以设置http的返回头的contenttype的内容来解决乱码问题,具体事例代码如下:

package com.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; @WebServlet("/encode.do")
public class EncodingServlet extends HttpServlet {
private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println(response.getCharacterEncoding());
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
// 返回数据
response.getWriter().print("还是放松放松放松放松");
} protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setHeader("CONTENTTYPE", "text/html;charset=gbk");
response.setCharacterEncoding("gbk");
// 返回数据
response.getWriter().print("还是放松放松放松放松");
} }

至此,关于servlet中request和response中的乱码问题已经解决了,有不足的地方,希望大家多多提意见!

Requset和Response中的乱码问题的更多相关文章

  1. Javaweb学习笔记——(十)——————response对象,response字符流缓冲器,响应头,状态码,重定向,requset对象,路径和乱码

    请求响应对象: request和response *当服务器接收都请求后,服务器会创建request和response对象,把请求数据封装到request对象中: *然后调用Servlet的sevic ...

  2. Fiddler中Response 的Raw乱码问题解决

    有时候我们看到Response中的HTML是乱码的, 这是因为HTML被压缩了, 我们可以通过两种方法去解压缩. 1. 点击Response Raw上方的"Response body is ...

  3. Servlet中response、request乱码问题解决

    Java Web(二) Servlet中response.request乱码问题解决   三月不减肥,五月徒伤悲,这就是我现在的状态,哈哈~ 健身.博客坚持. --WH 一.request请求参数出现 ...

  4. Java Web之Servlet中response、request乱码问题解决

    Java Web之Servlet中response.request乱码问题解决   一.request请求参数出现的乱码问题 get请求: get请求的参数是在url后面提交过来的,也就是在请求行中, ...

  5. javaweb中的乱码问题(初次接触时写)

    javaweb中的乱码问题 在初次接触javaweb中就遇到了乱码问题,下面是我遇到这些问题的解决办法 1. 页面乱码(jsp) 1. 在页面最前方加上 <%@ page language=&q ...

  6. javaweb中的乱码问题

    0.为什么需要编码,解码, 无论是图片,文档,声音,在网络IO,磁盘io中都是以字节流的方式存在及传递的,但是我们拿到字节流怎么解析呢?这句话就涉及了编码,解码两个过程,从字符数据转化为字节数据就是编 ...

  7. java中避免乱码

    response.setContentType("text/html;charset=UTF-8"); 这个是在action中的 这个是在json中设置乱码的 contentTyp ...

  8. Servlet------>request和response控制编码乱码问题

    我在request篇和response都有提到,觉得会忘记,所以从新整理一下 request细节四----->通过request控制编码问题 第一种方式是通过设置------>reques ...

  9. Java编程中中文乱码问题的研究及解决方案

    0 引言 Java最大的特性是与平台的无关性及开发环境的多样性.字符串被Java应用程序转化之前,是根据操作系统默认的编码方式编码.Java语言内部采用Unicode编码,它是定长双字节编码,即任何符 ...

随机推荐

  1. 编写每天定时切割Nginx日志的脚本

    自动每天定时切割Nginx日志的脚本,很方便很好用,推荐给大家使用.本脚本也是参考了张宴老师的文章,再次感谢张宴老师.1.创建脚本/usr/local/nginx/sbin/cut_nginx_log ...

  2. CSS3 图片旋转

    .nav_all { position:relative; z-index:; width:172px; display:inline; ; } .nav_all b { display:block; ...

  3. 卡特兰数 catalan number

    作者:阿凡卢 出处:http://www.cnblogs.com/luxiaoxun/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留 ...

  4. 生产者和消费者问题学习以及Java实现

    在计算机领域中,生产者-消费者问题(也叫bounded-buffer问题)是一类很经典的多进程同步问题.该问题描述了两类进程,即生产者进程和消费者进程,它们共享一个固定大小的缓冲区作为队列.生产者的任 ...

  5. C语言 · 复数归一化

     算法提高 复数归一化   时间限制:1.0s   内存限制:512.0MB      编写函数Normalize,将复数归一化,即若复数为a+bi,归一化结果为a/sqrt(a*a+b*b) + i ...

  6. ubuntu 12.04下apache 配置家目录地址

    apache2 最在搞前端相关的东西,上一次也记录了 Linux 下 LAMP环境的搭建,现在记录一下如果改变 apache2 的家目录地址该怎么做,改那个配置文件 修改配置文件 /etc/apach ...

  7. Python高级编程之生成器(Generator)与coroutine(三):coroutine与pipeline(管道)和Dataflow(数据流_

    原创作品,转载请注明出处:点我 在前两篇文章中,我们介绍了什么是Generator和coroutine,在这一篇文章中,我们会介绍coroutine在模拟pipeline(管道 )和控制Dataflo ...

  8. 考试星陈沧:借助Testin云測加速实现”考试电子化”目标

    考试星陈沧:借助Testin云測加速实现"考试电子化"目标 2014/10/11 · Testin · 开发人员訪谈 考试星国内首款在线考试云平台,可用于企业内部考核,经销商考核, ...

  9. 你真的需要一个jQuery插件吗

    jQuery的插件提供了一个很好的方法,节省了时间和简化了开发,避免程序员从头开始编写每个组件.但是,插件也将一个不稳定因素引入代码中.一个好的插件节省了无数的开发时间,一个质量不好的插件会导致修复错 ...

  10. [Java] java调用wsdl接口

    前提: ① 已经提供了一个wsdl接口 ② 该接口能正常调用 步骤1:使用cxf的wsdl2java工具生成本地类 下载CXF:http://cxf.apache.org/download.html ...