package mypack;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorldExample1 extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=GBK");
// 第一种情况
PrintWriter out = response.getWriter();
out.println("------第一种情况输出中文正常------"+" <br>");
//第二种情况
ServletOutputStream out = response.getOutputStream();
out.println("------第二种情况输出中文报错但输出英文正常------"+" <br>");
}
}
报错如下:
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
java.io.CharConversionException: Not an ISO 8859-1 character: 第
javax.servlet.ServletOutputStream.print(ServletOutputStream.java:89)
javax.servlet.ServletOutputStream.println(ServletOutputStream.java:242)
mypack.HelloWorldExample1.doGet(HelloWorldExample1.java:43)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:122)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:196)
===============================================
stream输出的是二进制流 没有对字符进行编码 : stream 只适用于 ISO 8859-1编码的字符
writer输出的是文本的信息, 是进行过系统编码后的。