File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB and other Java EE Technologies and the fly likes Determining Locale .... How? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Determining Locale .... How?" Watch "Determining Locale .... How?" New topic

Determining Locale .... How?

Joe Simone

Joined: Feb 16, 2005
Posts: 25
I have an EJB app and would like to know how I determine what the current locale setting is. If I call Locale.getDefault() I get the default JVM Locale. All of my resource strings have been externalized into resource bundles.

Any help is greatly appreciated.

Greg Charles

Joined: Oct 01, 2001
Posts: 2967

Generally speaking, you don't get the locale in EJB code. One of the strengths of the J2EE platform is the separation of business and presentation logic. The EJBs should be doing business logic, and the locale is only relevant for presentation done by JSPs or servlets (if presentation is handled on the server at all).

Usually, the user will set a preferred language for the site, which you can save in a user profile on the server or in a cookie. Something in the HTTP request may indicate the user's locale, but I'm not sure.
Chintan Rajyaguru
Ranch Hand

Joined: Aug 19, 2001
Posts: 341
I agree with the previous poster. Locale is usually related to internationalization, which EJB should be independent of. I can think of some usecases where you will have different logic based on locale. If you must, you can pass locale from the presentation layer.

Locale locale = request.getLocale();

This method is actually defined in the ServletRequest interface. The value you receive will depend on the the locale set when the http request (or user's browser). Locale.getDefault() always returns the locale of the JVM and hence should not be trusted.

Long story short, pass the locale to the business layer if you have to. BUT try not to pass the Locale object itself, pass the String value that way business layer is less dependent on the [http] presentation layer

C - Coming soon!
I agree. Here's the link:
subject: Determining Locale .... How?
It's not a secret anymore!