Latest web development tutorials

Servlet international

Before we begin, let's look at three important terms:

  • Internationalization (i18n): This means that a website provides different versions of the content translated into the visitor's language or nationality.
  • Localization (l10n): This means that the site was added to the resources to adapt a particular geographic region or culture, such as site translated into Hindi (Hindi).
  • Locale (locale): This is a special cultural or geographic region.It usually refers to the language symbol followed by an underscore and a national symbol. Such as "en_US" for US English setting represents the area.

There are some considerations when creating a global website. This tutorial does not explain the complete details of these considerations, it will pass a good example to show you how differentiated positioning (ie locale) to make web pages presented in different languages.

Servlet can pick the appropriate version of the site according to the locale of the requester, and provide the appropriate version of the site according to the local language, culture and needs. The following is the request object returned Locale object.

java.util.Locale request.getLocale () 

Detection locale

Listed below are important regional setting method, you can use them to detect geographic location, language, and regional settings requester. All of the following methods to display the name of the country and language names requester browser settings.

序号方法 & 描述
1String getCountry()
该方法以 2 个大写字母形式的 ISO 3166 格式返回该区域设置的国家/地区代码。
2String getDisplayCountry()
该方法返回适合向用户显示的区域设置的国家的名称。
3String getLanguage()
该方法以小写字母形式的 ISO 639 格式返回该区域设置的语言代码。
4String getDisplayLanguage()
该方法返回适合向用户显示的区域设置的语言的名称。
5String getISO3Country()
该方法返回该区域设置的国家的三个字母缩写。
6String getISO3Language()
该方法返回该区域设置的语言的三个字母的缩写。

Examples

This example demonstrates how to display the relevant national language and a request:

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
  {
      // Get the client locale Locale locale = request.getLocale ();
      String language = locale.getLanguage ();
      String country = locale.getCountry ();

      // Set the response content type response.setContentType ( "text / html; charset = UTF-8");
      PrintWriter out = response.getWriter ();

      String title = "detection zone settings";
      String docType = "\ n <DOCTYPE html!>";
      out.println (docType +
        "<Html> \ n" +
        "<Head> <title>" + title + "</ title> </ head> \ n" +
        "<Body bgcolor = \" # f0f0f0 \ "> \ n" +
        "<H1 align = \" center \ ">" + language + "</ h1> \ n" +
        "<H2 align = \" center \ ">" + country + "</ h2> \ n" +
        "</ Body> </ html>");
  }
} 

language settings

Servlet can be exported to Western European languages ​​(such as English, Spanish, German, French, Italian, Dutch, etc.) written pages. Here, in order to display all characters correctly, set the Content-Language header is very important.

The second point is to use HTML entities to display all the special characters, such as "& # 241;" represents "ñ", "& # 161;" means "¡" as follows:

import java.io. *;
import javax.servlet *.;
import javax.servlet.http *.;
import java.util.Locale;

public class DisplaySpanish extends HttpServlet {
    
  public void doGet (HttpServletRequest request,
                    HttpServletResponse response)
            throws ServletException, IOException
  {
    // Set the response content type response.setContentType ( "text / html; charset = UTF-8");
    PrintWriter out = response.getWriter ();
    // Set Spanish Language Code response.setHeader ( "Content-Language", "es");

    String title = "En Espa & ntilde; ol";
    String docType = "\ n <DOCTYPE html!>";
     out.println (docType +
     "<Html> \ n" +
     "<Head> <title>" + title + "</ title> </ head> \ n" +
     "<Body bgcolor = \" # f0f0f0 \ "> \ n" +
     "<H1>" + "En Espa & ntilde; ol:" + "</ h1> \ n" +
     "<H1>" + "& iexcl;! Hola Mundo" + "</ h1> \ n" +
     "</ Body> </ html>");
  }
} 

Date specific locale

You can use java.text.DateFormat class and static methods getDateTimeInstance () to format date and time specific to the locale. The following example demonstrates how to format a date specific to a given locale:

import java.io. *;
import javax.servlet *.;
import javax.servlet.http *.;
import java.util.Locale;
import java.text.DateFormat;
import java.util.Date;

public class DateLocale extends HttpServlet {
    
  public void doGet (HttpServletRequest request,
                    HttpServletResponse response)
            throws ServletException, IOException
  {
    // Set the response content type response.setContentType ( "text / html; charset = UTF-8");
    PrintWriter out = response.getWriter ();
    // Get the client locale Locale locale = request.getLocale ();
    String date = DateFormat.getDateTimeInstance (
                                  DateFormat.FULL, 
                                  DateFormat.SHORT, 
                                  locale) .format (new Date ());

    String title = "locale-specific date";
    String docType = "\ n <DOCTYPE html!>";
      out.println (docType +
      "<Html> \ n" +
      "<Head> <title>" + title + "</ title> </ head> \ n" +
      "<Body bgcolor = \" # f0f0f0 \ "> \ n" +
      "<H1 align = \" center \ ">" + date + "</ h1> \ n" +
      "</ Body> </ html>");
  }
} 

Currency specific locale

You can use java.text.NumberFormat class and static methods getCurrencyInstance () to format numbers (such as long double type or type) as currency-specific locale. The following example demonstrates how to format a currency specific to a given locale:

import java.io. *;
import javax.servlet *.;
import javax.servlet.http *.;
import java.util.Locale;
import java.text.NumberFormat;
import java.util.Date;

public class CurrencyLocale extends HttpServlet {
    
  public void doGet (HttpServletRequest request,
                    HttpServletResponse response)
            throws ServletException, IOException
  {
    // Set the response content type response.setContentType ( "text / html; charset = UTF-8");
    PrintWriter out = response.getWriter ();
    // Get the client locale Locale locale = request.getLocale ();
    NumberFormat nft = NumberFormat.getCurrencyInstance (locale);
    String formattedCurr = nft.format (1000000);

    String title = "locale-specific currency";
    String docType = "\ n <DOCTYPE html!>";
      out.println (docType +
      "<Html> \ n" +
      "<Head> <title>" + title + "</ title> </ head> \ n" +
      "<Body bgcolor = \" # f0f0f0 \ "> \ n" +
      "<H1 align = \" center \ ">" + formattedCurr + "</ h1> \ n" +
      "</ Body> </ html>");
  }
} 

Percentage specific locale

You can use java.text.NumberFormat class and static methods getPercentInstance () to format the percentage specific locale. The following example demonstrates how to format a percentage specific to a given locale:

import java.io. *;
import javax.servlet *.;
import javax.servlet.http *.;
import java.util.Locale;
import java.text.NumberFormat;
import java.util.Date;

public class PercentageLocale extends HttpServlet {
    
  public void doGet (HttpServletRequest request,
                    HttpServletResponse response)
            throws ServletException, IOException
  {
    // Set the response content type response.setContentType ( "text / html; charset = UTF-8");
    PrintWriter out = response.getWriter ();
    // Get the client locale Locale locale = request.getLocale ();
    NumberFormat nft = NumberFormat.getPercentInstance (locale);
    String formattedPerc = nft.format (0.51);

    String title = "locale specific percentage";
    String docType = "\ n <DOCTYPE html!>";
      out.println (docType +
      "<Html> \ n" +
      "<Head> <title>" + title + "</ title> </ head> \ n" +
      "<Body bgcolor = \" # f0f0f0 \ "> \ n" +
      "<H1 align = \" center \ ">" + formattedPerc + "</ h1> \ n" +
      "</ Body> </ html>");
  }
}