How can I obtain the message resources object in a servlet?

Carlos Pastor picture Carlos Pastor · Oct 20, 2009 · Viewed 13k times · Source

I'm developing a project with Struts and I was wondering if it's possible to get the message resources object in a servlet, which is included in the same project.

There's no possibility to get that object with the method getResources(HTTPServletRequest) because the servlet does not extends an Action class. Is there a way to do it?

Thanks in advance.

Answer

Carlos Pastor picture Carlos Pastor · Oct 28, 2009

Well, I finally found how to do it. Just if somebody gets stuck in the same issue, here's the solution: use the java.util.ResourceBundle class in your servlet.

You just have to create the ResourceBundle passing along the name of the properties class and the locale you want to use, like you can see below:

ResourceBundle rb = new ResourceBundle("com.foo.package.theClass", myLocale);
//And then get the messages from the rb object
rb.getMessage("myPropertiesKey");