HttpServletRequest가 제공하는 편리한 기본 기능들(HTTP 메시지의 start-line, header 정보 조회)을 알아보자.
- start-line 정보
@WebServlet(name = "requestHeaderServlet", urlPatterns = "/request-header")
public class RequestHeaderServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
printStartLine(req);
printHeaders(req);
printHeaderUtils(req);
printEtc(req);
}
/**start line 정보*/
private void printStartLine(HttpServletRequest req) {
System.out.println("--- REQUEST-LINE - start ---");
System.out.println("request.getMethod() = " + req.getMethod()); //GET
System.out.println("request.getProtocol() = " + req.getProtocol()); //HTTP/1.1
System.out.println("request.getScheme() = " + req.getScheme()); //http
System.out.println("request.getRequestURL() = " + req.getRequestURL());
// http://localhost:8080/request-header
System.out.println("request.getRequestURI() = " + req.getRequestURI());
// request-header
System.out.println("request.getQueryString() = " + req.getQueryString()); //username=hello
System.out.println("request.isSecure() = " + req.isSecure()); //https 사용 유무
System.out.println("--- REQUEST-LINE - end ---");
System.out.println();
}
- 헤더 정보
/**모든 Header 정보*/
private void printHeaders(HttpServletRequest req) {
System.out.println("--- Headers - start ---");
//방법1
/*Enumeration<String> headerNames = req.getHeaderNames();
while (headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
System.out.println(headerName + ": " + req.getHeader(headerName));
}*/
//방법2
req.getHeaderNames().asIterator()
.forEachRemaining(headerName -> System.out.println(headerName + ": " + req.getHeader(headerName)));
System.out.println("--- Headers - end ---");
System.out.println();
}
- Header 편리한 조회
/**Header 편리한 조회*/
private void printHeaderUtils(HttpServletRequest req) {
System.out.println("--- Header 편의 조회 start ---");
System.out.println("[Host 편의 조회]");
System.out.println("request.getServerName() = " + req.getServerName()); //Host 헤더
System.out.println("request.getServerPort() = " + req.getServerPort()); //Host 헤더
System.out.println();
System.out.println("[Accept-Language 편의 조회]");
req.getLocales().asIterator()
.forEachRemaining(locale -> System.out.println("locale = " + locale));
System.out.println("request.getLocale() = " + req.getLocale());
System.out.println();
System.out.println("[cookie 편의 조회]");
if (req.getCookies() != null) {
for (Cookie cookie : req.getCookies()) {
System.out.println(cookie.getName() + ": " + cookie.getValue());
}
}
System.out.println();
System.out.println("[Content 편의 조회]");
System.out.println("request.getContentType() = " + req.getContentType());
System.out.println("request.getContentLength() = " + req.getContentLength());
System.out.println("request.getCharacterEncoding() = " + req.getCharacterEncoding());
System.out.println("--- Header 편의 조회 end ---");
System.out.println();
}
- GET URL 쿼리 파라미터 형식은 content-type이 없다. (null)
- content-type은 HTTP 메시지 바디의 데이터 형식
- 기타 정보
- 기타 정보는 HTTP 메시지의 정보는 아니다.
//기타 정보
private void printEtc(HttpServletRequest req) {
System.out.println("--- 기타 조회 start ---");
System.out.println("[Remote 정보]");
System.out.println("request.getRemoteHost() = " + req.getRemoteHost()); //
System.out.println("request.getRemoteAddr() = " + req.getRemoteAddr()); //
System.out.println("request.getRemotePort() = " + req.getRemotePort()); //
System.out.println();
System.out.println("[Local 정보]");
System.out.println("request.getLocalName() = " + req.getLocalName()); //
System.out.println("request.getLocalAddr() = " + req.getLocalAddr()); //
System.out.println("request.getLocalPort() = " + req.getLocalPort()); //
System.out.println("--- 기타 조회 end ---");
System.out.println();
}
[스프링 MVC 8] 서블릿: HttpServletRequest (tistory.com)
[스프링 MVC 7] 서블릿: Hello 서블릿 (tistory.com)
728x90
반응형
'Spring Tutorial' 카테고리의 다른 글
[스프링 MVC 2] 웹 애플리케이션 이해: 서블릿 (0) | 2023.07.19 |
---|---|
[스프링 MVC 10] 서블릿: HTTP 요청 데이터 (*중요) (0) | 2023.07.19 |
[스프링 MVC 8] 서블릿: HttpServletRequest (0) | 2023.07.17 |
[스프링 MVC 7] 서블릿: Hello 서블릿 (0) | 2023.07.17 |
[스프링 MVC 6] 서블릿: 프로젝트 생성 (0) | 2023.07.17 |