How can I load a resource file from a servlet?

Shirish Bathe

[The below example leaves out a cruicial step: where to put the .properties files. Fortunately, Java looks for properties files using the same method with which it looks for class files. So just put the .properties files in the appropriate package directory, either under WEB-INF/classes or in a JAR file in WEB-INF/lib/ -Alex]

Please try with following code. Compile and deploy servlet files in resp. directories.
Create 3 different resource files.
Start Servlet engine and access servlet using following url
1. For franch message
http://172.22.67.23:8080/examples/servlet/ResourceServlet?MyLanguage=0
2. For german message
http://172.22.67.23:8080/examples/servlet/ResourceServlet?MyLanguage=1
3. For english message
http://172.22.67.23:8080/examples/servlet/ResourceServlet?MyLanguage=2
-------------------- ResourceServlet ------------------------------------
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ResourceServlet extends HttpServlet {

ResourceBundle rb = null;

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/html");

PrintWriter out = response.getWriter();
HttpSession session = request.getSession(true);

int localeSelected = 2;
String strTemp = request.getParameter("MyLanguage");
if (null != strTemp)
localeSelected = (new BigDecimal(strTemp)).intValue();

switch (localeSelected){
case 0 :
System.out.println("User Requested French Page");
rb = ResourceBundle.getBundle("LocaleStrings", Locale.FRENCH);
System.out.println(rb.toString());
break;

case 1 :
System.out.println("User Requested German Page");
rb = ResourceBundle.getBundle("LocaleStrings", Locale.GERMAN);
break;

case 2 :
default:
System.out.println("User Requested Default/English Page");
rb = ResourceBundle.getBundle("LocaleStrings", Locale.ENGLISH);
}


out.println("<html>");
out.println("<body>");

out.println("<head>");
String title = rb.getString("title");
out.println("<title>" + title + "</title>");
out.println("</head>");

out.println("<body>");
out.println(rb.getString("Question"));

out.println("</body>");
out.println("</html>");

out.println("</body>");
out.println("</html>");
}

public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
doGet(request, response);
}

}




------------------- LocaleStrings.properties ---------------------
Question=How are you?
title=This page is in english


------------------- LocaleStrings_fr.properties ---------------------
Question=Comment allez-vous?
title=This page is in Franch


------------------- LocaleStrings_de.properties ---------------------
Question=Wie geht es Ihnen?
title=This page is in German


Hope it is solution you required?


Have a nice day...



Shirish
(bathe@satyamonline.com)

0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact