Spring MVC / HttpServletRequest
by rlaehddnd0422HttpServletRequest
HTTP ์์ฒญ ๋ฉ์์ง๋ฅผ ํ์ฑํด์ ๊ฒฐ๊ณผ๋ฅผ HttpServletRequest ๊ฐ์ฒด์ ๋ด์์ ์ ๊ณตํฉ๋๋ค.
HttpServletRequest๋ฅผ ์ฌ์ฉํ๋ฉด HTTP ์์ฒญ ๋ฉ์์ง๋ฅผ ํธ๋ฆฌํ๊ฒ ์กฐํํ ์ ์์ต๋๋ค.
HTTP ์์ฒญ ๋ฉ์์ง
POST /save HTTP/1.1 // start-line
Host : localhost:8080 // header
Content-Type : application/x-www-unlencoded
username=dongwoong&age=25 // body
์ถ๊ฐ์ ์ผ๋ก ์์ ์ ์ฅ์ ๊ธฐ๋ฅ, ์ธ์ ๊ด๋ฆฌ ๊ธฐ๋ฅ๋ ์ ๊ณตํฉ๋๋ค.
1) ์์ ์ ์ฅ์ ๊ธฐ๋ฅ์ ํด๋น HTTP ์์ฒญ์ด ์์๋ถํฐ ๋๋ ๋ ๊น์ง ์ ์ง๋๋ ์์์ ์ฅ์ ๊ธฐ๋ฅ์ผ๋ก,
setAttribute(name,value)๋ฅผ ํตํด ์ ์ฅ
getAttribute(name)์ ํตํ value ์กฐํ๊ฐ ๊ฐ๋ฅํฉ๋๋ค.
2) ์ธ์ ๊ด๋ฆฌ๊ธฐ๋ฅ์ getSession(create:true)๋ฅผ ํตํด ์ธ์ ์ ๊ด๋ฆฌํ๋๋ก ์ค์ ํ ์ ์์ต๋๋ค.
HttpServletRequest, HttpServletResponse ๋ฅผ ๋ช ํํ ์ดํดํ๋ ค๋ฉด HTTP ์คํ์ด ์ ๊ณตํ๋ request, response ๋ฉ์์ง ์์ฒด๋ฅผ ์ดํดํด์ผ ํ๊ธฐ ๋๋ฌธ์ ์ด ์ ํฌ์คํ ์ ์ฐธ๊ณ .
https://rlaehddnd0422.tistory.com/45
https://rlaehddnd0422.tistory.com/42
HttpServletRequest ๊ธฐ๋ณธ ์ฌ์ฉ๋ฒ
package hello.servlet.basic.request;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name="requestHeaderServlet", urlPatterns = "/request-header")
public class RequestHeaderServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
{
printStartLine(request);
printHeaders(request);
printHeaderUtils(request);
printEtc(request);
}
private void printStartLine(HttpServletRequest request) {
System.out.println("--- REQUEST-LINE - start ---");
System.out.println("request.getMethod() = " + request.getMethod()); //GET
System.out.println("request.getProtocol() = " + request.getProtocol()); // HTTP 1.1
System.out.println("request.getScheme() = " + request.getScheme()); //http
// http://localhost:8080/request-header
System.out.println("request.getRequestURL() = " + request.getRequestURL());
// /request-header
System.out.println("request.getRequestURI() = " + request.getRequestURI());
//username=hi
System.out.println("request.getQueryString() = " +
request.getQueryString());
System.out.println("request.isSecure() = " + request.isSecure()); //http ์ฌ์ฉ ์ ๋ฌด
System.out.println("--- REQUEST-LINE - end ---");
System.out.println();
}
private void printHeaders(HttpServletRequest request) {
System.out.println("--- Headers - start ---");
/*
Enumeration<String> headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
System.out.println(headerName + ": " + request.getHeader(headerName));
}
*/
request.getHeaderNames().asIterator()
.forEachRemaining(headerName -> System.out.println(headerName + ":" + request.getHeader(headerName)));
System.out.println("--- Headers - end ---");
System.out.println();
}
private void printHeaderUtils(HttpServletRequest request)
{
System.out.println("--- Header ํธ์ ์กฐํ start ---");
System.out.println("[Host ํธ์ ์กฐํ]");
System.out.println("request.getServerName() = " + request.getServerName()); //Host ํค๋
System.out.println("request.getServerPort() = " + request.getServerPort()); //Host ํค๋
System.out.println();
System.out.println("[Accept-Language ํธ์ ์กฐํ]"); request.getLocales().asIterator()
.forEachRemaining(locale -> System.out.println("locale = " + locale));
System.out.println("request.getLocale() = " + request.getLocale());
System.out.println();
System.out.println("[cookie ํธ์ ์กฐํ]");
if (request.getCookies() != null) {
for (Cookie cookie : request.getCookies())
{
System.out.println(cookie.getName() + ": " + cookie.getValue());
}
}
System.out.println();
System.out.println("[Content ํธ์ ์กฐํ]");
System.out.println("request.getContentType() = " + request.getContentType());
System.out.println("request.getContentLength() = " + request.getContentLength());
System.out.println("request.getCharacterEncoding() = " + request.getCharacterEncoding());
System.out.println("--- Header ํธ์ ์กฐํ end ---");
System.out.println();
}
private void printEtc(HttpServletRequest request) { System.out.println("--- ๊ธฐํ ์กฐํ start ---");
System.out.println("[Remote ์ ๋ณด]");
System.out.println("request.getRemoteHost() = " +
request.getRemoteHost()); //
System.out.println("request.getRemoteAddr() = " +
request.getRemoteAddr()); //
System.out.println("request.getRemotePort() = " +
request.getRemotePort()); //
System.out.println();
System.out.println("[Local ์ ๋ณด]");
System.out.println("request.getLocalName() = " +
request.getLocalName()); //
System.out.println("request.getLocalAddr() = " +
request.getLocalAddr()); //
System.out.println("request.getLocalPort() = " +
request.getLocalPort()); //
System.out.println("--- ๊ธฐํ ์กฐํ end ---");
System.out.println();
}
}
Start-Line ์ ๋ณด
- getMethod() : ๋ฉ์๋ ์ ๋ณด๋ฅผ ๋ฆฌํด ( GET, POST .. )
- getProtocol() : ํ๋กํ ์ฝ ์ ๋ณด๋ฅผ ๋ฆฌํด ( HTTP/1.1 )
- getScheme() : ํ๋กํ ์ฝ ์ ๋ณด๋ฅผ ๋ฆฌํด ( http, https .. )
- getRequestURL() : ์์ฒญ URL ๋ฆฌํด ( http://localhost:8080/request-header )
- getRequestURI() : URI๋ง ๋ฆฌํด ( request-header )
- getQueryString() : query ํ๋ผ๋ฏธํฐ ๋ฆฌํด ( ์์ผ๋ฉด null ๋ฆฌํด )
- isSecure() : https ์ฌ์ฉ ์ ๋ฌด ๋ฆฌํด
Header ์ ๋ณด
- getServerName() : ์๋ฒ ์ด๋ฆ ์กฐํ
- getServerPort() : ์๋ฒ ํฌํธ๋ฒํธ ์กฐํ
- getHeaderNames() : ํค๋ ์กฐํ
- asIterator().forEachRemaining()์ ํตํด ๋ชจ๋ ํค๋์ด๋ฆ๊ณผ ํค๋ ์ ๋ณด๋ฅผ ํ์ธํ ์ ์์ต๋๋ค.
- getLocale() : ์ฐ์ ์์ ๊ฐ์ฅ ๋์ Accept-Language ์กฐํ
- asIterator().forEachRemaining()์ ํตํด ๋ชจ๋ Language ์กฐํ๊ฐ ๊ฐ๋ฅํฉ๋๋ค.
- getCookies() : ์ฟ ํค ์กฐํ
- getContentType() : ์ฝํ ํธํ์ ์กฐํ
- getContentLength() : ์ฝํ ํธ ๊ธธ์ด ์กฐํ
- getCharacterEncoding : ์ธ์ฝ๋ฉ ๋ฐฉ์ ์กฐํ
request.getLocales().asIterator()
.forEachRemaining(locale -> System.out.println("locale = " + locale));
๊ทธ ์ธ ๊ธฐํ ์ ๋ณด ( ํฌํธ๋ฒํธ, ํธ์คํธ์ฃผ์ ๋ฑ๋ฑ ) ์กฐํ ๊ฐ๋ฅ.
์ถ๋ ฅ ๊ฒฐ๊ณผ
--- REQUEST-LINE - start ---
request.getMethod() = GET
request.getProtocol() = HTTP/1.1
request.getScheme() = http
request.getRequestURL() = http://localhost:8080/request-header
request.getRequestURI() = /request-header
request.getQueryString() = null
request.isSecure() = false
--- REQUEST-LINE - end ---
--- Headers - start ---
host:localhost:8080
connection:keep-alive
cache-control:max-age=0
sec-ch-ua:"Chromium";v="110", "Not A(Brand";v="24", "Google Chrome";v="110"
sec-ch-ua-mobile:?0
sec-ch-ua-platform:"macOS"
upgrade-insecure-requests:1
user-agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36
accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
sec-fetch-site:none
sec-fetch-mode:navigate
sec-fetch-user:?1
sec-fetch-dest:document
accept-encoding:gzip, deflate, br
accept-language:ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7
--- Headers - end ---
--- Header ํธ์ ์กฐํ start ---
[Host ํธ์ ์กฐํ]
request.getServerName() = localhost
request.getServerPort() = 8080
[Accept-Language ํธ์ ์กฐํ]
locale = ko_KR
locale = ko
locale = en_US
locale = en
request.getLocale() = ko_KR
[cookie ํธ์ ์กฐํ]
[Content ํธ์ ์กฐํ]
request.getContentType() = null
request.getContentLength() = -1
request.getCharacterEncoding() = UTF-8
--- Header ํธ์ ์กฐํ end ---
--- ๊ธฐํ ์กฐํ start ---
[Remote ์ ๋ณด]
request.getRemoteHost() = 0:0:0:0:0:0:0:1
request.getRemoteAddr() = 0:0:0:0:0:0:0:1
request.getRemotePort() = 60763
[Local ์ ๋ณด]
request.getLocalName() = localhost
request.getLocalAddr() = 0:0:0:0:0:0:0:1
request.getLocalPort() = 8080
--- ๊ธฐํ ์กฐํ end ---
HTTP ์์ฒญ ๋ฐ์ดํฐ
HTTP ์์ฒญ ๋ฉ์์ง๋ฅผ ํตํด ํด๋ผ์ด์ธํธ์์ ์๋ฒ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ๋ ๋ฐฉ๋ฒ์๋ ์ฃผ๋ก 3๊ฐ์ง ๋ฐฉ๋ฒ์ ์ฌ์ฉํฉ๋๋ค.
1. GET - ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ ์ฌ์ฉ
- ๋ฉ์์ง ๋ฐ๋ ์์ด, URL์ ์ฟผ๋ฆฌํ๋ผ๋ฏธํฐ์ ๋ฐ์ดํฐ๋ฅผ ํฌํจํด์ ์ ๋ฌํ๋ ๋ฐฉ์์ ๋๋ค.
- ex) ๊ฒ์, ํํฐ, ํ์ด์ง์์ ๋ง์ด ์ฌ์ฉํ๋ ๋ฐฉ์
- ์ฌ์ฉ ์์ : url?name=kim&age=25
package hello.servlet.basic.request;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
/*
* 1. ํ๋ผ๋ฏธํฐ ์ ์ก๊ธฐ๋ฅ
* http://localhost:8080/request-param?username=hello&age=20
*
*/
@WebServlet(name="requestParamServlet", urlPatterns = "/request-param")
public class RequestParamServlet extends HttpServlet
{
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
System.out.println("[์ ์ฒด ํ๋ผ๋ฏธํฐ ์กฐํ]-start");
request.getParameterNames().asIterator()
.forEachRemaining(paramName -> System.out.println(paramName + "=" + request.getParameter(paramName)));
System.out.println("[์ ์ฒด ํ๋ผ๋ฏธํฐ ์กฐํ]-end");
System.out.println();
System.out.println("[๋จ์ผ ํ๋ผ๋ฏธํฐ ์กฐํ]-start");
String name = request.getParameter("name");
System.out.println("name = " + name);
String age = request.getParameter("age");
System.out.println("age = " + age);
System.out.println();
System.out.println("[์ด๋ฆ์ด ๊ฐ์ ๋ณต์ ํ๋ผ๋ฏธํฐ ์กฐํ]");
String[] names = request.getParameterValues("name");
for (String s : names) {
System.out.println(s);
}
response.getWriter().write("OK.");
}
}
- localhost:8080/request-param์ ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ๋ฅผ ์ถ๊ฐํ URI์ธ localhost:8080/request-param?name=kim&age=20๋ฅผ ์๋ฒ์ ์์ฒญํฉ๋๋ค.
- HttpServletRequest ๊ฐ์ฒด์ธ request์ ์์ฒญ ์ ๋ณด(ํ๋ผ๋ฏธํฐ)๋ฅผ ๋ด์์ ์๋ฒ์ ์ ์กํฉ๋๋ค.
- ์๋ฒ๋ HTTP ์๋ต ๋ฉ์์ง๋ฅผ HttpServletResponse ๊ฐ์ฒด์ ๋ด์ ํด๋ผ์ด์ธํธ์ ์ ์กํฉ๋๋ค.
request.getParameter("ํ๋ผ๋ฏธํฐ ์ด๋ฆ") ์ ํตํด ์์ฒญํ ๋ ์๋ฒ์ ๋ณด๋ธ ํ๋ผ๋ฏธํฐ ์ด๋ฆ์ ๋ํ ๊ฐ์ ์กฐํํ ์ ์์ต๋๋ค.
์ค๋ณต๋ ํ๋ผ๋ฏธํฐ์ ๊ฒฝ์ฐ request.getParameterValues()๋ฅผ ์ฌ์ฉํ๋ฉด ์ค๋ณต ํ๋ผ๋ฏธํฐ์ ๋ํ ๊ฐ๋ค์ ๋ชจ๋ ์กฐํํ ์ ์์ต๋๋ค.
2. POST - HTML FORM ์ฌ์ฉ
- content-type : application/x-www-form-urlencoded ๋ฅผ ์ฌ์ฉํฉ๋๋ค.
- ๋ฉ์์ง ๋ฐ๋์ ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ ํ์์ผ๋ก ์ ๋ฌํ๋ ๋ฐฉ์์ ๋๋ค.
- ex) ํ์ ๊ฐ์ , ์ํ ์ฃผ๋ฌธ
html-form.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/request-param" method="post">
username: <input type="text" name="name" /> age: <input type="text" name="age" /> <button type="submit">์ ์ก</button>
</form>
</body>
</html>
์ฌ๊ธฐ์ ์์ฑํ ํผ์ /request-param์ POST ๋ฐฉ์์ผ๋ก ์ ๋ฌ๋ฉ๋๋ค.
์ด๋ ๊ฒ ์์ฑํ ๋ค์ ์ ์ก์ ๋๋ฅด๋ฉด request message body์ ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ ํ์์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํฉ๋๋ค.
name=kdo6301&age=11 ์ด ๋ฉ์์ง ๋ฐ๋์ ๋ด๊ฒจ /request-param์ผ๋ก ์ ๋ฌ๋ฉ๋๋ค.
GET URL ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ ํ์์ผ๋ก ํด๋ผ์ด์ธํธ์์ ์๋ฒ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ ๋๋ HTTP ๋ฐ๋๋ฅผ ์ฌ์ฉํ์ง ์๊ธฐ ๋๋ฌธ์ Content-type์ด ์์ง๋ง, POST HTML Form ํ์์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ๋ฉด HTTP ๋ฉ์์ง ๋ฐ๋์ ํด๋น ๋ฐ์ดํฐ๋ฅผ ํฌํจํด์ ๋ณด๋ด๊ธฐ ๋๋ฌธ์ ๋ฐ๋์ ํฌํจ๋ ๋ฐ์ดํฐ๊ฐ ์ด๋ค ํ์์ธ์ง Content-type์ ๊ผญ ์ง์ ํด์ผ ํฉ๋๋ค.
์ด๋ ๊ฒ ํผ์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์กํ๋ ํ์์ Content-type : application/x-www-form-urlencoded์ ์ฌ์ฉํฉ๋๋ค.
3. HTTP message body์ ๋ฐ์ดํฐ๋ฅผ ์ง์ ๋ด์์ ์์ฒญ
- ์ฃผ๋ก HTTP API์์ ์ฃผ๋ก ์ฌ์ฉํฉ๋๋ค. (json, xml, text)
- ๋ฐ์ดํฐ ํ์์ ์ฃผ๋ก json ์ฌ์ฉ ( POST, PUT, PATCH )
3-1. TEXT
HTTP message body์ ๋ฐ์ดํฐ๋ฅผ ์ง์ ๋ด์์ ์์ฒญ
package hello.servlet.basic.request;
import org.springframework.util.StreamUtils;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
@WebServlet(name="requestBodyStringServlet", urlPatterns = "/request-body-string")
public class RequestBodyStringServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletInputStream inputStream = request.getInputStream();
String messagebody = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);
System.out.println("messagebody = " + messagebody);
response.getWriter().write("OK.");
}
}
inputStream์ ๋ฐ์ดํธ ์ฝ๋๋ฅผ ๋ฐํํฉ๋๋ค.
์ฆ, request์ message body๋ฅผ ๋ฐ์ดํธ์ฝ๋๋ก ๋ฐํํ์ฌ inputStream์ ์ ์ฅ์ด ๋๋๋ฐ, ์ฐ๋ฆฌ๊ฐ ์ฝ์ ์ ์๋ ์คํธ๋ง์ผ๋ก ๋ณด๋ ค๋ฉด Charset์ UTF-8๋ก ์ง์ ํด์ผ ํฉ๋๋ค.
์ฌ๊ธฐ์์ StreamUtils๋ฅผ ์ฌ์ฉํด inputStream์ ์ ์ฅ๋ ์คํธ๋ง์ messagebody์ utf-8๋ก ๋ณํํ์ฌ ์ ์ฅํด์ค๋๋ค.
message body๋ฅผ ์ด์ฉํด ๋จ์ TEXT๋ฅผ ์ ์กํ๊ธฐ ์ํด์๋ content-type์ text/plain๋ก ์ง์ ํด ์ฃผ์ด์ผํฉ๋๋ค.
Send ๊ฒฐ๊ณผ
3-2. JSON
JSON ํ์ ์ ์ก.
content-type : application/json
message body : {"name" : "kimjson", "age" : 25}
package hello.servlet.basic;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Getter @Setter @ToString
public class HelloData {
private String name;
private int age;
}
package hello.servlet.basic.request;
import com.fasterxml.jackson.databind.ObjectMapper;
import hello.servlet.basic.HelloData;
import org.springframework.util.StreamUtils;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
@WebServlet(name="requestBodyJsonServlet", urlPatterns = "/request-body-json")
public class RequestBodyJsonServlet extends HttpServlet {
private ObjectMapper objectMapper = new ObjectMapper();
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletInputStream inputStream = request.getInputStream();
String messagebody = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);
System.out.println("messagebody = " + messagebody);
HelloData helloData = objectMapper.readValue(messagebody, HelloData.class);
System.out.println("helloData.getAge() = " + helloData.getAge());
System.out.println("helloData.getName() = " + helloData.getName());
System.out.println("helloData = " + helloData);
response.getWriter().write("OK");
}
}
์ฐธ๊ณ : JSON ๊ฒฐ๊ณผ๋ฅผ ํ์ฑํด์ ์ฌ์ฉํ ์ ์๋ ์๋ฐ ๊ฐ์ฒด๋ก ๋ณํํ๋ ค๋ฉด Jackson, Gson ๊ฐ์ JSON ๋ณํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ถ๊ฐํด์ ์ฌ์ฉํด์ผ ํฉ๋๋ค.
์คํ๋ง ๋ถํธ๋ก Spring MVC๋ฅผ ์ ํํ๋ฉด ๊ธฐ๋ณธ์ผ๋ก Jackson ๋ผ์ด๋ธ๋ฌ๋ฆฌ ( ObjectMapper )๋ฅผ ํจ๊ป ์ ๊ณตํด์ฃผ๊ธฐ ๋๋ฌธ์ ์ฌ๊ธฐ์์๋ ObjectMapper๋ฅผ ์ด์ฉํด json ๊ฒฐ๊ณผ๋ฅผ ๊ฐ์ฒด์ ํ์ฑํ์์ต๋๋ค.
'๐ Backend > MVC Pattern' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Front-Controller ํจํด (0) | 2023.02.27 |
---|---|
Spring / MVC ํจํด (0) | 2023.02.24 |
Spring / ์์ Servlet ํจํด, JSP ํจํด (0) | 2023.02.23 |
Spring MVC / HttpServletResponse (0) | 2023.02.22 |
Spring MVC / ์น ์ ํ๋ฆฌ์ผ์ด์ (0) | 2023.02.21 |
๋ธ๋ก๊ทธ์ ์ ๋ณด
Study Repository
rlaehddnd0422