Getting the list of languages that the client browser supports is an important step in localizing your servlets and JSPs. In each request, the client sends the HTTP Accept-Language header. The target servlet or JSP reads the value of this header in the request and can use it to set the content type and generate a custom response.
The servlet API provides the following convenience methods for getting the client's locale:
getLocale
getLocales
The getLocale method returns a Locale object. This locale is defined by the client's browser setting. It is really a convenient way to get the value of the Accept-Language HTTP header. The getLocales method returns a list of locales in decreasing order of importance.
The HttpServletResponse object also provides a getLocale method that returns the locale object that has been assigned to the response.
The following example invokes getLocale to get the locale of the request, dynamically sets the character set, and conditionalizes the content of the response:
...
<%
����String charset = "";
����Locale locale = request.getLocale();
����if(locale.getLanguage().equals("ko")) {
������charset = "EUR_KR";
����} else {
������charset = "ISO-8859-1";
����}
����response.setContentType("text/html; charset=" + charset);
����response.setLocale(locale);
����out.println("<p>charset : " + charset + "</p>");
%>
<%@ page import="java.util.*" %>
<% if(locale.getLanguage().equals("ko")) { %>
������// Response specific to Korean request
<% } else { %>
������// All other responses
<% } %>
...
In JSPs, the page directive is a static method. This means that you cannot use it to change the character set dynamically. As a result, the previous example uses the response.setContentType method in a scriptlet to identify the character set.
For more information on using character sets in servlets and JSPs, see "Understanding character sets and encoding".
Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/jrun/4/Programmers_Guide/i10n5.htm