์์
์คํ๋ง์ ์ฌ์ฉํ์ง๋ ์์ง๋ง ์คํ๋ง ๋ถํธ ํ๋ก์ ํธ๋ฅผ ๋ง๋ ๋ค.
์๋ธ๋ฆฟ์ ํฐ์บฃ ๊ฐ์ WAS๋ฅผ ์ง์ ์ค์นํ๊ณ , ๊ทธ ์์ ์๋ธ๋ฆฟ ์ฝ๋๋ฅผ ํด๋์ค ํ์ผ๋ก ๋น๋ํด์ ์ฌ๋ฆฐ ๋ค์ ํฐ์บฃ ์๋ฒ๋ฅผ ์คํํด์ผ ํ๋๋ฐ,
์คํ๋ง ๋ถํธ๋ ํฐ์บฃ ์๋ฒ๋ฅผ ๋ด์ฅํ๊ณ ์์ผ๋, ์ด๋ก ์งํํ๋ค.
๋ณดํต์ Jar๋ฅผ ์ ํํ์ง๋ง, JSP๋ฅผ ๋๋ฆฌ๊ธฐ ์ํด War๋ฅผ ์ ํํ๋ค.
HelloServlet
// ์๋ธ๋ฆฟ์ด ํธ์ถ๋๋ฉด ์ด service๊ฐ ํธ์ถ๋๋ค.
@WebServlet(name = "helloServlet", urlPatterns = "/hello")
public class HelloServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.service(req, resp);
System.out.println("HelloServlet");
System.out.println("Request : " + req);
System.out.println("Response : " + resp);
String name = req.getParameter("name");
System.out.println(name);
resp.setCharacterEncoding("utf-8");
resp.getWriter().write("hello" + name);
}
}
HelloServlet
Request : org.apache.catalina.connector.RequestFacade@de1a8e6
Response : org.apache.catalina.connector.ResponseFacade@2b3735f6
amazzi
@WebServlet
: ์๋ธ๋ฆฟ ์ด๋ ธํ ์ด์ @ServletComponentScan
: ์คํ๋ง์ด ์๋์ผ๋ก ์๋ธ๋ฆฟ์ ์ฐพ์ ๋ฑ๋กํด์ค๋ค.- HTTP ์์ฒญ์ด ์ค๋ฉด ์๋ธ๋ฆฟ ์ปจํ ์ด๋์ธ WAS๊ฐ HTTP ์์ฒญ, ์๋ต ๊ฐ์ฒด๋ฅผ ์๋ธ๋ฆฟ์ ๋์ ธ์ค๋ค.
req.getParameter()
: ์์ฒญ์์ ํด๋น Parameter๋ฅผ ๊ฐ์ ธ์จ๋ค.resp.setCharacterEncoding("utf-8");
: ํค๋์ CharacterEncoding์ ์ง์ ํ๋ค.resp.getWriter().write();
:write()
์ ๋ฐ๋์ ๋ด์ ๋ฐ์ดํฐ๋ฅผ ๋ฃ์ด์ค๋ค.
๐ค RequestFacade?
HttpServletRequest๋ ์ธํฐํ์ด์ค์ด๋ค.
์ฌ๋ฌ๊ฐ์ง WAS ์๋ฒ๋ค์ด ์ด ์ธํฐํ์ด์ค์ ๊ตฌํ์ฒด๋ฅผ ๊ตฌํํ๊ณ ์์ด ๋ค์ํ WAS๋ฅผ ์ฌ์ฉํ ์ ์๋ ๊ฒ์ด๋ค.
HTTP ์์ฒญ ๋ฉ์์ง ๋ก๊ทธ๋ก ํ์ธํ๊ธฐ
application-properties์ ๋ค์์ ์ถ๊ฐํ๋ค.
logging.level.org.apache.coyote.http11=debug
์น ์ ํ๋ฆฌ์ผ์ด์ ์๋ฒ์ ์์ฒญ ์๋ต ๊ตฌ์กฐ
HttpServletRequest
์๋ธ๋ฆฟ์ ๊ฐ๋ฐ์๊ฐ HTTP ์์ฒญ ๋ฉ์์ง๋ฅผ ํธ๋ฆฌํ๊ฒ ์ฌ์ฉํ ์ ์๋๋ก ๊ฐ๋ฐ์ ๋์ ์ HTTP ์์ฒญ ๋ฉ์์ง๋ฅผ ํ์ฑํ๋ค.
๊ทธ๋ฆฌ๊ณ ๊ฒฐ๊ณผ๋ฅผ HttpServletRequest ๊ฐ์ฒด์ ๋ด์์ ์ ๊ณตํ๋ค.
- START LINE
- HTTP ๋ฉ์๋
- URL
- ์ฟผ๋ฆฌ ์คํธ๋ง
- ์คํค๋ง, ํ๋กํ ์ฝ
- HEADER
- ํค๋ ์กฐํ
- BODY
- form ํ๋ผ๋ฏธํฐ์ ํ์ ์กฐํ
- message body ๋ฐ์ดํฐ ์ง์ ์กฐํ
- ๋ถ๊ฐ ๊ธฐ๋ฅ
- ์์ ์ ์ฅ์ ๊ธฐ๋ฅ
- ํด๋น HTTP ์์ฒญ์ด ์์๋ถํฐ ๋๋ ๋ ๊น์ง ์ ์ง๋๋ ์์ ์ ์ฅ์ ๊ธฐ๋ฅ
- ์ ์ฅ: request.setAttribute(name, value)
- ์กฐํ: request.getAttribute(name)
- ์ธ์
๊ด๋ฆฌ ๊ธฐ๋ฅ
- request.getSession(create: true)
- ์์ ์ ์ฅ์ ๊ธฐ๋ฅ
๊ธฐ๋ณธ ์ฌ์ฉ
Start Line
request.getMethod() = GET
request.getProtocal() = HTTP/1.1
request.getScheme() = http
request.getRequestURL() = http://localhost:8080/request-header
request.getRequestURI() = /request-header
request.getQueryString() = username=hello
request.isSecure() = false
ํค๋ ์ ๋ณด
request.getHeaderNames().asIterator()
.forEachRemaining(headerName -> System.out.println(headerName + ": " +
request.getHeader(headerName)));
System.out.println("--- Headers - end ---");
System.out.println();
์ด ์ธ์๋ Header ์ ๋ณด๋ฅผ ์ถ์ถํ๋ ๋ฉ์๋๋ค์ด ์๋ค.
- request.getServerName()
- request.getServerPort()
- request.getLocale()
- request.getCookies()
- request.getContentType()
- request.getCharacterEncoding()
HTTP ์์ฒญ ๋ฐ์ดํฐ - GET ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ
๋ฉ์์ง ๋ฐ๋ ์์ด, URL์ ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ๋ฅผ ์ฌ์ฉํด์ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ๋ ๋ฐฉ๋ฒ.
์) ๊ฒ์, ํํฐ, ํ์ด์ง๋ฑ์์ ๋ง์ด ์ฌ์ฉ
http://localhost:8080/request-param?username=hello&age=20
์ฟผ๋ฆฌํ๋ผ๋ฏธํฐ๋URL์๋ค์๊ณผ๊ฐ์ด ?๋ฅผ์์์ผ๋ก๋ณด๋ผ์์๋ค.์ถ๊ฐํ๋ผ๋ฏธํฐ๋ &๋ก๊ตฌ๋ถํ๋ฉด๋๋ค.
RequestParamServlet
/**
* ํ๋ผ๋ฏธํฐ ์ ์ก ๊ธฐ๋ฅ
* 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 req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("1. ์ ์ฒด ํ๋ผ๋ฏธํฐ ์กฐํ");
req.getParameterNames()
.asIterator()
.forEachRemaining(paramName ->
System.out.println(paramName + "=" + req.getParameter(paramName)));
System.out.println("2. ๋จ์ผ ํ๋ผ๋ฏธํฐ ์กฐํ (๋ ๋ง์ด ์ฐ๋ ๋ฐฉ์)");
String username = req.getParameter("username");
System.out.println(username);
System.out.println("3. ํ๋ผ๋ฏธํฐ ์ด๋ฆ์ด ๊ฐ์ ์ฌ๋ฌ๊ฐ ๊ฐ์ด ์์ ๊ฒฝ์ฐ");
String[] usernames = req.getParameterValues("username");
}
}
HTTP ์์ฒญ ๋ฐ์ดํฐ - POST HTML Form
๋ฉ์์ง ๋ฐ๋์ ๋ฐ์ดํฐ๊ฐ ๋ค์ด๊ฐ๊ธฐ ๋๋ฌธ์ content-type์ด ์๋ค.
content-type : application/x-www-form-urlencoded
๋ฐ๋์ ์ฟผ๋ฆฌ ํ๋ฆฌ๋ฏธํฐ ํ์์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ๋ค. username=hello&age=20
hello-form.html ์ผ๋ถ
<form action="/request-param" method="post">
username: <input type="text" name="username"/> age: <input type="text" name="age"/>
<button type="submit">์ ์ก</button>
</form>
์ด์ ์ ๋ง๋ค์๋ requestParamServlet์ ์์ฒญ์ ๋ณด๋ด๋ฉด,
๋ค์๊ณผ ๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ์ป๋๋ค!
๊ทธ๋ฐ๋ฐ requestParamServlet๋ ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ๋ง ์กฐํํ๋๋ฐ ?
application/x-www-form-urlencoded ํ์์ ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ ํ์๊ณผ ๊ฐ๋ค.
๋ฐ๋ผ์ ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ ์กฐํ ๋ฉ์๋๋ฅผ ๊ทธ๋๋ก ์ฌ์ฉํ๋ฉด ๋๋ค.
์๋ฒ ์
์ฅ์์๋ ๋์ ํ์์ด ๋์ผํ๋ฏ๋ก, request.getParameter() ๋ก ํธ๋ฆฌํ๊ฒ ๊ตฌ๋ถ์์ด ์กฐํํ ์ ์๋ค.
request.getParameter() ๋ GET URL ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ ํ์๋ ์ง์ํ๊ณ , POST HTML Form ํ์๋ ๋ ๋ค ์ง์ํ๋ค.
๐ก POSTMAN์ผ๋ก ํ ์คํธํ ๊ฒฝ์ฐ
Form ๋ฐ์ดํฐ๋ฅผ ์
๋ ฅํ๊ธฐ ๊ท์ฐฎ์๋ฐ, ์ด๋ Content-type์ application/x-www-form-urlencoded
๋ก ์ค์ ํ๋ค.
HTTP ์์ฒญ ๋ฐ์ดํฐ - API ๋ฉ์์ง ๋ฐ๋
HTTP message body์ ๋ฐ์ดํฐ๋ฅผ ์ง์ ๋ด์์ ์์ฒญํ๋ค.
HTTP API์์ ์ฃผ๋ก JSON์ ์ฌ์ฉํ๋ค.
๋จ์ ํ ์คํธ
RequestBodyStringServlet - Rawํ String
@WebServlet(name = "requestBodyStringServlet", urlPatterns = "/request-body-string")
public class RequestBodyStringServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletInputStream inputStream = req.getInputStream(); // byte ์ฝ๋๋ฅผ ์ป์
String messageBody = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8); // ์คํ๋ง์ด ์ ๊ณตํ๋ ์ ํธ๋ฆฌํฐ
}
}
HTML form๋ ์ด๋ ๊ฒ ์กฐํํ ์ ์์ง๋ง ๋ฒ๊ฑฐ๋ก์ฐ๋ ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ ์กฐํ๋ฅผ ์ฌ์ฉํ์!
JSON
- content-type: application/json
HelloData
@Getter
@Setter
public class HelloData {
private String userName;
private int age;
}
RequestBodyJsonServlet
@WebServlet(name = "requestBodyJsonServlet", urlPatterns = "/request-body-json")
public class RequestBodyJsonServlet extends HttpServlet {
private ObjectMapper objectMapper = new ObjectMapper();
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletInputStream inputStream = req.getInputStream(); // byte ์ฝ๋๋ฅผ ์ป์
String messageBody = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8); // ์คํ๋ง์ด ์ ๊ณตํ๋ ์ ํธ๋ฆฌํฐ
HelloData helloData = objectMapper.readValue(messageBody, HelloData.class);
}
}
JSON ๊ฒฐ๊ณผ๋ฅผ ํ์ฑํด์ ๊ฐ์ฒด๋ก ๋ณํํ๋ ค๋ฉด Jackson, Gson ๊ฐ์ JSON ๋ณํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ถ๊ฐํด์ ์ฌ์ฉํด์ผ ํ๋ค.
์คํ๋ง ๋ถํธ๋ก Spring MVC๋ฅผ ์ ํํ๋ฉด ๊ธฐ๋ณธ์ผ๋ก Jackson ๋ผ์ด๋ธ๋ฌ๋ฆฌ( ObjectMapper)๋ฅผ ์ ๊ณตํ๋ค.
HTTPServletResponse - ๊ธฐ๋ณธ ์ฌ์ฉ๋ฒ
๊ฐ๋ฐ์๊ฐ ์ง์ ์๋ต ๊ฐ์ฒด๋ฅผ ๋ง๋ค๊ธฐ๋ ๋ฒ๊ฑฐ๋กญ์ง ์๊ฒ ์๋ธ๋ฆฟ์ด ์ ๊ณตํด์ค๋ค!
- HTTP ์๋ต์ฝ๋ ์ง์
- ํค๋ ์์ฑ
- ๋ฐ๋ ์์ฑ ๋ฑ๋ฑโฆ
ResponseHeaderServlet
@WebServlet(name = "responseHeaderServlet", urlPatterns = "/response-header")
public class ResponseHeaderServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//[status-line] ์ํ์ฝ๋ ์ง์
resp.setStatus(HttpServletResponse.SC_OK); //200
//[response-headers] ์๋ต ํค๋ ์ง์
resp.setHeader("Content-Type", "text/plain;charset=utf-8");
resp.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
resp.setHeader("Pragma", "no-cache");
resp.setHeader("my-header", "hello");
//[Header ํธ์ ๋ฉ์๋] content(response); cookie(response); redirect(response);
//[message body] ์๋ต ๋ฐ๋ ์ง์
PrintWriter writer = resp.getWriter();
writer.println("ok");
//Content-Type: text/plain;charset=utf-8
//Content-Length: 2
//response.setHeader("Content-Type", "text/plain;charset=utf-8");
resp.setContentType("text/plain");
resp.setCharacterEncoding("utf-8");
//response.setContentLength(2); //(์๋ต์ ์๋ ์์ฑ)
}
}
์ฟ ํค ์ค์ ํ๊ธฐ
//Set-Cookie: myCookie=good; Max-Age=600;
response.setHeader("Set-Cookie", "myCookie=good; Max-Age=600");
// ๋๋
Cookie cookie = new Cookie("myCookie", "good");
cookie.setMaxAge(600); //600์ด
response.addCookie(cookie);
Redirect ์ค์ ํ๊ธฐ
//Status Code 302
//Location: /basic/hello-form.html
response.setStatus(HttpServletResponse.SC_FOUND); //302
response.setHeader("Location", "/basic/hello-form.html");
// ๋๋
response.sendRedirect("/basic/hello-form.html");
HTTP ์์ฒญ ๋ฐ์ดํฐ
๋จ์ ํ ์คํธ, HTML
HttpServletResponse
@WebServlet(name = "responseHtmlServlet", urlPatterns = "/response-html")
public class HttpServletResponse extends HttpServlet {
@Override
protected void service(HttpServletRequest req, javax.servlet.http.HttpServletResponse resp) throws ServletException, IOException {
// Content-Type: text:html; charset=utf-8
resp.setContentType("text:html");
resp.setCharacterEncoding("utf-8");
PrintWriter writer = resp.getWriter();
writer.println("<html>");
writer.println("<body>");
writer.println(" <div>์ด๊ฑด ์๋์ง</div>");
writer.println("</body>");
writer.println("</html>");
}
}
๊ฐ๋จํ์ง๋ง, HTML์ ์ผ์ผํ ์์ฑํด์ฃผ์ด์ผ ํ๋ค?
API JSON
ResponseJsonServlet
@WebServlet(name = "responseJsonServlet", urlPatterns = "/response-json")
public class ResponseJsonServlet extends HttpServlet {
private final ObjectMapper objectMapper = new ObjectMapper();
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//Content-Type: application/json
resp.setHeader("content-type", "application/json");
resp.setCharacterEncoding("utf-8");
HelloData data = new HelloData();
data.setUserName("mazzi");
data.setAge(100);
// JSON์ผ๋ก ๋ณํ
String result = objectMapper.writeValueAsString(data);
resp.getWriter().write(result);
}
}
(์ฌ์ค ์คํ๋ง ์ฐ๋ฉด ์ด๋ ๊ฒ ๊ธธ์ด์ง์ง๋ ์๋๋ค.)
โ๏ธ ๊น์๋์ ์คํ๋ง MVC 1ํธ - ๋ฐฑ์๋ ์น ๊ฐ๋ฐ ํต์ฌ ๊ธฐ์ ๊ฐ์ ๋ ธํธ โ๏ธ