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()?
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().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With