从servlet转发到错误页面(就是普通jsp,其中isErrorPage=true),转发前有一句:
request.setAttribute("javax.servlet.jsp.jspException",new NullPointerException());//其中的异常可以是任何类型,这里只是一个示意。
就会是乱码 servlet 和 jsp都是utf-8编码。如果去掉这一句,或者把javax.servlet.jsp.jspException改为别的,或者把错误页面的isErrorPage=true去掉,此问题都会消失。这是我截到的回包,发现他不会设置编码,非常奇怪:
HTTP/1.1 500 Internal Server Error
Content-Type: text/html-----------这个地方正常应该是Content-Type: text/html;charset=UTF-8
Transfer-Encoding: chunked
Date: Fri, 20 Jan 2006 03:54:19 GMT
Server: Apache-Coyote/1.1
Connection: close
有没有高手给个明确的解释。

解决方案 »

  1.   

    <%@page contentType="text/html; charset=UTF-8" isErrorPage="true" %>
    <%@page import="java.util.*" %>
    <html>
    <head>
    <title>exception</title>
    </head>
    <body bgcolor="#ffffff">
    <h1>有错误发生</h1>
    </body>
    </html>-------------------------------------------
        private static final String CONTENT_TYPE = "text/html; charset=UTF-8";    //Initialize global variables
        public void init() throws ServletException {
        }    //Process the HTTP Get request
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws
                ServletException, IOException {
            response.setContentType(CONTENT_TYPE);        request.setAttribute("javax.servlet.jsp.jspException",
                                 new NullPointerException());
            getServletContext().getRequestDispatcher("/exception.jsp").forward(
                    request, response);    }
      

  2.   

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws
                ServletException, IOException {
            response.setContentType(CONTENT_TYPE);        throw new NullPointerException());
        }在其他的配置里配对EXCEPTION的处理页面
      

  3.   

    抛不须查异常可以但是如果抛出了IOException 或SQLException 就必须捕获了
      

  4.   

    又做了几次实验,发现是tomcat的Bug。我的环境是:windows2000+Jbuilder2005 (11.0.236.0,内置tomcat5.0.27) 工程使用:jsp2.0 servlet 2.4我用了自己装的tomcat5.0.16 后,此故障消失。
    用了Jbuilder2005内置的tomcat4.1.30后,此故障消失。所以为认定这是tomcat的实现问题。