三个概念
- 国际化: 意义着一个网站提供不同版本的翻译成访问者的语言或国籍的内容。
- 本地化: 意味着向网站添加资源,以使其适应特定的地理或文化区域。
- 区域设置: 针对某个国家的某个地区的设置。
Servlet可以根据请求者的区域设置时区相应版本的网站,并根据当地的语言、文化和需求提供相应的网站版本,
request返回Locale对象
java.util.Locale request.getLocale();
检测区域设置
序号 | 方法 & 描述 |
---|---|
1 | String getCountry() 该方法以 2 个大写字母形式的 ISO 3166 格式返回该区域设置的国家/地区代码。 |
2 | String getDisplayCountry() 该方法返回适合向用户显示的区域设置的国家的名称。 |
3 | String getLanguage() 该方法以小写字母形式的 ISO 639 格式返回该区域设置的语言代码。 |
4 | String getDisplayLanguage() 该方法返回适合向用户显示的区域设置的语言的名称。 |
5 | String getISO3Country() 该方法返回该区域设置的国家的三个字母缩写。 |
6 | String getISO3Language() 该方法返回该区域设置的语言的三个字母的缩写。 |
GetLocale.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Locale;
public class GetLocale extends HttpServlet{
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// 获取客户端的区域设置
Locale locale = request.getLocale();
String language = locale.getLanguage();
String country = locale.getCountry();
// 设置响应内容类型
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "检测区域设置";
String docType =
"<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n" +