Spring ์๋ธ๋ฆฟ [HttpServletRequest/HttpServletResponse]
spring mvc๊ฐ์๋ฅผ ๋ฃ๊ธฐ ์์ํ๋ค!
mvc๋ฅผ ๋ฐฐ์ฐ๊ธฐ ์ ์ ์๋ธ๋ฆฟ๋ถํฐ ๊ฐ๋จํ ์ดํด๋ณด๋๋ฐ ์ ์ดํดํ ๊ฑด์ง๋ ๋ชจ๋ฅด๊ฒ ๋ค...!
์ด๋ฒ์๋ ์๋ธ๋ฆฟ์ ์ด์ฉํด์ ์ดํด๋ณธ ์์ฒญ๊ณผ ์๋ต ๋ฉ์์ง๋ฅผ ํ์ธํด๋ณด๊ณ ์๋ ์๋ฆฌ๋ฅผ ์ ๋ฆฌํด๋ณด๋ ค๊ณ ํ๋ค.
๐ ์คํ๋ง ๋ถํธ์์ ์๋ธ๋ฆฟ์ ์ง์ ๋ฑ๋กํด์ ์ฐ๋ ค๋ฉด ์ด๋ ธํ ์ด์ ์ ํ๋ ์ถ๊ฐํด์ผ ํ๋ค.
@ServletComponentScan //์๋ธ๋ฆฟ ์๋ ๋ฑ๋ก
[ ์๋ธ๋ฆฟ ๋ฑ๋กํ๊ธฐ ]
@WebServlet(name="", urlPatterns = "/")
- name : ์๋ธ๋ฆฟ ์ด๋ฆ
- urlPatterns : URL ๋งคํ
[ HttpServletRequest ]
๋จผ์ http ์์ฒญ ๋ฉ์์ง๋ฅผ ์ดํด๋ณด์๋ฉด.
POST /save HTTP/1.1
Host: localhost:8080
Content-Type: application/x-www-form-urlencoded
username=kin&age=20
๐ HttpServletRequest๋ฅผ ์ฌ์ฉํ๋ฉด ์์ ์๋ HTTP ์์ฒญ ๋ฉ์์ง๋ฅผ ํธํ๊ฒ ์กฐํํ ์ ์๋ค.
โพ START LINE
- HTTP ๋ฉ์๋
- URL
- ์ฟผ๋ฆฌ ์คํธ๋ง
- ์คํค๋ง, ํ๋กํ ์ฝ
โพ ํค๋
โพ ๋ฐ๋
- form ํ๋ผ๋ฏธํฐ ํ์ ์กฐํ
- message body ๋ฐ์ดํฐ ์ง์ ์กฐํ
โ ๋ถ๊ฐ ๊ธฐ๋ฅ
โพ ์์ ์ ์ฅ์ ๊ธฐ๋ฅ
HTTP ์์ฒญ์ด ์์๋ถํฐ ๋๋ ๋๊น์ง ์ ์ง๋๋ ์์ ์ ์ฅ์ ๊ธฐ๋ฅ
- ์ ์ฅ : request.setAttribute(name, value)
- ์กฐํ : request.getAttribute(name)
โพ ์ธ์ ๊ด๋ฆฌ ๊ธฐ๋ฅ
- request.getSession(create: true)
โ HttpServletRequest ๊ธฐ๋ณธ ์ฌ์ฉ๋ฒ
@WebServlet(name = "requestHeaderServlet", urlPatterns = "/request-header")
public class RequestHeaderServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
printStartLine(request); // START LINE ์ถ๋ ฅ
printHeader(request); // ํค๋ ์ ๋ณด ์ถ๋ ฅ
printHeaderUtils(request); // ํธ๋ฆฌํ ์กฐํ
printEtc(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.getProtocal() = " + request.getProtocol()); //HTTP/1.1
System.out.println("request.getScheme() = " + request.getScheme()); //http
System.out.println("request.getRequestURL() = " + request.getRequestURL());
// http://localhost:8080/request-header
System.out.println("request.getRequestURI() = " + request.getRequestURI());
// /request-header
System.out.println("request.getQueryString() = " + request.getQueryString()); // null
System.out.println("request.isSecure() = " + request.isSecure()); //https ์ฌ์ฉ ์ ๋ฌด
System.out.println("--- REQUEST-LINE - end ---");
}
- request.getMethod() : http ๋ฉ์๋ ์กฐํ ( GET, POST, PATCH, PUT, DELETE...)
- request.getProtocol() : httpํ๋กํ ์ฝ ์กฐํ
- request.getScheme() : http ์คํค๋ง ์กฐํ (http, https)
- request.getRequestURL() : http ์ฃผ์ ์กฐํ
- request.getRequestURI() : http uri ์กฐํ
- request.getQueryString() : http ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ ์กฐํ ( ex)? username=hello ์ฌ๊ธฐ์ ์๋ ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ)
- request.isSecure() : https์ธ์ง ์๋์ง
์คํํ๊ณ localhost:8080/request-header๋ก ์ด๋ํ๋ฉด START LINE ์ ๋ณด๊ฐ ์ถ๋ ฅ๋๋ค.
๐ ํค๋ ์ ๋ณด
private void printHeader(HttpServletRequest request) {
System.out.println("--- Header - start ---");
request.getHeaderNames().asIterator()
.forEachRemaining(headerName -> System.out.println(headerName + ": " + request.getHeader(headerName)));
System.out.println("--- Header - end ---");
}
๋ชจ๋ ํค๋ ์ ๋ณด๋ฅผ ์ถ๋ ฅํด์ค๋ค.
http://localhost:8080/request-header? username=hello
์์ ๊ฐ์ ์ฃผ์๋ก ์กฐํํ์ ๊ฒฝ์ฐ์ ์๋์ ๊ฐ์ ์ ๋ณด๊ฐ ์ถ๋ ฅ๋๋ค.
๐ Header ํธ๋ฆฌํ ์กฐํ
๐ header (host, port) , Accept-Language, cookie, Content ์กฐํํ๊ธฐ
โพ header (host, port) ์กฐํ
System.out.println("[Host ํธ์ ์กฐํ]");
System.out.println("request.getServerName() = " +
request.getServerName()); //Host ํค๋
System.out.println("request.getServerPort() = " +
request.getServerPort()); //Host ํค๋
โพ Accept-Language ์กฐํ
request.getLocales().asIterator()
.forEachRemaining(locale -> System.out.println("locale = " + locale));
System.out.println("request.getLocale() = " + request.getLocale());
โพ Cookie ์กฐํ
if (request.getCookies() != null) {
for (Cookie cookie : request.getCookies()) {
System.out.println(cookie.getName() + ": " + cookie.getValue());
}
}
โพ Content ์กฐํ
System.out.println("request.getContentType() = " + request.getContentType());
System.out.println("request.getContentLength() = " + request.getContentLength());
System.out.println("request.getCharacterEncoding() = " + request.getCharacterEncoding());
๐ ๊ธฐํ ์ ๋ณด
๐ Remote ์ ๋ณด, Local ์ ๋ณด
โพ Remote ์ ๋ณด
System.out.println("request.getRemoteHost() = " + request.getRemoteHost());
System.out.println("request.getRemoteAddr() = " + request.getRemoteAddr());
System.out.println("request.getRemotePort() = " + request.getRemotePort());
โพ Local ์ ๋ณด
System.out.println("request.getLocalName() = " + request.getLocalName());
System.out.println("request.getLocalAddr() = " + request.getLocalAddr());
System.out.println("request.getLocalPort() = " + request.getLocalPort());
[ HTTP ์์ฒญ ๋ฐ์ดํฐ ]
http ์์ฒญ ๋ฉ์์ง๋ฅผ ํตํด ํด๋ผ์ด์ธํธ์์ ์๋ฒ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ๋ ๋ฐฉ๋ฒ์๋ 3๊ฐ์ง๊ฐ ์๋ค.
โพ GET - ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ
- /url? username=hello&age=20
- ๋ฉ์์ง ๋ฐ๋ ์์ด, URL์ ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ์ ๋ฐ์ดํฐ๋ฅผ ํฌํจํด์ ์ ๋ฌ
- Ex) ๊ฒ์, ํํฐ, ํ์ด์ง์์ ๋ง์ด ์ฌ์ฉ
โพ POST - HTML Form
- content-type: application/x-www-form-unlencoded
- ๋ฉ์์ง ๋ฐ๋์ ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ ํ์์ผ๋ก ์ ๋ฌ username=hello&age=20
- ํ์๊ฐ์ , ์ํ ์ฃผ๋ฌธ, HTML Form ์ฌ์ฉ
โพ HTTP message body์ ๋ฐ์ดํฐ๋ฅผ ์ง์ ๋ด์์ ์์ฒญ
- HTTP API์์ ์ฃผ๋ก ์ฌ์ฉ, JSON, XML, TEXT
- ๋ฐ์ดํฐ ํ์์ ์ฃผ๋ก JSON ์ฌ์ฉ (POST, PUT, PATCH)
โ GET๊ณผ POST๋ ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ ํ์์ผ๋ก ์ ๋ฌํ๊ธฐ ๋๋ฌธ์ request.getParameter()๋ก ์กฐํ ๊ฐ๋ฅ
๐ Get
http://localhost:8080/request-param?username=hello&username=kim&age=20
์์ ๊ฐ์ด username์ 2๊ฐ ๋ฃ๊ณ , age๋ฅผ ๋ฃ๋ ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ ์ฃผ์์ ๋ฃ์๋ค. get๋ฐฉ์์ ์ฃผ์์ ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ ํ์์ผ๋ก ์กฐํํ๊ธฐ ๋๋ฌธ์ url์ ๋ฃ์ด์ ์กฐํํด์ผ ํ๋ค.
@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("[๋จ์ผ ํ๋ผ๋ฏธํฐ ์กฐํ]");
String username = request.getParameter("username");
System.out.println("username = " + username);
String age = request.getParameter("age");
System.out.println("age = " + age);
System.out.println();
System.out.println("[์ด๋ฆ์ด ๊ฐ์ ๋ณต์ ํ๋ผ๋ฏธํฐ ์กฐํ]");
String[] usernames = request.getParameterValues("username");
for (String name : usernames) {
System.out.println("username = " + name);
}
}
}
>> ์คํํ๋ฉด
๐ POST
HTML Form์ ์ฌ์ฉํด์ ํด๋ผ์ด์ธํธ์์ ์๋ฒ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์ก
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/request-param" method="post">
username: <input type="text" name="username" />
age: <input type="text" name="age" />
<button type="submit">์ ์ก</button>
</form>
</body>
</html>
์ด๋ ๊ฒ ๋ฉ์์ง ๋ฐ๋์ ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ ํ์์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ๋ค.
/request-param์ผ๋ก ์๋ํ๋๋ก ํ๊ธฐ ๋๋ฌธ์ ์์ GET์ผ๋ก ์กฐํํ๋ ๊ฒ๊ณผ ๊ฒฐ๊ณผ๊ฐ ๋๊ฐ์ด ๋์จ๋ค.
์์์ ์ค๋ช ํ๋ฏ์ด GET๊ณผ POST๋ ๋ชจ๋ ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์กํ๊ธฐ ๋๋ฌธ์ ๋๊ฐ์ด ์กฐํํ ์ ์๋ค.
๐ HTTP ์์ฒญ ๋ฐ์ดํฐ - JSON
@Getter @Setter //getter setter๋ฅผ lombok์ผ๋ก ์๋ ์ธํ
public class HelloData {
private String username;
private int age;
}
@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.username = " + helloData.getUsername());
System.out.println("helloData.age = " + helloData.getAge());
}
}
>> ์กฐํ ํ๋ฉด
POST MAN์ ์ด์ฉํด์ ์กฐํํด๋ณด์๋ค. ๊ทธ๋ผ ์ด๋ ๊ฒ JSON ํ์์ผ๋ก ์กฐํ๊ฐ ๊ฐ๋ฅํ๋ค.
JSON ๋ฐฉ๋ฒ์ด ์ฝ๊ธฐ ํธํด์ ๊ฐ์ฅ ๋ง์ด ์ด๋ค๊ณ ํ๋ค!
[ HttpServletResponse ]
๐ HttpServletResponse ์ญํ
- HTTP ์๋ต ์ฝ๋ ์ง์
- ํค๋ ์์ฑ
- ๋ฐ๋ ์์ฑ
- ํธ์ ๊ธฐ๋ฅ ์ ๊ณต : Content-Type, ์ฟ ํค, Redirect
@WebServlet(name="responseHeaderServlet", urlPatterns = "/response-header")
public class ResponseHeaderServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//[status-line]
response.setStatus(HttpServletResponse.SC_OK); // 200
//[response-headers]
response.setHeader("Content-Type", "text/plain;charset=utf-8");
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
response.setHeader("Pragma", "no-cache");
response.setHeader("my-header","hello");
//[Header ํธ์ ๋ฉ์๋]
content(response);
cookie(response);
redirect(response);
}
//content ํธ์ ๋ฉ์๋
private void content(HttpServletResponse response) {
response.setContentType("text/plain");
response.setCharacterEncoding("utf-8");
}
//cookie ํธ์ ๋ฉ์๋
private void cookie(HttpServletResponse response) {
Cookie cookie = new Cookie("myCookie", "good");
cookie.setMaxAge(600); //600์ด
response.addCookie(cookie);
}
//redirect ํธ์ ๋ฉ์๋
private void redirect(HttpServletResponse response) throws IOException {
response.sendRedirect("/basic/hello-form.html");
}
}
โ response.setStatus(HttpServletResponse.SC_OK); ์ด๊ฑธ ์ฐ๋ ์ด์ ๋ 200 ok๋ฅผ ์ง์ ํด์ฃผ๋ ๊ฒ์ด๋ค.
response.setStatus(200); ์ด๋ ๊ฒ ์จ๋ ๋๋ค!
๊ฐ๋ฐ์ ๋๊ตฌ์์ ํ์ธํด๋ณผ ์ ์๋ค. ๋ค๋ฅธ ์ซ์๋ก ๋ฐ๊พธ๋ฉด ๋ค๋ฅธ ์ซ์๋ก ๋์จ๋ค!
redirect์ ๊ดํ ๋ด์ฉ์ ์ด์ ํฌ์คํ ์์ ํ์ธํ ์ ์๋ค! ๐
๐ HTTP ์๋ต ๋ฐ์ดํฐ - ๋จ์ ํ ์คํธ, HTML
HTTP ์๋ต ๋ฉ์์ง๋ HTML๋ก ๋ด์์ ์ ๋ฌํ๋ ๊ฒฝ์ฐ์ JSON์ผ๋ก ๋๋ ์ ์ ๋ฌํ๋ ๊ฒฝ์ฐ๊ฐ ์๋ค.
๋จผ์ HTML๋ก ์ ๋ฌํ๋ ๋ฐฉ๋ฒ์ ์ดํด๋ณด์.
@WebServlet(name = "responseHtmlServlet", urlPatterns = "/response-html")
public class ResponseHtmlServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html"); //HTML๋ก ๋ฐํํ ๋๋ ๊ผญ text/html๋ก ํ๊ธฐ
response.setCharacterEncoding("utf-8"); //์ด๊ฑฐ ์ํ๋ฉด ํ๊ธ ๋ค ๊นจ์ง
PrintWriter writer = response.getWriter();
writer.println("<html>");
writer.println("<body>");
writer.println(" <div>์๋
?</div>");
writer.println("</body>");
writer.println("</html>");
// servlet์ผ๋ก html๋ ๋๋งํ ๋๋ ์ด๋ ๊ฒ ์ ์ด์ผ๋จ
}
}
>> ์คํ ํ๋ฉด
์ด๋ ๊ฒ ์๋ ?์ด๋ผ๊ณ ๋์ค์ง๋ง ๊ฐ๋ฐ์ ๋๊ตฌ์์ ์์คํ์ผ์ ์ดํด๋ณด๋ฉด ์๋ต์ผ๋ก ๋ณด๋ธ html๊ฐ ๋ค์ด์๋ ๊ฑธ ํ์ธํ ์ ์๋ค!!
๐ HTTP ์๋ต ๋ฐ์ดํฐ - JSON
@WebServlet(name = "responseJsonServlet", urlPatterns = "/response-json")
public class ResponseJsonServlet extends HttpServlet {
private ObjectMapper objectMapper = new ObjectMapper();
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json"); //Content-Type: application/json ๋ก ์ง์ ํด์ค์ผ๋จ
response.setCharacterEncoding("utf-8");
HelloData helloData = new HelloData();
helloData.setUsername("kim");
helloData.setAge(20);
//{"username":"kim", "age":20}
String result = objectMapper.writeValueAsString(helloData); //๊ฐ์ฒด๋ฅผ JSON๋ฌธ์๋ก ๋ณ๊ฒฝ
response.getWriter().write(result);
}
}
>> ์คํํ๋ฉด
์์ค์ฝ๋๋ฅผ ๋ด๋ json ํ์์ผ๋ก ์๋ต๋์๋ค!!
โ json์ ์ด๋ฏธ ์ด๊ธฐ ์ค์ ์ด utf-8์ด๊ธฐ ๋๋ฌธ์ ๊ตณ์ด ์ถ๊ฐํด์ค ํ์๊ฐ ์๋ค.
์๋ธ๋ฆฟ์ ์ด๋ฆ๋ง ๋ค์์ ๋ ๋๊ฒ ์ด๋ ค์ธ ๊ฒ์ด๋ผ๊ณ ์๊ฐํ๋๋ฐ ์ด๋ ๊ฒ ์ค์ต์ผ๋ก ํด๋ณด๋๊น ์ด๋ ์ ๋ ์ดํด๊ฐ ๊ฐ ๊ฒ ๊ฐ๋ค.
๋ธ๋ก๊ทธ๋ก ์ ๋ฆฌํ๋๊น ํ๋ฒ ๋ ์ ๋ฆฌํ๋ ๋๋์ด์ด์ ์ข์ ๊ฒ ๊ฐ๋ค!! ๐
mvc๋ฅผ ์ฌ์ฉํ๋ฉด ์ด๊ฒ๋ณด๋ค ํจ์ฌ ์ฝ๋๊ฐ ๋ง์ด ์ค์ด๋ ๋ค๋๋ฐ ๋ฒ์จ ์ ๊ธฐํ๋ค,,
์นํ์ด์ง๊ฐ ๋ค์์ ๋๊ฒ ๋ณต์กํ๊ฒ ๋์ํ๊ณ ์๋ค๋ ๊ฒ์ ๋ค์ ํ๋ฒ ๋๋ผ๊ฒ ๋์๋ค( ษตฬฅฬฅ หฬซ ษตฬฅฬฅ) ์์ผ๋ก ๋ ํ์ดํ !
์ด ํฌ์คํ ์ ์ธํ๋ฐ ๊น์ํ ๋์ ์คํ๋ง MVC1ํธ์ ๋ฃ๊ณ ์์ฑ๋ ๋ณต์ต ์ ๋ฆฌ ํฌ์คํ ์ ๋๋ค.