0

I'm using a Glassfish webserver to run my website. I'm still developing the website and I'd like to trace the location from where people attempt to use the website. In particular, I would like to be able to block all of Africa and the Middle East from accessing my website in an attempt to prevent fraud. I'd additionally like to know someone's location to show them related content to their location on the website. For example, if a user accesses the site from Dallas, Texas he would see Dallas, Texas content.

I'm using jsp/Java and would prefer such a solution.

I guess this is done by somehow tracing an IP address to a location.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
user817129
  • 522
  • 3
  • 10
  • 19

2 Answers2

0

You can use this Geo API from JavaScript to detect where the user comes from and handle the redirect using window.load. I don't find this as the right approach since users can block JavaScript execution in their browsers.

On the other hand, you can use MaxMind IP GeoLocation database and use some business logic to detect the IP addres using a Servlet-Filter and method ServletRequest#getRemoteAddr():

@WebFilter("/*")
public class IPFilter implements Filter {
    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
        throws IOException, ServletException {
        String clientIp = req.getRemoteAddr();
        if (bannedIp(clientIp) {
            response.sendRedirect(request.getContextPath() + "/bannedIp.jsp");
            return;
        }
        chain.doFilter(req, res);
    }
    private boolean bannedIp(String clientIp) {
        //add logic that detects if IP comes from a banned location...
    }
}
Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
0

I can give you two links. One that would identify country name by analyzing ip, and then block it web filtering.

  1. Method for determining country from ip: link
  2. Web filter class that was given in another stackoverflow post: link
Community
  • 1
  • 1
QuestionEverything
  • 4,809
  • 7
  • 42
  • 61
  • Your last link is Tomcat specific. You should add this in your answer as well (since probably won't work for GlassFish or WebLogic). – Luiggi Mendoza Jun 09 '13 at 04:35