package cn.itcast.response;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//在servlet中用outputStream输出数据的问题,以及输出中文的问题
public class ResponseDemo extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
test4(response);
}
private void test4(HttpServletResponse response) throws IOException,
UnsupportedEncodingException {
OutputStream out = response.getOutputStream();
out.write((1 + "").getBytes());
}
private void test3(HttpServletResponse response) throws IOException,
UnsupportedEncodingException {
// 注意,下面写错了浏览器提示下载
response.setHeader("content-type", "text/html,charset=UTF-8");
String data = "中国";
OutputStream out = response.getOutputStream();
out.write(data.getBytes("UTF-8"));
}
private void test2(HttpServletResponse response) throws IOException,
UnsupportedEncodingException {
// 用html技术中的meta标签模拟一个http响应头,来控制浏览器的行为
String data = "中国";
OutputStream out = response.getOutputStream();
out
.write("<meta http-equiv='content-type' content='text/html;charset='UTF-8'></meta>"
.getBytes());
out.write(data.getBytes("UTF-8"));
}
private void test1(HttpServletResponse response) throws IOException,
UnsupportedEncodingException {
response.setHeader("content-type", "text/html,charset=UTF-8");
String data = "中国";
OutputStream out = response.getOutputStream();
out.write(data.getBytes("UTF-8"));
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}