Spring MVC / HttpServletResponse
by rlaehddnd0422HttpServletResponse ๊ธฐ๋ณธ ์ฌ์ฉ๋ฒ
์ญํ
๊ธฐ๋ณธ์ ์ผ๋ก HTTP ์๋ต ๋ฉ์์ง ์์ฑ.
ํธ์ ๊ธฐ๋ฅ์ผ๋ก๋ Content-type ์ค์ . ์ฟ ํค ์ค์ . ๋ฆฌ๋ค์ด๋ ํธ๊ฐ ์์ต๋๋ค.
๋ฆฌ๋ค์ด๋ ํธ ์ฐธ๊ณ .
package hello.servlet.basic.response;
import javax.servlet.ServletException;
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;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "responseHeaderServlet",urlPatterns = "/response-header")
public class ResponseHeaderServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setStatus(HttpServletResponse.SC_OK); // 200
response.setHeader("Content-type","text/plain");
response.setHeader("Cache-Control","no-cache,no-store,must-revalidate");
response.setHeader("Pragma","no-cache");
response.setHeader("my-header","hello");
content(response);
cookie(response);
redirect(response);
// message body
PrintWriter writer = response.getWriter();
writer.println("OK");
}
private void content(HttpServletResponse response) {
//Content-Type: text/plain;charset=utf-8
//Content-Length: 2
response.setContentType("text/plain");
response.setCharacterEncoding("utf-8");
//response.setContentLength(2); //(์๋ต์ ์๋ ์์ฑ)
}
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("myCookie", "good");
cookie.setMaxAge(600); //600์ด
response.addCookie(cookie);
}
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");
}
}
- setXXX์ ํตํด ํค๋์ ๋ณด๋ฅผ ์ค์ ํ ์ ์์ต๋๋ค.
- ex) ContentType์ด๋ Encoding ๋ฐฉ์ ๋ฑ
- ex) setStatus ๋ก ์ํ์ฝ๋ ์ค์ ๊ฐ๋ฅ
- setHeader("ํค๋์ด๋ฆ", ๊ฐ)์ผ๋ก ํค๋ ์ ๋ณด๋ฅผ ์ง์ ์ค์ ํ ์ ์์ต๋๋ค.
- ์ฟ ํค๋ฅผ ์ง์ ๋ง๋ค์ด addCookie๋ฅผ ํตํด ์ฟ ํค๋ฅผ ์ง์ ํ ์ ์์ต๋๋ค.
- sendRedirect๋ฅผ ํตํด ๋ฆฌ๋ค์ด๋ ์ ํ URI๋ฅผ ์ง์ ํ ์ ์์ต๋๋ค.
HTTP Response ๋ฐ์ดํฐ - ๋จ์ ํ ์คํธ๋ HTML
๋จ์ ํ ์คํธ ์๋ต
PrintWriter writer = response.getWriter();
writer.println("OK");
HTML ์๋ต
package hello.servlet.basic.response;
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.io.PrintWriter;
@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.println("<html>");
writer.println("<body>");
writer.println("<div> ์๋
? </div>");
writer.println("</body>");
writer.println("</html>");
}
}
Content-Type : text/html๋ก ์ง์ ํด ์ฃผ์ด์ผ ํฉ๋๋ค.
HTTP Response - JSON
package hello.servlet.basic.response;
import com.fasterxml.jackson.databind.ObjectMapper;
import hello.servlet.basic.HelloData;
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;
@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 {
//Content-type : application/json
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
HelloData hellodata = new HelloData();
hellodata.setName("Kim");
hellodata.setAge(20);
String s = objectMapper.writeValueAsString(hellodata);
response.getWriter().write(s);
}
}
Content-Type : application/json์ผ๋ก ์ง์ ํด์ฃผ์ด์ผ ํฉ๋๋ค.
+ Jackson ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์ ๊ณตํ๋ objectMapper.writeValueAsString() ๋ฅผ ์ฌ์ฉํ๋ฉด ๊ฐ์ฒด๋ฅผ JSON ๋ฌธ์๋ก ๋ณ๊ฒฝํ ์ ์์ต๋๋ค.
์ฐธ๊ณ : application/json์ ์คํ ์ utf-8ํ์์ ์ฌ์ฉํ๋๋ก ์ ์๋์ด ์๊ธฐ ๋๋ฌธ์
Content-type : application/json;charset=utf-8 ์ด๋ผ๊ณ ์ ๋ฌํ๋ ๊ฒ์ ์๋ฏธ์๋ ํ๋ผ๋ฏธํฐ๋ฅผ ์ถ๊ฐํ ๊ฒ์ด ๋ฉ๋๋ค.
'๐ Backend > MVC Pattern' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Front-Controller ํจํด (0) | 2023.02.27 |
---|---|
Spring / MVC ํจํด (0) | 2023.02.24 |
Spring / ์์ Servlet ํจํด, JSP ํจํด (0) | 2023.02.23 |
Spring MVC / HttpServletRequest (0) | 2023.02.22 |
Spring MVC / ์น ์ ํ๋ฆฌ์ผ์ด์ (0) | 2023.02.21 |
๋ธ๋ก๊ทธ์ ์ ๋ณด
Study Repository
rlaehddnd0422