HttpServletRequest 인터페이스는 서블릿에서 클라이언트로부터의 HTTP 요청에 대한 정보를 제공한다.
🔻클라이언트에서 서버로 전송할 때의 여러가지 관련 정보 가져오기
getRequestURI()
- 클라이언트가 요청한 URI(Uniform Resource Identifier)를 반환한다.
- 예를 들어, http://www.example.com/contextPath/servletPath/extraPath에서 /contextPath/servletPath/extraPath 부분을 반환한다.
getServerName()
- 현재 서버의 호스트명을 반환한다.
- 예를 들어, http://www.example.com에서 http://www.example.com 부분을 반환한다.
getMethod()
- 클라이언트가 요청한 HTTP 메소드를 반환한다. (GET, POST, PUT, DELETE 등)
- 예를 들어, 클라이언트가 GET 방식으로 요청했다면 "GET"을 반환한다.
getRemoteAddr()
- 클라이언트의 IP 주소를 반환한다.
- 클라이언트의 실제 IP 주소나 프록시 서버의 주소를 반환할 수 있다.
getContextPath()
- 현재 서블릿 컨텍스트의 경로를 반환한다.
- 예를 들어, http://www.example.com/contextPath/servletPath/extraPath에서 /contextPath 부분을 반환한다.
- context root를 고정보다 가변으로 설정하는 것이 안정성이 높은 방법이다.
예제코드
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ex10_request</title>
<link rel="stylesheet" href="http://pinnpublic.dothome.co.kr/cdn/example-min.css">
</head>
<body>
<h1>Request</h1>
<p>요청 URL: <%= request.getRequestURI() %></p>
<p>요청 서버 도메인: <%= request.getServerName() %></p>
<p>요청 방식: <%= request.getMethod() %></p>
<p>클라이언트 주소: <%= request.getRemoteAddr() %></p>
<p>컨텍스트 경로: <%= request.getContextPath() %></p>
<a href="ex09.jsp">9번 예제(상대경로)</a>
<a href="/jsp/ex09.jsp">9번 예제(절대경로)</a>
<!-- context root를 고정보다 가변으로 설정하는 것이 안정성이 높은 방법이다. -->
<a href="<%= request.getContextPath() %>/ex09.jsp">9번 예제(절대경로)</a>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="http://pinnpublic.dothome.co.kr/cdn/example-min.js"></script>
</body>
</html>
'SERVER(Servlet, JSP)' 카테고리의 다른 글
[JSP] JSP 객체 : session 객체 (0) | 2023.10.18 |
---|---|
[JSP] JSP 내장 객체 (request, pageContext, session, application ) (0) | 2023.10.18 |
[JSP] JSP 내장 객체와 request (0) | 2023.10.17 |
[JSP] JSP 구성요소 (JSP 지시자, 스크립트 요소, 액션 태그) (0) | 2023.10.17 |
[JSP] JSP 프로젝트 생성 (0) | 2023.10.17 |