728x90
๐ง HttpServletRequest
์๋ธ๋ฆฟ์ ์๋์ ๊ฐ์ HTTP ์์ฒญ ๋ฉ์์ง๋ฅผ ํ์ฑํด์ HttpServletRequest์ ๋ด์์ค๋๋ค.
POST /save HTTP/1.1
Host: localhost:8080
Content-Type: application/x-www-form-urlencoded
username=kim&age=20
...
ํ์ฑ๋ ์์ฒญ ์ ๋ณด๋ ์๋์ ๊ฐ์ด ์กฐํํ ์ ์์ต๋๋ค.
๐ง ํค๋ ์ ๋ณด ์กฐํํ๊ธฐ
/**
* REQUEST - START LINE ์ ๋ณด ์กฐํ
*/
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-test
System.out.println("request.getRequestURI() = " + request.getRequestURI());
// username=hi&password=123
System.out.println("request.getQueryString() = " + request.getQueryString());
// request.getParameter("username"); ๊ณผ ๊ฐ์ด ์ฌ์ฉ ๊ฐ๋ฅ
System.out.println("request.isSecure() = " + request.isSecure()); // https์ฌ์ฉ ์ ๋ฌด
System.out.println("--- REQUEST-LINE - end ---");
System.out.println();
}
/**
* REQUEST - HEADER ์ ๋ณด ์กฐํ
*/
private void printHeaders(HttpServletRequest request) {
System.out.println("--- Headers - start ---");
request.getHeaderNames()
.asIterator()
.forEachRemaining(headerName ->
System.out.println(headerName + ": "+request.getHeader(headerName)));
System.out.println("--- Headers - end ---");
System.out.println();
}
/**
* REQUEST - HEADER์ ๋ํ ํธ๋ฆฌํ ์กฐํ
*/
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();
}
๐ง ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ & Form ๋ฐ์ดํฐ์กฐํํ๊ธฐ
/**
* 1. ํ๋ผ๋ฏธํฐ ์ ์ก ๊ธฐ๋ฅ
* http://localhost:8080/request-param?username=hello&age=20&username=hi
*/
@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\n");
System.out.println("[๋จ์ผ ํ๋ผ๋ฏธํฐ ์กฐํ] - start");
String username = request.getParameter("username");
System.out.println("request.getParameter(username) = " + username);
String age = request.getParameter("age");
System.out.println("request.getParameter(age) = " + age);
System.out.println("[๋จ์ผ ํ๋ผ๋ฏธํฐ ์กฐํ] - end\n");
System.out.println("[์ด๋ฆ์ด ๊ฐ์ ๋ณต์ ํ๋ผ๋ฏธํฐ ์กฐํ] - start");
System.out.println("request.getParameterValues(username)");
String[] usernames = request.getParameterValues("username");
for (String name : usernames) {
System.out.println("username=" + name);
}
System.out.println("[์ด๋ฆ์ด ๊ฐ์ ๋ณต์ ํ๋ผ๋ฏธํฐ ์กฐํ] - end");
}
}
๋ค์๊ณผ ๊ฐ์ด ์์ฒญ์ ๋ณด๋์ต๋๋ค.
http://localhost:8080/request-param?username=hello&age=20&username=hi
๐ง API ๋ฉ์์ง ๋ฐ๋ ์กฐํํ๊ธฐ - ๋จ์ ํ ์คํธ
@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(); // message body์ ๋ด์ฉ์ ๋ฐ์ดํธ์ฝ๋๋ก ์ฝ์ด์จ๋ค.
String messageBody = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8); // ๋ฐ์ดํธ์ฝ๋(UTF_8 ์ธ์ฝ๋ฉ) -> ๋ฌธ์์ด
System.out.println("messageBody = " + messageBody);
}
}
์์ฒญ ์ ๋ณด๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
๐ง API ๋ฉ์์ง ๋ฐ๋ ์กฐํํ๊ธฐ - JSON
@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);
RequestData helloData = objectMapper.readValue(messageBody, RequestData.class);
System.out.println("helloData.username = " + helloData.getUsername());
System.out.println("helloData.age = " + helloData.getAge());
}
@Getter
@Setter
private static class RequestData {
private String username;
private int age;
}
}
์์ฒญ ์ ๋ณด๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
๐ง HttpServletRequest ์ถ๊ฐ ๊ธฐ๋ฅ
HttpServletRequest๋ ์์ ์ ์ฅ์ ๊ธฐ๋ฅ๊ณผ ์ธ์ ๊ด๋ฆฌ ๊ธฐ๋ฅ์ ์ ๊ณตํฉ๋๋ค.
์์ ์ ์ฅ์ ๊ธฐ๋ฅ์ ํด๋น Http ์์ฒญ์ ์์๋ถํฐ ์ฒ๋ฆฌ๊ฐ ๋๋ ๋ ๊น์ง ์ ์ง๋ฉ๋๋ค.
๊ฐ ์์ ์ ์ฅ
request.setAttribute(name, value)
๊ฐ ์กฐํ
request.getAttribute(name)
์ธ์ ์์ฑ
request.getSession(create: true)
Reference
728x90
'๐๏ธ Spring > Web MVC' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[MVC] ์์ฒญ ๋งคํ (@RequestMapping, PathVariable) (0) | 2021.12.29 |
---|---|
[MVC] HandlerMapping๊ณผ HandlerAdapter (0) | 2021.12.29 |
[MVC] ์คํ๋ง ์น MVC ๊ตฌ์กฐ (0) | 2021.12.26 |
[MVC] ํ๋ก ํธ ์ปจํธ๋กค๋ฌ ํจํด (0) | 2021.12.26 |
[MVC] ์๋ธ๋ฆฟ - HttpServletResponse (0) | 2021.12.25 |