Servlets that are developed to be deployed into WebSphere
Application Server often use properties files that will also be included
in the WAR file that is deployed. The ResourceBundle class simplifies
accessing the contents of properties file, but it requires following
Locale-specific (National Language Support enabled) naming rules.
Otherwise, a 500 error might be returned by the servlet.
Example:
- Customer creates Sample.ear that contains a Web Module,
Sample.
- The Sample Web Module has a single servlet,
HelloWorld.class.
- The HelloWorld.class servlet opens the properties file,
SampleData, that contains text to be displayed by the servlet.
- The operating system, in this case Windows® 2000, is configured for US
English. This is locale en_US.
- If the properties file is named SampleData.properties, the
following error is displayed in the output of the servlet:
Error 500: Can't find bundle for base
name Sample, locale en_US |
|
- If the properties file is named SampleData_en_US.properties,
then the properties file is opened correctly and the text of the property
is retrieved and displayed by the servlet in the browser:
Hello IBM Level Two Support |
Note: You might have to restart the EAR file after you have made this
correction.
Notes on using the Application Assembly Tool (AAT):
- HelloWorld.class and HelloWorld.java are
saved into the Web Module in the folder: Web Modules > Sample >
Files > Class Files
- The SampleData_en_US.properties file is added
into the Web Module in the folder: Web Modules > Sample > Files
> Resource Files
- In both cases, you need to use AAT to select the directory
for where the files are stored (in the Open dialog), then Add Files option
to add the individual files. Ignore the "Files of type" entries, since you
are going to select a "Root Directory", not a file.
- The following should be defined, and in this order:
- New EAR file
- New Web Module in the EAR
- Define a context root for the Web Module (For example,
/Sample)
- New Web Component for HelloWorld.class
- New Servlet Mapping for HelloWorld (For example, /hello)
- Add Class files (For example, HelloWorld.class)
- Add Resource file for your Locale (For example, en_US)
- How to test: http://localhost/Sample/hello
Sample servlet code:
import
javax.servlet.http.*;
import java.io.*;
import javax.servlet.*;
import java.util.*;
import java.text.*;
// Constructor
public class HelloWorld extends javax.servlet.http.HttpServlet {
public HelloWorld2() {
super();
}
// Service method: HTTP Get
public void doGet(HttpServletRequest req, HttpServletResponse
res)
throws ServletException, IOException {
ResourceBundle messages =
ResourceBundle.getBundle("SampleData");
String property = messages.getString("text");
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<HTML>");
out.println("<HEAD><TITLE>Hello
World</TITLE></HEAD>");
out.println("<BODY>");
if (null == property || "".equals(property)) {
out.println("<BIG>Hello World
</BIG>");
}
else {
out.println("<BIG>Hello " + property +
"</BIG>");
}
out.println("</BODY></HTML>");
}
} |
Contents of SampleData_en_US.properties:
text=IBM Level Two
Support |
|