본문 바로가기

JAVA

[JAVA] HTTP requst 정보 (클라이언트 IP, 헤더, URL 등) 총 정리

자주 사용하는데 막상 사용하려고 하면 메서드명이 잘 생각나지 않아서 request 정보 정리했습니다. 

사용자 로그를 저장하거나, 사용자 접속환경 등을 판단할 때 자주 사용했습니다. 

JSP 파일로 만들어서 사용했던거라 <%-- --%> 문구가 추가되어 있습니다. 

 

 

<%@ page import="java.util.*" %>
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page import="java.net.InetAddress" %>

■ local의 기본 정보 (IP, Name, Port)    
Local IP : <%=request.getLocalAddr()%>
Local Name : <%=request.getLocalName()%>
Local Port : <%=request.getLocalPort()%>

■ 클라이언트의 정보
Remote IP : <%=request.getRemoteAddr()%>
Remote Host : <%=request.getRemoteHost()%>
Remote Port : <%=request.getRemotePort()%>

■ 서버 이름과 포트 (일반적으로 local 기본정보와 동일)
Server Name : <%=request.getServerName()%> (<%=InetAddress.getLocalHost().getHostAddress()%>)
Server Port : <%=request.getServerPort()%>

■ 지역 정보 (대부분 한국을 의미하는 ko가 나옴)
Locale : <%=request.getLocale()%>

■ 사용하는 프로토콜 ("프로토콜/메이저버전.마이너버전" 의 형태)
Protocol : <%=request.getProtocol()%>

■ http, https, ftp와 같은 것을 의미
Scheme : <%=request.getScheme()%>

■ https와 같은 보안 채널의 사용 여부 (true/false 값으로 되어 있음)
Secure Channel : <%=request.isSecure()%>

■ 요청에 대한 URI, URL, 컨텍스트 경로, 서블릿 경로, GET/POST등의 메소드
Request's URI : <%=request.getRequestURI()%>
Request's URL : <%=request.getRequestURL()%>
Context Path : <%=request.getContextPath()%>
Servlet Path : <%=request.getServletPath()%>
Method : <%=request.getMethod()%>

■ 세션 ID에 대한 정보
Session ID : <%=request.getRequestedSessionId()%>
Session ID from Cookie : <%=request.isRequestedSessionIdFromCookie()%>
Session ID from URL : <%=request.isRequestedSessionIdFromURL()%>
Session ID is still valid : <%=request.isRequestedSessionIdValid()%>

■ Header 정보
<%
Enumeration eHeader = request.getHeaderNames();
while (eHeader.hasMoreElements()) {
    String hName = (String)eHeader.nextElement();
    String hValue = request.getHeader(hName);
    out.println(hName + " : " + hValue + "");
}
%>

■ Request 객체를 통해서 쿠키 정보를 보는 방식
<%
Cookie cookies[] = request.getCookies();
for (int i=0; i < cookies.length; i++) {
    String name = cookies[i].getName();
    String value = cookies[i].getValue();
    out.println(name + " : " + value + "");
}
%>