request获取ip

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String ip = "192.168.1.11";
        String remote = request.getRemoteAddr();//获得远程访问的ip
        if (remote.equals(ip)) {
            response.sendError(404, "您访问的页面不存在");
        } else {
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().print("您的ip是:<h1>" + remote + "</h1>");
        }
    }
原文地址:https://www.cnblogs.com/sflik/p/4567866.html