servletResponse writer输出数据

package response;

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 ServletResponseDemo2 extends HttpServlet {
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //更改response的默认吗表为gbk
        response.setCharacterEncoding("gbk");
        //告诉浏览器用相同的吗表解析数据
//        response.setHeader("Content-type", "text/html;charset=gbk");
        //sun公司提供的便捷方式,可以替换上一句代码
        response.setContentType("text/html;charset=gbk");
        String data = "中国";
        PrintWriter writer = response.getWriter();
        //writer将数据写入到response中response查找iso8859吗表。将查到的编码发给浏览器,浏览器按照自己默认的吗表解析出数据
        writer.write(data);
    }

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

}

posted @ 2014-08-14 11:10  siashan  阅读(1264)  评论(0编辑  收藏  举报