输出中文数据
字节流:
// 以字节流默认的本地GBK编码输出中文数据,没有乱码
String s = "幸福是神马 ";
ServletOutputStream out = response.getOutputStream();
out.write(s.getBytes());
// 以字节流用UTF-8编码输出中文数据,有乱码
out.write("中文".getBytes("UTF-8"));
解决办法:
方式一:更改浏览器的查看编码(不可取)
注: 方式二、三、四都是一样的,即通知浏览器,使用的码表
方式二:response.setHeader("Content-Type", "text/html;charset=UTF-8");
方式三:response.getOutputStream().write("<meta http-equiv='Content-Type'
content='text/html;charset=UTF-8'>".getBytes("UTF-8"));
*方式四:response.setContentType("text/html;charset=UTF-8");
*****************************************************************************************************************
例:
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ResponseDemo1 extends HttpServlet implements test1 {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
test1(response);
}
// 以字节流默认的本地GBK编码输出中文数据,没有乱码
public void test1(HttpServletResponse response) throws IOException {
String s = "幸福是神马 ";
ServletOutputStream out = response.getOutputStream();
out.write(s.getBytes());
}
// 以字节流用UTF-8编码向客户端输出中文数据
public void test2(HttpServletResponse response) throws IOException,
UnsupportedEncodingException {
String s = "幸福与贫富无关";
// 通知客户端/浏览器查UTF-8码表
response.setHeader("Content-Type", "text/html;charset=UTF-8");
ServletOutputStream out = response.getOutputStream();
out.write(s.getBytes("UTF-8"));
}
// 以字节流用UTF-8编码向客户端输出中文数据
public void test3(HttpServletResponse response) throws IOException,
UnsupportedEncodingException {
String s = "幸福与内心相连";
// 通知客户端/浏览器查UTF-8码表
ServletOutputStream out = response.getOutputStream();
out.write("<meta http-equiv='Content-Type' content='text/html;chatset=UTF-8'>".getBytes("UTF-8"));
out.write(s.getBytes("UTF-8"));
}
// 以字节流用UTF-8编码向客户端输出中文数据
public void test4(HttpServletResponse response) throws IOException,
UnsupportedEncodingException {
String s = "学人要学陈冠希";
// 通知客户端/浏览器查UTF-8码表
response.setContentType("text/html;charset=UTF-8");
ServletOutputStream out = response.getOutputStream();
out.write(s.getBytes("UTF-8"));
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
*************************************************************************************************
字符流:
Servlet中的字符流默认查ISO-8859-1(SUN的Servlet规范要求的)
更改这个默认的编码的方式: response.setCharacterEncoding("UTF-8");
//不要忘记通知浏览器的编码
response.setCharacterEncoding("UTF-8");
方式1:response.setHeader("Content-Type", "text/html;charset=UTF-8");
方式2:response.setContentType("text/html;chatset=UTF-8");
PrintWriter out = response.getWriter();
out.write(s);
在字符流输出中文数据时:response.setContentType("text/html;charset=UTF-8");
有两个作用:通知字符流以UTF-8编码输出
通知客户端以UTF-8解码显示
注: 输出整数,转换成字符串形式,即可,比如98无论查哪个码表都是b
*****************************************************************************************************************
例:
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 ResponseDemo2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
test3(response);
}
// 注: Servlet中的字符流默认查的是ISO-8859-1码表(SUN的servlet规范要求的)
public void test1(HttpServletResponse response) throws IOException {
String s = "**要带摄像机";
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.write(s);
}
public void test2(HttpServletResponse response) throws IOException {
String s = "请问你是逗逼么";
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;chatset=UTF-8");
PrintWriter out = response.getWriter();
out.write(s);
}
public void test3(HttpServletResponse response) throws IOException {
String s = "请问你是猴子请来的么?";
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;chatset=UTF-8");
PrintWriter out = response.getWriter();
out.write(s);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
*****************************************************************************************************************