minOS

서블릿 - HttpServletResponse : 기본 사용법 본문

TIL/김영한의 스프링 MVC 1편 - 백엔드 웹 개발 핵심 기술

서블릿 - HttpServletResponse : 기본 사용법

minOE 2024. 6. 20. 19:42
728x90

HttpServletResponse 역할

HTTP 응답 메세지 생성
- HTTP 응답 코드 지정
- 헤더 생성
- 바디 생성

편의 메서드 존재
- Content-Type,쿠키,Redirect

 

 

편의 메서드 사용하지 않고 응답 메세지 생성

@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); // 상태코드: 커맨드 +B


        //[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");
        
         //[message body]
        PrintWriter writer = response.getWriter();
        writer.println("츄르");

 

 

 

 

Content-Type 편의 메서드 사용

@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); // 상태코드: 커맨드 +B


        //[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");
        //[Header 편의 메서드]
        content(response);

        //[message body]
        PrintWriter writer = response.getWriter();
        writer.println("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); //(생략시 자동 생성)
    }
 }

 

 

 

 

Cookie 편의 메서드 사용

@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); // 상태코드: 커맨드 +B


        //[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");
   
        //[Cookie 편의 메서드]
        cookie(response);
       


        //[message body]
        PrintWriter writer = response.getWriter();
        writer.println("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("myCookie", "good");
        cookie.setMaxAge(600); //600초
        response.addCookie(cookie);
    }
}



 

 

Redirect 편의 메서드 사용

@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); // 상태코드: 커맨드 +B


        //[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");
        //[redirect 편의 메소드]
        redirect(response);


        //[message body]
        PrintWriter writer = response.getWriter();
        writer.println("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");
	}
}

 

 

 

728x90