How to set JSP response locale in Jetty 7/8?
If I set the HTTP response locale programmatically in a servlet as follows:
@Override
protected voi开发者_Python百科d 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.
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:
<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):
Locale Tester
pageContext.request.locale = 'en_AU'
default locale = 'en_US'
pageContext.response.locale = 'abcdefg' (should be 'abcdefg')
Jetty 7 returns this (wrongly):
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.
I found out via the Jetty mailing list that this is a bug in Jetty 7, which has now been fixed.
精彩评论