标签:
一、产生乱码的原因:
当浏览器通过http协议访问web服务器时,假设此时浏览器的编码是utf-8,当把请求发给web服务器时,web服务器却以ISO-8859-1编码接收,所以就产生了乱码。
//设置web服务器接收时,以utf-8形式接收,如下:
request.setCharacterEncoding(“utf-8”); //编码方式设置和浏览器一样(还有常见的gbk、gb2312)
//设置浏览器显示时,以utf-8显示。
response.setContentType("text/html;charset=utf-8");
二、三种因为中文而产生乱码举例:
创建一个Web Project(Encoding.java)用于测试,中文乱码问题。
(1)表单form
a) Post 提交
创建一个Html文件(TestFormEncoding.html)代码如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>TestFormEncoding.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<form action="/Encoding/TestEncoding" method="post">
姓 名:<input type="text" name="username" /><br/>
<input type="submit" value="提交" />
</form>
</body>
</html>
创建一个Servlet文件(TestEncoding.java),代码如下:
package com.focus;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestEncoding extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
//PrintWriter out = response.getWriter();
String username = request.getParameter("username");
System.out.println("欢迎 "+username+" 访问本网站");
}
}
当分别输入focus、哈哈 时,在服务器窗口显示如下:
输入哈哈,出现了如下的乱码。
解决方法:
doPost代码如下:
response.setContentType("text/html");
//设置Web服务器的默认编码为utf-8,即接收浏览器请求时,以utf-8编码方式解析。
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
System.out.println("欢迎 "+username+" 访问本网站");
当分别输入哈哈、嘻嘻时,效果如下:
当把doPost代码替换如下时:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
//输出到客户端,而不是在服务器输出
out.println("欢迎 "+username+" 访问本网站");
}
浏览器输出结果如下:
因为浏览器请求Web服务器,Web服务器响应浏览器的编码方式都是不同的,都需要互相转换。
doPost代码开头添加如下代码时:
response.setContentType("text/html;charset=utf-8");
即可处理Web服务器响应浏览器时,把编码装换为utf-8
b) Get 提交
TestFormEncoding.html代码只把method=post改为method=get
改变TestEncoding.java如下:
package com.focus;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestEncoding extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
out.println("欢迎 "+username+" 访问本网站");
}
}
效果如下:
(2)超链接
如在TestFormEncoding.html下添加
<a href="/Encoding/TestEncoding?username=哈哈"> 点击我</a>
效果如下:
→ 和Get处理中文乱码的方式相同。
因为超链接的默认提交方式为Get
(3)sendRedirect() 发生乱码
TestFormEncoding.html代码不变,TestEncoding.java代码如下:
package com.focus;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestEncoding extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
request.setCharacterEncoding("utf-8");
String name = request.getParameter("username");
//转向到Welcome页面,并为username赋值
response.sendRedirect("/Encoding/Welcome?username="+name);
}
}
创建一个Servlet文件(Welcome.java)代码如下:
package com.focus;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Welcome extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
request.setCharacterEncoding("utf-8");
String name = request.getParameter("username");
out.println("用户名为:"+name);
}
}
在浏览器输入:
点击提交,效果如下:
------------------------------------------------------------------
上面三个例子中,服务器都是以字符的形式输出到浏览器上
PrintWriter out = response.getWriter()
Out.println();
而当要使用字节流的形式输出,Welcome.java 代码如下:
package com.focus;
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.omg.CORBA_2_3.portable.OutputStream;
public class Welcome extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
//以字节流的形式输出到浏览器
ServletOutputStream os = response.getOutputStream();
request.setCharacterEncoding("utf-8");
String name = request.getParameter("username");
//创建字节数组,把name装换为字节数组,并指定以UTF-8编码进行转换
byte[] nameByte = name.getBytes("utf-8");
os.write(nameByte);
}
}
效果如下:
三、在IE6版本以及以下的版本时,当中文为奇数时,处理方法
1.全面换成gbk编码方式
在Servlet中代码如下:
String username = new String(request.getParameter(“username”).getBytes(“iso-8859-1”),”gbk”);
2.超链接仍然使用utf-8
在Servlet中代码如下:
String info = java.new.URLEncoder.encode(“中文”,”utf-8”);
Out.println(“<a href=\”/1/2?username=”+info+”\”>tesing</a>”);
接收info
String username = new String (request.parameter(“username”).getBytes(“iso-8859-1”),”utf-8”);
转向仍然使用utf-8
在Servlet中代码如下:
String info = java.net.URLEncoder.encode(username,”utf-8”);
response.sendRedirect(“/1/2?username=”+info);
接收info
tring username = new String (request.parameter(“username”).getBytes(“iso-8859-1”),”utf-8”)
补充:
当我们下载文件时,可能提示框是中文乱码,处理如下:
String temp = java.net.URLEncoder.encode(“南山南.mp3”,”utf-8”);
response.setHeader(“Content-Disposition”,”attachment;filename=”+temp);
四、总结:
当使用PrintWriter字符流输出时。
处理浏览器请求Web服务器出现乱码时,通过
request.setCharacterEncoding("utf-8");
解决乱码。
处理Web服务器响应浏览器出现乱码时,通过
response.setContentType("text/html;charset=utf-8");
//或 response.setHeader("content-type", "text/html;charset=UTF-8"); 两者等价
当使用ServletOutputStream 字节流输出时。
处理PrintWriter字符流输出处理中文乱码的两种方法外,还要把字符转换为字节,并指定编码方式
byte[] nameByte = name.getBytes("utf-8");
Java Web -- Servlet(10) 中文乱码处理专题
标签:
原文地址:http://blog.csdn.net/foces_18/article/details/51221322