Saturday, April 17, 2010

Reading browser locale from Java

As part of developing complicated applications, we write much complex logic to address some of the business requirements.  But you know when we need a simple thing, to read the browser locale, unless you already worked on the specific problem ...
hmmmmm......
wonder what you do first thing, google it... rather going through Java API....  okay..
enough explanation here is what we need to do to read it..
most of the time it goes in an intercepting servlet or a web filter...

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


public class ReadBrowserLocaleServlet extends HttpServlet {
                         
    public void doGet (HttpServletRequest req,HttpServletResponse res)
                         throws ServletException, IOException
    {
       doPost(req,res);
    }

    public void doPost (HttpServletRequest req, HttpServletResponse res)
                         throws ServletException, IOException
    {
                        
     String browserLocale=req.getHeader("Accept-Language");
    System.out.println("Browser Locale" + browserLocale);
      
    }   
  } 

Let me know if you like it.... Thanks..