web 应用响应乱码问题
非西欧语系乱码原因
在没有设置任何内容类型或编码之前,HttpServletResponse使用的字符编码默认是ISO-8859-1。也就是说,如果直接输出中文,在浏览器上就会看到乱码。
有两种方式可以修改HttpServletResponse输出的编码方式
方式一:设置response的setLocale
浏览器如果有发送Accept-Language标头,则可以使用HttpServletRequest的getLocale()来取得一个Locale对象,代表客户端可接受的语系。可以使用HttpServletResponse的setLocale()来设置地区(Locale)信息,地区信息就包括了语系与编码信息。语系信息通常通过响应标头Content-Language来设置,而setLocale()也会设置HTTP响应的Content-Language标头。
具体实现步骤:
(1)在web.xml中设置默认的区域与编码对应,如果不设置对应关系,编码方式不会变,还是默认的ISO-8859-1,设置示例在下面的web.xml里。
(2)设置好以上信息后,若使用下面的其中一个
response.setLocale(Locale.TAIWAN);
response.setLocale(new Locale("zh", "TW"));
response.setLocale(Locale.CHINA);
就会将HTTP响应的Content-Language设置为zh_CN,而字符编码处理设置为UTF-8。
此时,若使用HttpServletResponse的getCharacterEncoding()方法取得编码设置就是UTF-8。
example
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Archetype Created Web Application</display-name> <!-- 用于映射本地文件在网页上显示为utf-8编码 -->
<locale-encoding-mapping-list>
<locale-encoding-mapping>
<locale>zh_CN</locale>
<encoding>UTF-8</encoding>
</locale-encoding-mapping>
<locale-encoding-mapping>
<locale>zh_TW</locale>
<encoding>UTF-8</encoding>
</locale-encoding-mapping>
</locale-encoding-mapping-list>
</web-app>
web.xml
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form method="get" action="world">
名称:<input type="text" name="name"><br>
<button>发出 GET 请求</button>
</form><br><br>
<form method="post" action="world">
名称:<input type="text" name="name"><br>
<button>发出 POST 请求</button>
</form>
</body>
</html>
Html code
package com.test; import org.junit.Test; 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 java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Locale; @WebServlet("/world")
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
name = new String(name.getBytes("ISO-8859-1"), "UTF-8");
System.out.println("name: " + name); response.setContentType("text/html;");
response.setLocale(Locale.TAIWAN);
System.out.println(response.getCharacterEncoding()); // UTF-8
response.getWriter().write("Hello, " + name);
} @Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
name = new String(name.getBytes("ISO-8859-1"), "UTF-8");
System.out.println("name: " + name); response.setContentType("text/html;");
response.setLocale(Locale.TAIWAN);
System.out.println(response.getCharacterEncoding()); // UTF-8
response.getWriter().write("Hello, " + name);
}
}
Java Code
方式二:使用response的setCharacterEncoding()或setContentType()
调用HttpServletResponse的setContentType()时,指定charset,charset的值会自动用来调用setCharacterEncoding()。
浏览器需要知道如何处理你的响应,所以必须告知内容类型,setContentType()方法在响应中设置content-type响应标头,你只要指定MIME(Multipurpose Internet Mail Extensions)类型就可以了。由于编码设置与内容类型通常都要设置,所以调用setContentType()设置内容类型时,同时指定charset属性是个方便且常见的做法。
如果使用了setCharacterEncoding()或setContentType()时指定了charset,则setLocale()就会被忽略。
example
Html代码还使用上面的
package com.test; import org.junit.Test; 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 java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Locale; @WebServlet("/world")
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
name = new String(name.getBytes("ISO-8859-1"), "UTF-8");
System.out.println("name: " + name); response.setContentType("text/html;");
response.setCharacterEncoding("UTF-8");
System.out.println(response.getCharacterEncoding()); // UTF-8
response.getWriter().write("Hello, " + name);
} @Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
name = new String(name.getBytes("ISO-8859-1"), "UTF-8");
System.out.println("name: " + name); response.setContentType("text/html;");
response.setCharacterEncoding("UTF-8");
System.out.println(response.getCharacterEncoding()); // UTF-8
response.getWriter().write("Hello, " + name);
}
}
Java Code
注意
这两种方式,无论使用哪一种方式,都要设置响应内容类型response.setContentType("text/html;")才能解决乱码。
如果不设置响应内容类型,虽然响应编码方式设置成功,通过response.getCharacterEncoding()得到的是我们想要的UTF-8,但到浏览器依然乱码。
web 应用响应乱码问题的更多相关文章
- web应用中文乱码问题的原因分析
为了让使用Java语言编写的程序能在各种语言的平台下运行,Java在其内部使用Unicode字符集来表示字符,这样就存在Unicode字符集和本地字符集进行转换的过程.当在Java中读取字符数据的时候 ...
- 中文乱码问题(页面乱码,eclipse乱码,请求响应乱码)
1.首先在开发工具eclipse中设置工作空间和文件编码格式,详情参见 http://www.cnblogs.com/lixiang1993/p/7345161.html 2.在eclipse的安 ...
- SpringMVC 请求/响应乱码问题解决方案
请求乱码解决之get乱码问题 GET请求乱码原因分析 GET请求参数是通过请求行中的URL发送给Web服务器(Tomcat)的. Tomcat服务器会对URL进行编码操作(此时使用的是Tomcat设置 ...
- Java Web中解决乱码的方式
Java Web中解决乱码的方式 方式一:添加编码过滤器 package com.itmacy.dev.filter; import javax.servlet.*; import javax.ser ...
- JavaWeb使用Filter进行字符编码过滤 预防web服务中文乱码
JavaWeb使用Filter进行字符编码过滤 预防web服务中文乱码 准备条件:一个创建好的 JavaWeb 项目 步骤: 1.创建一个类并实现 Filter 接口 import javax.ser ...
- Web请求响应简单整理
简单对Web请求响应如何处理进行的整理,难免有理解不到位,理解有偏差的地方,如有理解有误的地方,希望大牛批评指正. 1.Web开发的定义首先看看微软对Web开发的定义:Web开发是一个指代网页或网 ...
- soapui-groovy脚本中文乱码及符号乱码、响应乱码解决方案
groovy脚本中文乱码及符号乱码,解决方案: 响应乱码解决方案:
- web请求响应
转载自:SanMaoSpace 1.Web开发的定义首先看看微软对Web开发的定义:Web开发是一个指代网页或网站编写过程的广义术语.网页使用 HTML.CSS 和 JavaScript编写.这些页面 ...
- web前端响应式布局,自适应全部分辨率
写phpd的我. 近期公司要弄个app关键是没有web开发,而我有比較闲,那就扛枪上阵吧. 响应式布局,web端的?php我一直都是用tp框架,对于web首先想到的是bootstrap框架.仅仅是简单 ...
随机推荐
- Storm实现单词统计代码
import java.io.File; import java.io.IOException; import java.util.Collection; import java.util.HashM ...
- php如何使用rabbitmq实现发布消息和消费消息(一对多)(tp框架)(第二篇)
一个publisher发布消息 多个个customer接受消息 1:准备工作参照: http://www.cnblogs.com/spicy/p/7886820.html 2,:路由: 3: 方法: ...
- #pragma的一些用法
1.#pragma message message 参数:Message参数能够在编译信息输出窗口输出相应的信息,这对于源代码的信息控制特别重要,其使用方法为: #pragma message(&qu ...
- mongodb 错误 SCRAM-SHA-1 authentication failed for --转
log 日志错误信息 2018-10-24T16:14:42.244+0800 I NETWORK [initandlisten] connection accepted from 192.168.1 ...
- 理解Linux内核之中断控制
乍一看下边的Linux内核代码,貌似L3389有bug,于是我就绕有兴趣地阅读了一下local_irq_save/local_irq_restore的源代码. /* linux-4.14.12/mm/ ...
- java编程常用的快捷键
Eclipse 常用快捷键 Eclipse的编辑功能非常强大,掌握了Eclipse快捷键功能,能够大大提高开发效率.Eclipse中有如下一些和编辑相关的快捷键. 1. [ALT+/] 此快捷键为用户 ...
- [Hive]使用 Antlr 开发领域语言
Antlr 简介 ANTLR 语言识别的一个工具 (ANother Tool for Language Recognition ) 是一种语言工具,它提供了一个框架,可以通过包含 Java, C++, ...
- 基于线程实现的生产者消费者模型(Object.wait(),Object.notify()方法)
需求背景 利用线程来模拟生产者和消费者模型 系统建模 这个系统涉及到三个角色,生产者,消费者,任务队列,三个角色之间的关系非常简单,生产者和消费者拥有一个任务队列的引用,生产者负责往队列中放置对象(i ...
- Jsp&Servlet入门级项目全程实录第4讲
惯例广告一发,对于初学真,真的很有用www.java1234.com,去试试吧! 1.添加搜索.添加.修改.删除按钮 <div id="tb"> <div> ...
- [日常] PHP设置 include_path 配置选项
动态设置php.ini中的include_path 配置选项: 两种方式set_include_path($new_include_path)ini_set('include_path',$new_i ...