简体   繁体   中英

How to change the Locale in a java web-app?

So this is the doGet() method in my Servlet:

protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) 
        throws ServletException, IOException {
    httpServletResponse.setLocale(new Locale("tr", "TR"));
    System.out.println("Cookies being printed:");
    for(Cookie cookie:httpServletRequest.getCookies()){
        System.out.println(cookie.getName());
        System.out.println(cookie.getValue());
    }
}

The first time I hit the doGet method:

Cookies being printed:
locale
en

And I refresh the page to invoke it again:

Cookies being printed:
JSESSIONID
A00EB65138C896FC282CE11EB20D1DD7
locale
en

It seems like this line has no effect:

httpServletResponse.setLocale(new Locale("tr", "TR"));

What is it I am doing wrong? Why I am a getting a cookie with locale=en?

This is a very simple web app and I am not setting any kind of cookies or anything in any other part of the application. This is already the welcome-file.

Setting a Cookie and setting a Locale are not the same.
Cookie has no direct relation with a Locale .

As per documentation on
javax.servlet.ServletResponse.setLocale(java.util.Locale loc) :

  • It sets locale of the response.
  • It sets locale specific character encoding.

These are executed if response is not committed yet.

Where as Cookie is to set some persistent data at clients browser environment.

To set a Cookie, we first create a cookie, add the same to response and then commit it. And the same is only read from next request onwards from the same client. Unless which, you can't read a cookie which is just set into response. It is not available in the request which the servlet has already received.

// to store cookie value in the format of 
// language + "_" + country + "_" 
//   + (variant + "_#" | "#") + script + "-" + extensions
String cookieValue_fullLength = new Locale( "tr", "TR" ).toString();
Cookie localeCookie_fl = new Cookie( "locale_fl", cookieValue_fullLength );
response.addCookie( localeCookie_fl );

// to store cookie value in the format of "language"
String cookieValue_Language = new Locale( "tr", "TR" ).getLanguage();
Cookie localeCookie_lang = new Cookie( "locale", cookieValue_Language );
response.addCookie( localeCookie_lang );

If there exists a cookie with the same name as "locale" then it would be overwritten.
If you implement this, your current cookie that is set to locale "en" would be overwritten.

After receiving a fresh request, executing following code

for( Cookie cookie : httpServletRequest.getCookies() ) {
    System.out.println( cookie.getName() + " - " + cookie.getValue() );
}

will print following results on the console:

JSESSIONID - A00EB65138C896FC282CE11EB20D1DD7
locale - tr
locale_fl - tr_TR_TR_#u-nu-thai

Value for locale_fl is shown just for example. Check with the correct one after execution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM