[英]How to change the Locale in a java web-app?
So this is the doGet() method in my Servlet: 这就是我的Servlet中的doGet()方法:
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: 我第一次点击doGet方法:
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")); httpServletResponse.setLocale(new Locale(“ tr”,“ TR”)));
What is it I am doing wrong? 我做错了什么? Why I am a getting a cookie with locale=en?
为什么我要使用locale = en获取cookie?
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. 这是一个非常简单的Web应用程序,我没有在应用程序的任何其他部分中设置任何Cookie或任何内容。 This is already the welcome-file.
这已经是欢迎文件。
Setting a Cookie
and setting a Locale
are not the same. 设置
Cookie
和设置Locale
是不同的。
Cookie
has no direct relation with a Locale
. Cookie
与Locale
没有直接关系。
As per documentation on 根据文档
javax.servlet.ServletResponse.setLocale(java.util.Locale loc) : javax.servlet.ServletResponse.setLocale(java.util.Locale loc) :
These are executed if response is not committed yet. 如果尚未提交响应,则执行这些命令。
Where as Cookie
is to set some persistent data at clients browser environment. 其中
Cookie
是在客户端浏览器环境中设置一些持久性数据。
To set a Cookie, we first create a cookie, add the same to response and then commit it. 要设置Cookie,我们首先创建一个Cookie,将其添加到响应中,然后提交。 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.
否则,您将无法读取已设置为响应的Cookie。 It is not available in the request which the servlet has already received.
在servlet已经收到的请求中不可用。
// 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. 如果存在一个与
"locale"
同名的cookie,则它将被覆盖。
If you implement this, your current cookie that is set to locale "en"
would be overwritten. 如果实施此方法,则设置为语言环境
"en"
当前cookie将被覆盖。
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. 仅显示了
locale_fl
值。 Check with the correct one after execution. 执行后检查正确的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.