package cn.edu.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.HttpServletResp
package cn.edu.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;//通过response的writer流输出数据的问题public class ResponseDemo2 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { text1(response); } private void text1(HttpServletResponse response) throws IOException { //设置response使用的码表,以控制response以什么码表向浏览器写出数据 response.setCharacterEncoding("UTF-8"); //指定浏览器以什么码表打开浏览器发送的数据 //response.setHeader("content-type", "text/html;charset=UTF-8"); response.setContentType("text/html;charset=UTF-8"); //上面那句话间接的包含了setCharacterEncoding("UTF-8"); String data="中国"; PrintWriter out =response.getWriter(); //writer流只能写字符或者字符串(写字节流的东西不行) out.write(data); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); }}