상황 : "모바일인지 아닌지 확인하여 페이지 분기하라"
js,jsp에서 스크립트로 해도 되나 상황에 따라서 자바코드로 해야할 경우가 있다.
@RequestMapping(value="/login.do")
public String loginPage(Model, model, HttpServletRequest request) {
String userOS = request.getHeader("user-agent");
Pattern userAgent = Pattern.compile("(Android|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini)", Pattern.CASE_INSESITIVE);
Matcher matcher = userAgent.matcher(userOS);
if(matcher.find()){
return "redirect:/mobilePage.do?";
}
}
> HttpServletRequest로 요청헤더에 접속정보(user-agent)를 변수에 저장
> Pattern 클래스로 원하는 패턴을 생성
> Matcher 클래스로 생성된 Pattern객체에서 요청헤더 값과 매칭하는 값을 찾아서 저장.
> Matcher클래스의 .find()함수로 boolean값 반환.
'JAVA' 카테고리의 다른 글
이메일 전송구현시 사용하는 SSL,TSL이란? (0) | 2021.11.11 |
---|---|
toString()이란 뭘까? (0) | 2021.11.11 |
out.printf ("%s, %d", "문자열", 숫자); (0) | 2021.11.11 |
JD-GUI 디컴파일러 설치 & 사용 (0) | 2021.11.09 |
[소소한 설정] 자바 버전 변경에 따른 이클립스, 톰캣 설정 (0) | 2021.10.06 |