Detect locale, language settings & local specific time

Q.  Write a JSP application to detect locale, language settings and local specific time.

Answer:

There are some important methods which can be used to detect requester's location, language and of course locale. For example getLocale(), getLanguage(), getCountry() etc.

Following example shows how to detect locate, current language setting, local specific time through JSP application.  

Demo.jsp

<%@ page import="java.io.*,java.util.Locale" %>
<%@ page import="javax.servlet.*,javax.servlet.http.* "%>
<%@ page import="java.text.NumberFormat,java.util.Date" %>
<%@ page import="java.text.DateFormat,java.util.Date" %>

<%
   //Get the client's Locale
   Locale locale = request.getLocale();
   String language = locale.getLanguage();
   String country = locale.getCountry();

   String title = "Locale Specific Dates";
   //Get the client's Locale
   String date = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.SHORT, locale).format(new Date( ));
  
   NumberFormat nft = NumberFormat.getCurrencyInstance(locale);
   String formattedCurr = nft.format(500);
%>
<html>
    <head>
        <title>Detecting Locale</title>
    </head>
    <body>
        <form>
            <fieldset style="width:20%; background-color:#e6ffe6;">
                <center>
                    <h1>Detecting Locale</h1>
                </center>
                <p align="center">
                <%
                    out.println("Language : " + language  + "<br />");
                    out.println("Country  : " + country   + "<br />");
                %>
                </p>
                <div align="center">
                    <h1><% out.print(title); %></h1>
                    <p>Local Date: <%  out.print(date); %></p>
                    <p>Formatted Currency: <%  out.print(formattedCurr); %></p>
                </div>
            </fieldset>
        </form>
    </body>
</html>


web.xml

<web-app>
    <servlet>
        <servlet-name>xyz</servlet-name>
        <jsp-file>/Demo.jsp</jsp-file>
    </servlet>
    <servlet-mapping>
        <servlet-name>xyz</servlet-name>
        <url-pattern>/test</url-pattern>
    </servlet-mapping>
</web-app>


Output:

internationalization