티스토리 뷰

Mobile기기에서 접속을 했는지 PC에서 접속을 했는지에 따라 화면을 달리 보여줘야 하는 상황이 있다. 이럴 경우 여러가지 방법이 있지만 JAVA 단에서 할수 있는 가장 간단한 방법에 대해서 기술하고자 한다. 

private static final String IS_MOBILE = "MOBI";
private static final String IS_PC = "PC";

public static String isDevice(HttpServletRequest req) {
    String userAgent = req.getHeader("User-Agent").toUpperCase();
		
    if(userAgent.indexOf(IS_MOBILE) > -1) {
         return IS_MOBILE;
    } else {
        return IS_PC;
    }
}

HttpServletRequest를 받을 수 있는곳 어디에서든지 Mobi라는 키워드로 위의 메소드로 판별을 할 수 있다. 

안드로이드와 IOS 등등 세밀하게 구분하고 싶다면 위의 분기문에 추가를 해주면 된다. 

 

다음 글을 참고하여 작성하였다. 

The following table summarizes the way major browser vendors indicate that their browsers are running on a mobile device:

Common browsers User Agent strings브라우저규칙예제

브라우저 규칙 예제
Mozilla (Gecko, Firefox) Mobile or Tablet token in the comment. Mozilla/5.0 (Android; Mobile; rv:13.0) Gecko/13.0 Firefox/13.0
WebKit-based (Android, Safari) Mobile Safari token outside the comment. Mozilla/5.0 (Linux; U; Android 4.0.3; de-ch; HTC Sensation Build/IML74K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30
Blink-based (Chromium, Google Chrome, Opera 15+) Mobile Safari token outside the comment Mozilla/5.0 (Linux; Android 4.4.2); Nexus 5 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Mobile Safari/537.36 OPR/20.0.1396.72047
Presto-based (Opera 12-) Opera Mobi/xyz token in the comment (Opera 12-) Opera/9.80 (Android 2.3.3; Linux; Opera Mobi/ADR-1111101157; U; es-ES) Presto/2.9.201 Version/11.50
Internet Explorer IEMobile/xyz token in the comment. Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0)

In summary, we recommend looking for the string “Mobi” anywhere in the User Agent to detect a mobile device.

출처 : https://developer.mozilla.org/ko/docs/Web/HTTP/Browser_detection_using_the_user_agent

 

끝!

 

댓글
최근에 올라온 글
최근에 달린 댓글
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31