How to get country locale from a http request

How to get country locale from a http request

Hi,

I Have a requirement like , i need to get the country locale(from whcih country he is logged in) when user login to a web application. Based on that country locale i am going to implement business logic.

Can anyone help me out to get this done.

any help would be highly appreciated.

Thanks Katnam

View Answers

October 9, 2012 at 12:24 PM

Locale l = InetAddressLocator.getLocale(request.getRemoteAddr());
String countryName = l.getCountry();
String language = l.getDisplayLanguage();
String host = request.getRemoteHost();

    System.out.println("your IP address: "+InetAddressLocator.getLocale(request.getRemoteAddr()));   
System.out.println("your country name: "+countryName);  
    System.out.println("your language name: "+language);  
    System.out.println("your host name: "+host);

October 9, 2012 at 12:43 PM

could you please let me know what exactly InetAddressLocator ?

where i will get this class?

Thanks.


October 9, 2012 at 3:47 PM

Here is a code of getLocale() example that displays the date in specific in 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{
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    Locale locale = request.getLocale( );
    String date = DateFormat.getDateTimeInstance(
                                  DateFormat.FULL, 
                                  DateFormat.SHORT, 
                                  locale).format(new Date( ));

    out.println(date);
  }
}

October 9, 2012 at 3:57 PM

Here is a getLocale() example in java servlet.

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();

      out.println(language + ":" + country);
  }
}

October 10, 2012 at 9:36 AM

Thank you all..









