package cn.ITHong.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 ResponseDemo1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
test4(response);
}
/**
* 字符流输出中文数据,正常显示,有两个处理,与test3()作用完全一样*/
public void test5(HttpServletResponse response) throws IOException {
int i =98;
//字节流到客户端会出现字符
//所以传数字,必须改变为字符流。
response.getOutputStream().write((i+"").getBytes());
}
/**
* 字符流输出中文数据,正常显示,有两个处理,与test3()作用完全一样*/
public void test4(HttpServletResponse response) throws IOException {
String s = "同学们测试";
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.write(s);//UTF-8要求咯
}
/**
* 字符流输出中文数据,正常显示*/
public void test3(HttpServletResponse response) throws IOException {
String s = "同学们";
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.write(s);//UTF-8要求咯
}
/**
* 字符流输出中文数据,正常显示*/
public void test2(HttpServletResponse response) throws IOException {
String s = "**腰带摄像机";
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-type", "text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.write(s);//UTF-8要求咯
}
/**
* 字符流输出中文数据,乱码*/
public void test1(HttpServletResponse response) throws IOException {
String s = "**腰带摄像机";
PrintWriter out = response.getWriter();
out.write(s);//默认查的是ISO-8859-1码表(SUN的servlet规范要求)
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
Java字符流
最新推荐文章于 2023-01-24 22:18:53 发布