728x90
๐ง HttpServletResponse
์๋ธ๋ฆฟ์ ์๋ต์ ๋ํด HttpServletResponse๋ฅผ ํตํด ๋ค์๊ณผ ๊ฐ์ ๊ธฐ๋ฅ์ ์ ๊ณตํฉ๋๋ค.
- HTTP ์๋ต ๋ฉ์์ง ์์ฑ
- HTTP ์๋ต์ฝ๋ ์ง์
- ํค๋ ์์ฑ
- ๋ฐ๋ ์์ฑ
๋ํ Content-Type, ์ฟ ํค, Redirect๋ฅผ ํธํ๊ฒ ์ฌ์ฉํ ์ ์๊ฒ ํด์ฃผ๋ ๊ธฐ๋ฅ์ ์ ๊ณตํด์ค๋๋ค.
๐ง ํค๋ ์ค์
HttpServletResponse.setHeader()
@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 - HEADER ์ค์
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"); // ์์์ ํค๋ ์์ฑํ๊ธฐ
}
}
๐ ํธ์ ๋ฉ์๋ ์ฌ์ฉ
@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 - HEADER ์ค์
response.setContentType("test/plain");
response.setCharacterEncoding("utf-8");
}
}
๐ง ์ฟ ํค ์ค์
HttpServletResponse.addCookie(cookie)
private void cookie(HttpServletResponse response) {
// Set-Cookie: myCookie=good; Max-Age=600;
// response.setHeader("Set-Cookie", "myCookie=good; Max-Age=600");
Cookie cookie = new Cookie("", "good");
cookie.setMaxAge(600);
response.addCookie(cookie);
}
๐ง ๋ฆฌ๋ค์ด๋ ํธ(Redirect) ์ค์
HttpServletResponse.sendRedirect(url)
private void redirect(HttpServletResponse response) throws IOException {
// 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");
}
๐ง Content ์ค์ (Text & HTML๋ก ์๋ต ๋ณด๋ด๊ธฐ)
@WebServlet(name = "responseHtmlServlet", urlPatterns = "/response-html")
public class ResponseHtmlServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Content-Type: text/html;charset=utf-8
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
PrintWriter writer = response.getWriter();
writer.write("<html>");
writer.write("<body>");
writer.write("<div> ์๋
? </div>");
writer.write("</body>");
writer.write("</html>");
}
}
๐ง Content ์ค์ (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.setHeader("content-type", "application/json");
// response.setCharacterEncoding("utf-8"); json์ ๋ฌด์กฐ๊ฑด utf-8์ด๊ธฐ ๋๋ฌธ์ ์ถ๊ฐํ์ง ์์๋ ๋๋ค.
HelloData data = new HelloData();
data.setUsername("kim");
data.setAge(20); // {"username":"kim","age":20}
String result = objectMapper.writeValueAsString(data);
response.getOutputStream().print(result);
}
@Getter
@Setter
private static class HelloData {
private String username;
private int age;
}
}
๐ง 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] ์๋ธ๋ฆฟ - HttpServletRequest (0) | 2021.12.25 |