Related Tutorials/Questions & Answers:
How to get country locale from a http request
How to get country locale from a http request  Hi, I Have a requirement like , i need to get the country locale(from whcih country he is logged in) when user login to a web application. Based on that country locale i am going
Get current country from iPhone ??
Get current country from iPhone ??  hello, I want to get the current country in the iPhone settings. Can anyone tell me how to get the current... you can get current country
Advertisements
how to get HTTP GET request value using javascript
how to get HTTP GET request value using javascript  how to get HTTP GET request value using javascript   how is how you can get HTTP GET request value using javascript: var parameter = window.location.search.replace
How to fireup HTTP request from JavaFX Client
How to fireup HTTP request from JavaFX Client  Please tel me how do I can fire HTTP post request from an JavaFX client. Sceneario: I've an JavaFX... i've working version of JSP Code to handle HTTP post request tested by posting
How to send the request and get the request?
How to send the request and get the request?  how to send a request to a JSP file in another domain in the same server and get the request done i.e how to include JSP file of one domain to another doamin JSP within in the same
How Can I get ArrayList of Data from jsp with ajax request
How Can I get ArrayList of Data from jsp with ajax request  Hi, I retereved 5 Employee Record of Data from Struts Action class to jsp with ajax response. But I don't how how can reterve this dynamic 5 employee records
How to send HTTP request in java?
How to send HTTP request in java?  How to send HTTP request in java
how to get request url in node js
how to get request url in node js  Hi, I want to get the request url in Node js. how to get request url in node js? Thanks   Hi, Node... of request. how to get request url in node js? Here is the code: console.log
how to get request url in node js
how to get request url in node js  Hi, I want to get the request url in Node js. how to get request url in node js? Thanks   Hi, Node... of request. how to get request url in node js? Here is the code: console.log
How to get the request scope values? - Struts
How to get the request scope values?  Get value in Struts
Java get System Locale
Java get System Locale       In this section, you will learn how to obtain the locale. We are providing you an example which will obtain the locale of the system
HTTP Status 405 - Request method 'GET' not supported. Its urgent
HTTP Status 405 - Request method 'GET' not supported. Its urgent  ...') in onclick method, following error is displaying in new window. "HTTP Status 405 - Request method 'GET' not supported." Anybody reply ASAP, its urgent
JMeter HTTP request example
JMeter HTTP request example  Concerning: http://www.roseindia.net/jmeter/using-jmeter.shtml how do I set path? also what do i need to do to get the helloworld servlet work? Thanks in advance
Java get default Locale
Java get default Locale       Locale object is a representation of geographical, political, or cultural region. We can get and set our default locale through the java programs
Get Parameter Name From Servlet Request
Get Parameter Name From Servlet Request       This example illustrates about how to get parameter from jsp page... having name as firstname, lastname and middle name. We can get values
How to get data from Excel sheet - Struts
How to get data from Excel sheet  Hi, I have an excel sheet with some data(including characters and numbers). Now i want read the data from... the following links: http://www.roseindia.net/tutorial/java/poi
Java get System Locale
Java get System Locale       In this section, you will learn how to obtain the locale. We are providing you an example which will obtain the locale of the system by using
how to get data from checkboxes - JSP-Servlet
how to get data from checkboxes  hi, i got list of tables on screen... need to get only those tables to the next page where i can get list of columns to that selected tables. please help me.  hi, we can get the selected
How to send request to the visa/master card to get verifed the credit card - Security
How to send request to the visa/master card to get verifed the credit card  Sending request to verify the card number
java how to get domain name from url
java how to get domain name from url  How to get the domain name from a url string? Example program in java for "how to get domain name from url" Thanks (adsbygoogle = window.adsbygoogle || []).push({});  
How to get day from date in SQL?
How to get day from date in SQL?  Hi, How to get day from date in SQL? For your information I am working on the MySQL database. Thanks... use the following queries to get the data: select date_format(now(),'%e')as day
How to get hibernate configuration from sessionfactory?
How to get hibernate configuration from sessionfactory?  Hi, I need to get hibernate configuration from sessionfactory, how can i do it? Thanks... to understand how to get hibernate configuration from sessionfactory
How to save and get value from JSP
How to save and get value from JSP  Employee Name Time... 2012-12-12 save i want to save dis value jsp to action ...how can i get all value ..and store..how can its values goes ... (adsbygoogle
how to get selected name from combo box
how to get selected name from combo box   i have fetched all records... question is that............. i havt to get particular id of worker on selection... answer me this q. urgently. With Regards, Vishwnath. (adsbygoogle
how to get selected name from combo box
how to get selected name from combo box   i have fetched all records... question is that............. i havt to get particular id of worker on selection... answer me this q. urgently. With Regards, Vishwnath. (adsbygoogle
how to get selected name from combo box
how to get selected name from combo box  i have fetched all records... question is that............. i havt to get particular id of worker on selection... me this q. urgently. With Regards, Vishwnath. (adsbygoogle
how to get selected name from combo box
how to get selected name from combo box  i have fetched all records... question is that............. i havt to get particular id of worker on selection... me this q. urgently. With Regards, Vishwnath. (adsbygoogle
how to get selected name from combo box
how to get selected name from combo box  i have fetched all records... question is that............. i havt to get particular id of worker on selection... me this q. urgently. With Regards, Vishwnath. (adsbygoogle
how to get data from database into dropdownlist in jsp
how to get data from database into dropdownlist in jsp  //Customer Name: <select name="name"> <% try{ Class.forName...="select * from staff"; ResultSet rs=st.executeQuery(sql); while(rs.next
how to get data from sap - WebSevices
how to get data from sap   Hi all, I am new to java family. I...: I need to buils a system with few fields from sap and few fields from other third party system. how to fetch the data from sap and other third party system
How to send request to the visa/master card to get verifed the credit card - Security
How to send request to the visa/master card to get verifed the credit card  we are accepting the credit card number, So how to forward request get verified the card number. Thanks You
how to get data from database into dropdownlist in jsp
how to get data from database into dropdownlist in jsp  Customer Name:<select name="name"> <% try{ Class.forName...=con.createStatement(); String sql="select * from staff"; ResultSet rs
how to get data from database into dropdownlist in jsp
how to get data from database into dropdownlist in jsp  Can anybody tell me what is the problem in this code as i am not able to fetch the data from... the link JSP Get Data Into Dropdown list From Database   Thanq for reply
how to get the values to dropdownlist from oracle database
how to get the values to dropdownlist from oracle database   </script> </head> <body> <select name... * from countryname"); while(rs.next()){ %> <option value="<
how to get a values from processRecord - JSP-Servlet
how to get a values from processRecord  dear sir, i have a problem in getting a values from java to jsp.Here jsp is used to read a excel sheet that intern call a java program so i want a column values i.e a column contains
How to send URL in Post request from local server to remote server?
How to send URL in Post request from local server to remote server?  ... = window.adsbygoogle || []).push({}); 2> How to send a Post Request containing- "https... server(using Tomcat). Now i want: 1> How to establish a Connection
Session Object from request
getting cookie object from request object? I know all methods are available its relevant class. is there any reason for getting session object from request , adding cookie object into response, getting object from request object. one
Java Servlet : Http Request Headers
Java Servlet : Http Request Headers In this tutorial, you will learn how to Http Request Headers works in java servlet. Http Request Headers : HTTP Request Header is a request line text that a HTTP client(eg. web browser)sends
Retrieve HTTP Request Headers using JSP
Retrieve HTTP Request Headers using JSP   ... the request headers.ADS_TO_REPLACE_1 When a HTTP client sends a request, it is required...; request line indicating HTTP 1.1, it may be able to take advantage
How to convert http website into Https from java?
How to convert http website into Https from java?  could any one tell me how to convert http website into Https from java
ModuleNotFoundError: No module named 'http_request'
'http_request' How to remove the ModuleNotFoundError: No module named 'http_request' error? Thanks   Hi, In your python...ModuleNotFoundError: No module named 'http_request'  Hi, My Python
How to get xml file form http port using web service
How to get xml file form http port using web service  hi I am suresh i am using netbeans 6.9 to develop a web service for getting xml from http port... struck with getting xml file. How to access xml file in web service? Please Help
How to get the values from the Combo Box - JSP-Servlet
How to get the values from the Combo Box   Sir, Actually i am getting the values in the combo box from table.I want what ever... ("Browser does not support HTTP Request") return } var url="getuser.jsp
How to get bytes from ByteBuffer in java.
How to get bytes from ByteBuffer in java.       ...;  In this tutorial, we will discuss how to get bytes from buffer... class  fills byte buffer created by using allocate() method. The get
JSP Locale
The Tutorial illustrate you how to get the locale from the jsp page... JSP Locale          JSP Locale is used to get the preferred locale
How to get jSon object in servlet from jsp - JSP-Servlet
How to get jSon object in servlet from jsp  How to get jSon object in servlet from jsp? In jsp page i have written: var sel... variable in my configurationServlet. Can any one tel me how to get this json
How I get a variable from java script to use it in the scriptlet of jsp.
How I get a variable from java script to use it in the scriptlet of jsp.  Hi i want to get the tinyUrl value in the scriptlet of jsp. // Bit.ly API var url="http://cplive.com/dealDetail.do?method=displayDealDetail&
How to get month name from date(like-25/06/2012) using java?
How to get month name from date(like-25/06/2012) using java?  How to get month name from date(like-25/06/2012) using java
JavaScript XML Http Request
JavaScript XML Http Request  ...;, url, true) to make a GET request for the given url 'data.xml'. It provides... how to access XML file with an XMLHTTP object using JavaScript
how to get the data from database - Java Server Faces Questions
how to get the data from database  In database i have created some... me the procedure how to do this application.Database is mysql.  Hi friend, For JSF Search Application visit to : http://www.roseindia.net/jsf

Ads