Wednesday, 13 August 2014

getLocale() : Get the preferred locale of the client

public Locale getLocale()
Returns the preferred locale of the client based on the Accept-Language header. If the client request doesn't provide the header ' Accept-Language', then this method returns the default locale of the server.

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

@WebServlet(urlPatterns = {"/PreferredLocale"})
public class PreferredLocale extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException{
        try(PrintWriter out = res.getWriter()){
            Locale prefLocale = req.getLocale();
            
            out.println("<html><head><title>Preferred Locale</title></head>");
            out.println("<body><h1>Preferred Locale is : ");
            out.println(prefLocale +"</h1></body></html>");
        }
    }  
}

Sample Output



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment