2011-07-01 58 views
1

如果我在一個servlet編程設置HTTP響應區域設置如下:如何在Jetty 7/8中設置JSP響應語言環境?

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

然後碼頭7,後來下,任何的JSP試圖讀取通過表達式$該區域設置{} pageContext.response.locale將得到服務器的默認語言環境而不是上面的設置。如果我使用Jetty 6或Tomcat,它工作正常。

下面是完整的代碼來演示該問題:

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); 
    } 
} 

而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返回此(正確):

Locale Tester 

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

碼頭7返回這個(錯誤地):

Locale Tester 

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

FWIW,我使用Jetty/Tomcat Maven插件完成了上述所有測試。

+0

如果使用'新Locale(「ru」,「ru」)'而不是'new Locale(「abcdefg」)'會發生什麼? –

+0

在這種情況下,我得到「pageContext.response.locale ='en_US'(應該是'ru_RU')」,換句話說,我仍然有問題。 –

回答

0

我通過Jetty郵件列表發現這是Jetty 7中的一個bug,它現在有been fixed

相關問題