servlet 让浏览器输出中文,并成功打印出来.2种方法

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 {
private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
test1(response); } public void test1(HttpServletResponse response) throws IOException {
// 方法1:
// 要设置response,所使用的码表,以控制reponse以什么码表向浏览器写入数据
// response.setCharacterEncoding("utf-8");
// 同时设置浏览器以何种码表打开,指定浏览以什么 码表打开服务器发送的数据 
// response.setHeader("content-type", "text/html;charset=utf-8");
//  方法2:
response.setContentType("text/html;charset=utf-8");
String data = "中国";
PrintWriter outputStream = response.getWriter();
outputStream.write(data);
} public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
} }

  

上一篇:JavaScript在智能手机上的应用-用手势来改变图片大小


下一篇:[Usaco2014 Open Gold ]Cow Optics (树状数组+扫描线/函数式线段树)