简体   繁体   English

如何在Java Web应用程序中更改语言环境?

[英]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 . CookieLocale没有直接关系。

As per documentation on 根据文档
javax.servlet.ServletResponse.setLocale(java.util.Locale loc) : 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. 其中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.

相关问题 在标准Java网络应用程序中嵌入游戏? - Embed play in a standard Java web-app? 在AWS中使用Vaadin构建Java Web-App - Building Java Web-App with Vaadin in AWS Java Web应用程序中的Ajax和复杂的图形 - Ajax and sophisticated graphics in Java web-app 使用 Prometheus 监控 Java Web 应用程序 - Monitor Java web-app using Prometheus Java Web应用程序中的数据访问层 - Data access layer in java web-app Java Web应用程序中是否有用于身份验证的库? - Any library for authentication in a java web-app? 如何在web-app中运行这个.java文件作为独奏java应用程序来测试它? - How to run this .java file in a web-app as a solo java application just to test it? groovy或java:如何将'\\'替换为'\\\\''C:\\ www \\ web-app \\ StudyReports \\ test.bat' - groovy or java: how can replace '\' with '\\' 'C:\www\web-app\StudyReports\test.bat' 如何从独立的Java应用程序(而不是从GAE网络应用程序)连接到本地Google Cloud Storage? - How to connect to the local google cloud Storage from standalone java application (not from a GAE web-app)? 带有@SessionScoped Bean 的 Java 网络应用程序如何知道浏览器是否重新启动? - How does Java web-app with @SessionScoped Bean know if browser is re-started?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM