HTTP 응답 데이터 – 단순 텍스트, HTML

SHByun·2023년 1월 21일

강의 chap2-11


HTTP 응답 데이터 – 단순 텍스트, HTML

1. 응답 데이터 종류

  • 단순 텍스트 응답(writer.println("ok"); )
  • HTML 응답
  • HTTP API - MessageBody JSON 응답

2. 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.println("<html>");
        writer.println("<body>");
        writer.println("  <div>안녕</div>");
        writer.println("</body>");
        writer.println("</html>");
    }
}


  • Content-Type을 지정해야 한다.
  • 일일이 html을 써줘야 해서 불편하다.

출처

인프런 강의 - 김영한
스프링 MVC 1편 - 백엔드 웹 개발 핵심 기술
https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-mvc-1/dashboard

profile
안녕하세요

0개의 댓글