자바 IP 주소, 운영 체제 유형, 핸드폰 모델 가 져 오기

4671 단어 서버 문제
package com.suobei.web;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by wangmin on 2018/01/21.
 */
@WebServlet("/user")
public class UserServlet extends HttpServlet{

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String referrer = request.getHeader("Referer");
        System.out.println(referrer);
        String remoteAddr = request.getRemoteAddr();
        if(remoteAddr.equals("0:0:0:0:0:0:0:1")){
            System.out.println("  ip   :127.0.0.1");
        }else{
            System.out.println("  ip   :" + remoteAddr);
        }
        String requestHeader = request.getHeader("User-Agent");
        int index_one = requestHeader.indexOf("(");
        String requestBody = requestHeader.substring(index_one+1);
        String userInfo = requestBody.substring(0, requestBody.indexOf(")"));
        String[] userInfoList = userInfo.split(";");
        int length = userInfoList.length;
        String os = userInfoList[0];
        String mobileInfo = userInfoList[length - 1];
        if(os.equals("Windows NT 6.1")){
            System.out.println("       :windows7");
        }else{
            System.out.println("       :" + os);
        }
        int index = mobileInfo.indexOf("/");
        if(index > 0){
            mobileInfo = mobileInfo.substring(0, mobileInfo.indexOf("/") - 5);
            System.out.println("       :" + mobileInfo);
        }

    }
}

좋은 웹페이지 즐겨찾기