코드로 주소정보 가져오기(java)

Sunny·2022년 7월 22일
0

🌱 http/https, 서버주소, 포트 가져오기

  • request.getScheme() : http 또는 https를 반환
  • request.getServerName() : server 도메인명 반환
  • request.getServerPort() : Port 반환

ex)

String path=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort();

/* 
만약에 path가 http://localhost:8080 이라면
request.getScheme() 은 http 반환
request.getServerName() 은 localhost 반환
request.getServerPort() 는 8080 반환
*/

❕ 참고로 운영용 도메인 주소보면 port가 보이지 않아 위의 예시에서 request.getServerPort()가 적용될까 싶지만, 사실 포트는 숨겨져서 붙고있기 때문에 걱정안해도 된다.

ex) www.naver.com:80


🌱 전체 주소/일부 경로 가져오기

  • request.getRequestURL() : 전체 주소 정보 반환
  • request.getRequestURI() : contextPath 이후 경로 반환

ex)

String path1 = request.getRequestURL();
String path2 = request.getRequestURI(); 

/* 
만약에 path1이 http://localhost:8080/board/main.do 이라면
request.getRequestURL() 은 http://localhost:8080/board/main.do 반환

만약에 path2가 http://localhost:8080/board/main.do 이라면
request.getRequestURL() 은 /board/main.do 반환

*/
profile
개발에 재미를 붙여보기 :)

0개의 댓글