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 ResponseDemo1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String data = "中國";
response.setHeader("content-type", "text/html;charset=UTF-8");
OutputStream out =response.getOutputStream();
byte b[] = data.getBytes("UTf-8");
out.write(b);
}
private void test4(HttpServletResponse response) throws IOException,
UnsupportedEncodingException {
//用字符流輸出數據
String data = "中國";
response.setHeader("content-type", "text/html;charset=UTF-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
out.write(data);
}
private void test2(HttpServletResponse response) throws IOException,
UnsupportedEncodingException {
String data = "中國";
OutputStream out = response.getOutputStream();
response.setHeader("content-type", "text/html;charset=utf-8");
out
.write("<meta http-equive='Content-type' content='text/html;charset=utf-8'>"
.getBytes());
out.write(data.getBytes());
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}