使用OutputStream流输出中文注意问题:
package com.xxx.action;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* response 中文编码问题
* @author Administrator
*
*/
@WebServlet(name="text",urlPatterns="/test")
public class TestServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/**
* 编码要统一
*/
String str="你好";
//通过设置响应头控制浏览器以GBK的编码显示数据。
response.setHeader("content-type", "text/html;charset=GBK");
//使用OutputStream流向客户端浏览器输出中文,以GBK的编码进行输出,此时就要控制客户端浏览器以GBK的编码打开,否则显示的时候就会出现中文乱码
response.getOutputStream().write(str.getBytes("GBK"));
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
}
}
使用PrintWriter流输出中文注意问题:
package com.xxx.action;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* response 中文编码问题
* @author Administrator
*
*/
@WebServlet(name="text",urlPatterns="/test")
public class TestServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String str="你好";
//先设置以GBK的编码输出到浏览器
response.setCharacterEncoding("GBK");
//再获取PrintWriter输出流
PrintWriter out=response.getWriter();
//然后再使用response.setHeader("content-type", "text/html;charset=字符编码");设置响应头,控制浏览器以指定的字符编码编码进行显示
response.setHeader("content-type", "text/html;charset=GBK");
//response.getWriter().write("<meta http-equiv='content-type' content='text/html;charset=UTF-8'/>");和response.setHeader("content-type", "text/html;charset=GBK");效果是一样的
out.write(str);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
}
}
使用OutputStream或者PrintWriter向客户端浏览器输出数字
在开发过程中,如果希望服务器输出什么浏览器就能看到什么,那么在服务器端都要以字符串的形式进行输出。
package com.xxx.action;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* response 中文编码问题
* @author Administrator
*
*/
@WebServlet(name="text",urlPatterns="/test")
public class TestServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//在开发过程中,如果希望服务器输出什么浏览器就能看到什么,那么在服务器端都要以字符串的形式进行输出。
//使用OutputStream输出
/*
response.setHeader("content-type", "text/html;charset=GBK");
OutputStream out=response.getOutputStream();
out.write( (10+"").getBytes("GBK") );
*/
//使用printWriter输出
response.setHeader("content-type", "text/html;charser=GBK");
response.setCharacterEncoding("GBK");
PrintWriter writer=response.getWriter();
writer.write(10+"");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
}
}