response的outputStream输出中文的乱码问题

package ResponseDemo;

import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Demo1 extends HttpServlet
{
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		String data="中国 ";
		
		//response的outputStream输出数据的问题
		//方法一:程序以什么码表输出,一定要控制浏览器以什么码表打开
		//若"text/html;charset=UTF-8"写错,浏览器会提示下载
		response.setHeader("Content-type", "text/html;charset=UTF-8");
		OutputStream out=response.getOutputStream();
		out.write(data.getBytes("UTF-8"));
		
		//方法二:标签模拟一个http响应头
		out.write("".getBytes());
		out.write(data.getBytes("UTF-8"));
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		doGet(request, response);
	}

}
 
  


web.xml需要的配置信息:


    Demo1
    ResponseDemo.Demo1
  

    Demo1
    /servlet/Demo1
  

 

你可能感兴趣的:(Java)