response.setContentType("text/html;charset=utf-8")后依然乱码的解决方法
从浏览器获取数据到服务器,服务器将得到数据再显示在浏览器上英文字母正常显示,中文字符乱码的问题,已经使用了

response.setContentType("text/html;charset=utf-8");
将浏览器编码设置为utf-8,但依然乱码
源码如下:
package com.swift; import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.google.gson.Gson;
@WebServlet("/add")
public class ServletAdd extends HttpServlet {
private static final long serialVersionUID = 1L;
public ServletAdd() {
super();
} protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().append("Served at: ").append(request.getContextPath());
int id=Integer.parseInt(request.getParameter("id"));
String name=request.getParameter("name");
int age=Integer.parseInt(request.getParameter("age"));
Student st=new Student(id,name,age);
Gson gson=new Gson();
String json=gson.toJson(st);
response.setContentType("text/html;charset=utf-8");//这句使用无效,没有解决乱码问题
response.getWriter().append(json);
writeToFile(new File("d:/student.json"),json);
} private void writeToFile(File file,String json) {
PrintWriter pw = null;
try {
try {
pw=new PrintWriter(new OutputStreamWriter(new FileOutputStream(file,true),"utf-8"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pw.println(json);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
pw.close();
}
} protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
} }
解决过程
通过代码查看当前电脑的编码类型
System.out.println(System.getProperty("file.encoding"));
System.out.println(Charset.defaultCharset());
上面两种方法哪个都可以。
得知编码类型为GBK,所以将代码改为response.setContentType("text/html;charset=GBK");
但还是乱码,编码改正还是没有成功。
需要知道的注意事项有下面几个:
(1)、如果服务端设置编码格式为utf-8,使用的语句 response.setCharacterEncoding("utf-8");
而浏览器端我们查到的编码是GBK,那么一定会乱码,如下图

(2)、方法一,这时在得知浏览器端为GBK的情况,我们只要设置服务器端编码也为GBK,就可以了,使用语句如下:
response.setCharacterEncoding("utf-8");
但要注意这句代码一定要放在尽可能的前边,否则会和前边一样无效。
(3)、方法二,也可以通通都改成utf-8(就是浏览器端和服务器端同时设置成utf-8),代码如下:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("utf-8");//第一句,设置服务器端编码
response.setContentType("text/html;charset=utf-8");//第二句,设置浏览器端解码
response.getWriter().append("Served at: ").append(request.getContextPath());//这句没用
int id=Integer.parseInt(request.getParameter("id"));
String name=request.getParameter("name");
int age=Integer.parseInt(request.getParameter("age"));
Student st=new Student(id,name,age);
Gson gson=new Gson();
String json=gson.toJson(st);
response.getWriter().append(json);
writeToFile(new File("d:/student.json"),json);
}

成功解决
response.setContentType("text/html;charset=utf-8")后依然乱码的解决方法的更多相关文章
- orcal 数据库 maven架构 ssh框架 的全注解环境模版 maven中央仓库批量删除lastupdated文件后依然是lastupdated解决方法 mirror aliyun中央仓库
批量删除文件,得用批处理文件,文件名随便,路径改成你的Repository,代码如下 rem 这里写你的仓库路径 set REPOSITORY_PATH=d:\repo rem 正在搜索... for ...
- Oracle导入数据后中文乱码的解决方法
解决方法: 方法一. 1.在运行命令行输入regedit,打开注册表编辑器 2.找到HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE\KEY_OraDb11g_home1 3.看N ...
- form表单提交后结果乱码的解决方法
1.产生乱码原因:表单提交使用的method="get",get方式数据都是通过地址栏传输,数据会以iso-8859-1方式传输,因此产生乱码 2.概念:URI: Uniform ...
- Oracle使用PLSQL导入数据后中文乱码的解决方法
新建环境变量 名:NLS_LANG 值:SIMPLIFIE DCHINESE_CHINA.ZHS16GBK 保存后重启PLSQL Developer 重新导入. 如果还是乱码,将上面8的环境变量值改为 ...
- Response乱码的解决方法
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletExcept ...
- Sublime Text 无法使用Package Control或插件安装失败的解决方法
Sublime Text用了一年多了,公司搬家近期突然发现Package Control无法安装新插件了.尽管不影响原有功能的使用.还是要解决它.因为本人用Windows系统,仅仅讨论Windosw下 ...
- MySQL root密码忘记后更优雅的解决方法
MySQL root密码忘记后更优雅的解决方法 https://www.jb51.net/article/143453.htm /usr/bin/mysqld_safe --defaults-file ...
- windows 10 删除库后自动恢复的解决方法
目录 什么是windows 库? 手动删除不行吗? 如何正确的"删除"? title: windows 10 删除库后自动恢复的解决方法 date: 2019-06-09 15:4 ...
- 用eclipse 检索SVN 上 myEclipse 建的web项后,成java项目解决方法
用eclipse 检索SVN 上 myEclipse 建的web项后,成java项目解决方法 在网上找了非常多,都无论用. 说添加.project 文件几个属性.但我发现里面都有,在我这里无论什么用. ...
随机推荐
- iOS开发 - CocoaPods远程私有库从0到1
1. 创建主工程 路径:/Users/Funky/Desktop/CocoaPodRemoteLib/MyMainProject/MyMainProject.xcodeproj 2. 在码云(htt ...
- Jmeter_前端RSA加密下的登陆模拟_引用js文件实现(转)
在一次项目实战中,前端登录使用了RSA加密,使用LoadRunner压测的第一步,就是模拟用户登录,可惜loadRunner11并不能录制前端的加密过程,并且安装的LR是基于C语言版,网络上关于RSA ...
- POJ1031 Fence
题目来源:http://poj.org/problem?id=1031 题目大意: 有一个光源位于(0,0)处,一个多边形的围墙.围墙是“全黑”的,不透光也不反射光.距光源r处的光强度为I0=k/r, ...
- BZOJ 1500 [NOI2005]维修数列 FHQ Treap
终于A了这题...这题还是很好...但是我太菜...重构了三遍qwq FHQ Treap大法好!qwq...~~ Ins:直接拿输入造一棵树,把原来的树split成[1,pos],[pos+1,n], ...
- 牛客假日团队赛1 G.Superbull
链接: https://ac.nowcoder.com/acm/contest/918/G 题意: Bessie and her friends are playing hoofball in the ...
- CF D. Recovering BST (区间DP)
题意:给你n个节点,每个节点有一个权值,两个点可以连边当且仅当这两个点的gcd>1,问你这n个点能否构成一个二叉搜索树(每个节点最多有两个儿子,且左儿子小于右儿子),输入为递增顺序. 分析: 若 ...
- 08-图8 How Long Does It Take (25 分
Given the relations of all the activities of a project, you are supposed to find the earliest comple ...
- kindle资源
化繁为简!Kindle 漫画和电子书 资源汇总 & 用法 刚入Kindle那会,在网上翻看了不少文章,有的讲怎么下载电子书,有的讲怎么看漫画,有的讲怎么设置推送邮箱…… 好吧,我不想在用Kin ...
- JS——通过标签获取元素、循环语句、数组去重
通过标签获取元素: var aLi = document.getElementsByTagName('Li'); aLi[0].style.backgroundColor = 'gold'; 选中部分 ...
- QT 商业版调用activex插件
搭建好qt项目后 在main.cpp中写入如下代码 #include "test.h" #include <QtWidgets/QApplication> #inclu ...