"Servlet" Javaweb, using the Sina API interface, obtain the IP address, and obtain the corresponding IP attribution place

Source: Internet
Author: User

This is the reason to call the Sina API interface, mainly to avoid our local storage of an IP attribution repository,

At the same time, we have to store the user's IP locally, only the IP can be stored, no need to store its location, save a field.

For example, write a Web page with the IP address of the client:


First, the directory structure in Eclipse is as follows:


In addition to the servlet support package, there is a. jsp with A. java.

The Servlet3.0 is used here, so web. XML has nothing:

<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee/http Java.sun.com/xml/ns/javaee/web-app_3_0.xsd "version=" 3.0 "></web-app>

Later, index.jsp through JavaScript Ajax, to Ipgettest.java to get the corresponding JSON. The interaction between JavaScript and Servlet has been stated in the "servlet" Servlet3.0 and pure JavaScript through Ajax (click to open link), which is not mentioned here! The following is the code in INDEX.JSP:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">Index.jsp will get the following JSON from the Ipgettest.java:

Which http://ipapi.sinaapp.com/api.php?f=text&ip=xxx this Sina API, if F=text returns text information, if F=json returns a JSON string, Of course, this JSON string also needs itself as shown in the code, the need to speak utf-8 encoding through decodeuricomponent, into Chinese.

The starting and ending addresses here are the start and end addresses of the IP of the network segment.


The key is Ipgettest.java this file, this file in three steps, first through the request object, to obtain the user access to the IP, and then through Java to remote access to data technology to obtain Web content.

Get the text information, print directly to the console, get the JSON information to print directly to the servlet above.

Package Ipget;import Java.io.*;import Java.net.url;import java.util.scanner;import javax.servlet.*;import Javax.servlet.http.*;import javax.servlet.annotation.*; @WebServlet (name = "Ipgettest", Urlpatterns = {"/ipgettest"}) public class Ipgettest extends HttpServlet {private static final long Serialversionuid = 1l;private Scanner Scanner;protec Ted Void Service (HttpServletRequest request,httpservletresponse response) throws Servletexception, IOException {// These two lines are for the servlet to be able to print the data normally printstream out = new PrintStream (Response.getoutputstream ()); Response.setcontenttype (" Text/html;charset=utf-8 ");//Get client ipstring IP =" "; if (Request.getheader (" x-forwarded-for ") = = null) {IP = Request.getremoteaddr ();} else {IP = request.getheader ("X-forwarded-for");} System.out.print ("Your IP is:" + IP + "\ n");//Gets the text information of the IP attribution StringBuilder iptext = new StringBuilder (""); String url = "http://ipapi.sinaapp.com/api.php?f=text&ip=" + Ip;scanner = new scanner (new URL). OpenConnection () . getInputStream (), "Utf-8"); wHile (Scanner.hasnext ()) {Iptext.append (Scanner.next ());} System.out.println (Iptext);//Get the JSON information for IP attribution StringBuilder ipjson = new StringBuilder (""); url = "http:// ipapi.sinaapp.com/api.php?f=json&ip= "+ Ip;scanner = new scanner (new URL). OpenConnection (). getInputStream (), "Utf-8"); while (Scanner.hasnext ()) {Ipjson.append (Scanner.next ());} Out.println (Ipjson);}}

Because the client's IP can only be obtained through the server language, this is the only way to do so.

Here Java obtains remote information, through the "Java" Print stream and buffer reader to complete the input and output to file operations (click the Open link) in the use of scanner to transform the "Java" read the content in the Web page (click Open link) in the way of buffer readers to obtain remote information.

"Servlet" Javaweb, using the Sina API interface, obtain the IP address, and obtain the corresponding IP attribution place

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.