【问题标题】:JSP UTF-8 char encoding error on Glassfish 3.01Glassfish 3.01 上的 JSP UTF-8 字符编码错误
【发布时间】:2011-07-01 03:25:57
【问题描述】:

我有一个相当奇怪的问题。在我的 Web 应用程序中,我使用 UTF_8 编码。在我将一个 jsp 页面放入我的 /Protected/ 文件夹之前,这工作正常。完全相同的 jsp 文件在我的文档根目录下工作得很好。 为了到达 /protected/ 文件夹,我使用了过滤器。 像这样:

    public void doFilter(ServletRequest request, ServletResponse response,
                     FilterChain chain)
throws IOException, ServletException {

    HttpServletRequest req = (HttpServletRequest) request;
    HttpServletResponse res = (HttpServletResponse) response;
    HttpSession session = req.getSession();
    PrintWriter out = res.getWriter();
    if (session.getAttribute("user") == null){
        // redirect the user to the login page and give error message
             res.sendRedirect("/myApp/login.jsp");
         }
    else {

    chain.doFilter(request, response);

    }
}

在我的 sun-web.xml 中我指定了这个:

<locale-charset-info default-locale="">
<parameter-encoding default-charset="UTF-8"/>
</locale-charset-info>

在我的 jsp 文件中,我指定了这个:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

加:

非常感谢任何帮助!提前致谢!

【问题讨论】:

    标签: java jsp servlets character-encoding servlet-filters


    【解决方案1】:

    去掉过滤器中的以下行。

    PrintWriter out = res.getWriter();
    

    您只是在过滤器中根本不需要它,而且当第一次创建响应的输出流或写入器时,您以后不能再更改其字符编码。

    【讨论】:

    • 感谢您的帮助!它解决了我的问题。我只是在重定向之前使用 Printwriter out = res.getWriter() 进行了一些测试,但忘记了将其删除。再次感谢!
    猜你喜欢
    • 2015-01-02
    • 1970-01-01
    • 2016-09-14
    • 1970-01-01
    • 1970-01-01
    • 2014-12-25
    • 1970-01-01
    • 2012-03-27
    • 1970-01-01
    相关资源
    最近更新 更多