Retrieve browser locale from httpsession?

2019-02-26 08:41发布

Is it possible to derive the preferred language from a httpsession object (javax.servlet.http.HttpSession) ? It is possible to get it from the servletrequest but I dont have that. Thanks for any ideas. Sven

3条回答
放荡不羁爱自由
2楼-- · 2019-02-26 08:43

As its Javadoc shows, there is no method which does exactly that.

Either just obtain it from the request (preferred), or store it as an attribute of the session yourself.

查看更多
smile是对你的礼貌
3楼-- · 2019-02-26 08:48

usually we cannot from a HttpSession get the relevant httpRequest object. However there is a workaround.

You can create a Filter, in the filter, you have everything, httpSession, httpRequest etc. Then create a ThreadLocal variable to store the HttpRequest object. Or just store some request attributes that you need in your later methods.

Your method should be able to get the value of the ThreadLocal. HttpRequest, or language settings or whatever you stored before.

Don't know if this answered your question.

查看更多
【Aperson】
4楼-- · 2019-02-26 09:00

The preferred Locale for a user is available as a request header ("Accept-Language"). It is automatically filled in by the user's browser according to its preferences. Then, you can store this info in the user's session and retrieve it later when appropriate.

查看更多
登录 后发表回答