I am passing language code and country code from UI to create Locale
object. How to set this Locale object in HttpServletRequest
, so that any where in the web application I can this Locale object by using HttpServletRequest.getLocale()
?

- 7,086
- 1
- 33
- 49

- 31
- 1
- 2
2 Answers
The value returned by HttpServletRequest.getLocale()
is set automatically, by the Servlet container. From the Javadocs:
Returns the preferred Locale that the client will accept content in, based on the Accept-Language header. If the client request doesn't provide an Accept-Language header, this method returns the default locale for the server.
If you have a web application, you should preferably not ask the country code etc in your application, but rely on the browser preferences/settings (i.e. set by each user in the browser itself, and applicable to ALL web pages he visits). It's this value that is communicated via the above mechanism and returned by your HttpServletRequest.getLocale()
.

- 7,086
- 1
- 33
- 49
Assuming this is for a feature to allow the user to select the application language:
A request is a very short-lived object. If you want to set it once and then have the value persist across multiple requests, then the session is a better place. If you're dealing with a stateless application then the client will have to pass it along with each request.

- 5,095
- 2
- 35
- 47