如何在java中获取当前国家名称?
我想用java获取当前的国家名称。我使用这个代码如何在java中获取当前国家名称?,java,servlets,httprequest,Java,Servlets,Httprequest,我想用java获取当前的国家名称。我使用这个代码 private HttpServletRequest request; Locale currentCountry = request.getLocale(); System.out.println(currentCountry.getDisplayCountry()); 但是它是通过一个NullPointerException实现的,比如:java.lang.NullPointerException。在二线。请帮助我任何人。代码片段不足
private HttpServletRequest request;
Locale currentCountry = request.getLocale();
System.out.println(currentCountry.getDisplayCountry());
但是它是通过一个NullPointerException实现的,比如:
java.lang.NullPointerException
。在二线。请帮助我任何人。代码片段不足以告诉您为什么得到空指针。我假设您在服务器端使用servlet
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Locale;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HttpServletBean;
public class MyServlet extends HttpServletBean {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
PrintWriter out = response.getWriter();
Locale userlocale = request.getLocale();
out.println("Your Country : " + userlocale.getCountry());
out.println();
out.println("");
}
}
你知道NullPointerException是什么吗?哪个国家的名称?服务器,客户端?谢谢Jens。我要客户国家/地区名称。可能是