如何在Jetty 7/8中设置JSP响应场所?

时间:2022-11-28 22:50:42

If I set the HTTP response locale programmatically in a servlet as follows:

如果我在servlet中以编程方式设置HTTP响应本地语言环境,如下所示:

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
{
    response.setLocale(SOME_LOCALE);
    // .. etc
}

Then under Jetty 7 and later, any JSPs trying to read that locale via the expression ${pageContext.response.locale} will get the server's default locale instead of the one set above. If I use Jetty 6 or Tomcat, it works fine.

然后在Jetty 7下,任何jsp都试图通过表达式${pageContext.response来读取语言环境。locale}将获得服务器的默认语言环境,而不是上面的一组语言环境。如果我使用Jetty 6或Tomcat,它可以正常工作。

Here's the full code to demonstrate the problem:

下面是演示这个问题的完整代码:

public class MyServlet extends HttpServlet {

    // Use a dummy locale that's unlikely to be the user's default
    private static final Locale TEST_LOCALE = new Locale("abcdefg");

    @Override
    protected void doGet(final HttpServletRequest request, final HttpServletResponse response)
        throws IOException, ServletException
    {
        // Set a known response locale
        response.setLocale(TEST_LOCALE);

        // Publish some interesting locales to the JSP as request attributes for debugging
        request.setAttribute("defaultLocale", Locale.getDefault());
        request.setAttribute("testLocale", TEST_LOCALE);

        // Forward the request to our JSP
        getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
    }
}

And the JSP:

和JSP:

<html>
    <head>
        <title>Locale Tester</title>
    </head>
    <body>
        <h2>Locale Tester</h2>
        <ul>
            <li>pageContext.request.locale = '${pageContext.request.locale}'</li>
            <li>default locale = '<%= request.getAttribute("defaultLocale") %>'</li>
            <li>pageContext.response.locale = '${pageContext.response.locale}' (should be '<%= request.getAttribute("testLocale") %>')</li>
        </ul>
    </body>
</html>

Tomcat returns this (correctly):

Tomcat返回这个(正确):

Locale Tester

    pageContext.request.locale = 'en_AU'
    default locale = 'en_US'
    pageContext.response.locale = 'abcdefg' (should be 'abcdefg')

Jetty 7 returns this (wrongly):

Jetty 7返回(错误):

Locale Tester

    pageContext.request.locale = 'en_AU'
    default locale = 'en_US'
    pageContext.response.locale = 'en_US' (should be 'abcdefg')

FWIW, I did all the above testing using the Jetty/Tomcat Maven plugins.

FWIW,我使用Jetty/Tomcat Maven插件进行了上述所有测试。

1 个解决方案

#1


0  

I found out via the Jetty mailing list that this is a bug in Jetty 7, which has now been fixed.

我通过Jetty邮件列表发现这是Jetty 7中的一个bug,现在已经修复了。

#1


0  

I found out via the Jetty mailing list that this is a bug in Jetty 7, which has now been fixed.

我通过Jetty邮件列表发现这是Jetty 7中的一个bug,现在已经修复了。