HackToHell. Powered by Blogger.

A Java IP Logger for Google App engine with geolocation

I have created an ip logger in java along with an geo locator.

I thought that it would be a nice thing to share it with you.

How it works?

It gets the IP of the client using getRemoteHost() method to get the ip off the request and uses http://geoip.wtanaka.com to get the location of the ip!
Additionally it diplays the country flag.

Go check it out at
http://jiplogger.appspot.com/log

Here is the source.

package com.hth.iplogger;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;


import javax.servlet.http.*;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Text;



@SuppressWarnings("serial")
public class IpLoggerServlet extends HttpServlet {

DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
Entity loger = new Entity("Writeit");
loger.setProperty("ip",req.getRemoteHost().toString());
loger.setProperty("Date", "today");
StringBuilder sg = get("http://geoip.wtanaka.com/cc/"+req.getRemoteHost());
Text sv = new Text(sg.toString());
loger.setProperty("location",sv );

datastore.put(loger);
PrintWriter pw = resp.getWriter();
pw.println("<IMG SRC=http://geoip.wtanaka.com/flag/"+sg.toString()+".gif />");


}
public StringBuilder get(String q){
StringBuilder builder = new StringBuilder();
try{
URL url = new URL(q);
URLConnection urlc = url.openConnection();


BufferedInputStream buffer = new BufferedInputStream(urlc.getInputStream());


int byteRead;
while ((byteRead = buffer.read()) != -1)
builder.append((char) byteRead);

buffer.close();




} catch (MalformedURLException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
return builder;
}
}

Learn how to program in Google App engine

Share on Google Plus

About hacktohell

Love technology.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment