장성호's
장성호's blog
장성호's
  • 분류 전체보기
    • 알고리즘
      • 백준
      • 이론
    • WEB
      • Spring 인강
      • 네트워크
    • 개인 프로젝트
      • 쇼핑몰 만들기

블로그 메뉴

  • 홈
  • 깃허브
전체 방문자
오늘
어제
반응형
hELLO · Designed By 정상우.
장성호's

장성호's blog

[Spring] 스프링 공부 #19
WEB/Spring 인강

[Spring] 스프링 공부 #19

2022. 3. 31. 19:40
반응형

 

 

HttpServletResponse

역할

  • HTTP 응답코드 지정
  • 헤더 생성
  • 바디 생성

Status-line 및 response-header 설정 관련 메서드

@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    //[status-line]
    response.setStatus(HttpServletResponse.SC_OK);


    //[response-headers]
    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 편의 메서드

private void content(HttpServletResponse response) {

    //Content-Type: text/plain;charset=utf-8
    //Content-Length: 2
    //response.setHeader("Content-Type", "text/plain;charset=utf-8");
    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);
    
}

Redirect 편의 메서드

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");
    
}

결과

 

HttpServletResponse - HTML 응답

@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>");
}

 

HttpServletResponse - API JSON 응답

@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //Content-Type: application/json
    response.setHeader("content-type", "application/json");
    response.setCharacterEncoding("utf-8");
    HelloData data = new HelloData();
    data.setUsername("kim");
    data.setAge(20);


    String result = objectMapper.writeValueAsString(data);
    response.getWriter().write(result);
}

 

출처

[인프런] 스프링 MVC 1편 - 백엔드 웹 개발 핵심 기술

https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-mvc-1/dashboard

 

스프링 MVC 1편 - 백엔드 웹 개발 핵심 기술 - 인프런 | 강의

웹 애플리케이션을 개발할 때 필요한 모든 웹 기술을 기초부터 이해하고, 완성할 수 있습니다. 스프링 MVC의 핵심 원리와 구조를 이해하고, 더 깊이있는 백엔드 개발자로 성장할 수 있습니다., -

www.inflearn.com

 

반응형
저작자표시 비영리 변경금지 (새창열림)

'WEB > Spring 인강' 카테고리의 다른 글

[Spring] 스프링 공부 #21  (0) 2022.04.05
[Spring] 스프링 공부 #20  (0) 2022.04.04
[Spring] 스프링 공부 #18  (0) 2022.03.30
[Spring] 스프링 공부 #17  (0) 2022.03.29
[Spring] 스프링 공부 #16  (0) 2022.03.29
    'WEB/Spring 인강' 카테고리의 다른 글
    • [Spring] 스프링 공부 #21
    • [Spring] 스프링 공부 #20
    • [Spring] 스프링 공부 #18
    • [Spring] 스프링 공부 #17
    장성호's
    장성호's
    장성호's 개발 공부 블로그

    티스토리툴바