Spring

Spring ์„œ๋ธ”๋ฆฟ [HttpServletRequest/HttpServletResponse]

DAHLIA CHOI 2021. 8. 1. 16:39

spring mvc๊ฐ•์˜๋ฅผ ๋“ฃ๊ธฐ ์‹œ์ž‘ํ–ˆ๋‹ค!

mvc๋ฅผ ๋ฐฐ์šฐ๊ธฐ ์ „์— ์„œ๋ธ”๋ฆฟ๋ถ€ํ„ฐ ๊ฐ„๋‹จํžˆ ์‚ดํŽด๋ณด๋Š”๋ฐ ์ž˜ ์ดํ•ดํ•œ ๊ฑด์ง€๋Š” ๋ชจ๋ฅด๊ฒ ๋‹ค...!

์ด๋ฒˆ์—๋Š” ์„œ๋ธ”๋ฆฟ์„ ์ด์šฉํ•ด์„œ ์‚ดํŽด๋ณธ ์š”์ฒญ๊ณผ ์‘๋‹ต ๋ฉ”์‹œ์ง€๋ฅผ ํ™•์ธํ•ด๋ณด๊ณ  ์ž‘๋™ ์›๋ฆฌ๋ฅผ ์ •๋ฆฌํ•ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค.

 


๐ŸŒ  ์Šคํ”„๋ง ๋ถ€ํŠธ์—์„œ ์„œ๋ธ”๋ฆฟ์„ ์ง์ ‘ ๋“ฑ๋กํ•ด์„œ ์“ฐ๋ ค๋ฉด ์–ด๋…ธํ…Œ์ด์…˜์„ ํ•˜๋‚˜ ์ถ”๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค.

@ServletComponentScan //์„œ๋ธ”๋ฆฟ ์ž๋™ ๋“ฑ๋ก

 

 

 

[ ์„œ๋ธ”๋ฆฟ ๋“ฑ๋กํ•˜๊ธฐ ]

@WebServlet(name="", urlPatterns = "/")
  • name : ์„œ๋ธ”๋ฆฟ ์ด๋ฆ„
  • urlPatterns : URL ๋งคํ•‘

 

 

 

[ HttpServletRequest ]

 

๋จผ์ € http ์š”์ฒญ ๋ฉ”์‹œ์ง€๋ฅผ ์‚ดํŽด๋ณด์ž๋ฉด.

POST /save HTTP/1.1
Host: localhost:8080
Content-Type: application/x-www-form-urlencoded

username=kin&age=20

๐Ÿ‘‰ HttpServletRequest๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์œ„์— ์žˆ๋Š” HTTP ์š”์ฒญ ๋ฉ”์‹œ์ง€๋ฅผ ํŽธํ•˜๊ฒŒ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค.

 

โ—พ START LINE

  • HTTP ๋ฉ”์„œ๋“œ
  • URL
  • ์ฟผ๋ฆฌ ์ŠคํŠธ๋ง
  • ์Šคํ‚ค๋งˆ, ํ”„๋กœํ† ์ฝœ

 

โ—พ ํ—ค๋”

 

โ—พ ๋ฐ”๋””

  • form ํŒŒ๋ผ๋ฏธํ„ฐ ํ˜•์‹ ์กฐํšŒ
  • message body ๋ฐ์ดํ„ฐ ์ง์ ‘ ์กฐํšŒ

โž• ๋ถ€๊ฐ€ ๊ธฐ๋Šฅ

 

โ—พ ์ž„์‹œ ์ €์žฅ์†Œ ๊ธฐ๋Šฅ

HTTP ์š”์ฒญ์ด ์‹œ์ž‘๋ถ€ํ„ฐ ๋๋‚  ๋•Œ๊นŒ์ง€ ์œ ์ง€๋˜๋Š” ์ž„์‹œ ์ €์žฅ์†Œ ๊ธฐ๋Šฅ

  • ์ €์žฅ : request.setAttribute(name, value)
  • ์กฐํšŒ : request.getAttribute(name)

โ—พ ์„ธ์…˜ ๊ด€๋ฆฌ ๊ธฐ๋Šฅ

  • request.getSession(create: true)

 

โœ HttpServletRequest ๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ•

@WebServlet(name = "requestHeaderServlet", urlPatterns = "/request-header")
public class RequestHeaderServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        printStartLine(request); // START LINE ์ถœ๋ ฅ
        printHeader(request); // ํ—ค๋” ์ •๋ณด ์ถœ๋ ฅ
        printHeaderUtils(request); // ํŽธ๋ฆฌํ•œ ์กฐํšŒ
        printEtc(request); //๊ธฐํƒ€ ์ •๋ณด
    }

์ด ํด๋ž˜์Šค ์•ˆ์—์„œ ์š”์ฒญ ์กฐํšŒ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด์„œ ์š”์ฒญ ๋ฉ”์‹œ์ง€๋ฅผ ์กฐํšŒํ•œ๋‹ค.

 

๐ŸŒ  START LINE ์กฐํšŒ

private void printStartLine(HttpServletRequest request) {
        System.out.println("--- REQUEST-LINE - start ---");
        System.out.println("request.getMethod() = " + request.getMethod()); //GET
        System.out.println("request.getProtocal() = " + request.getProtocol()); //HTTP/1.1
        System.out.println("request.getScheme() = " + request.getScheme()); //http
        System.out.println("request.getRequestURL() = " + request.getRequestURL());
        // http://localhost:8080/request-header
        System.out.println("request.getRequestURI() = " + request.getRequestURI());
        // /request-header
        System.out.println("request.getQueryString() = " + request.getQueryString()); // null
        System.out.println("request.isSecure() = " + request.isSecure()); //https ์‚ฌ์šฉ ์œ ๋ฌด
        System.out.println("--- REQUEST-LINE - end ---");
    }

 

  • request.getMethod() : http ๋ฉ”์„œ๋“œ ์กฐํšŒ ( GET, POST, PATCH, PUT, DELETE...)
  • request.getProtocol() : httpํ”„๋กœํ† ์ฝœ ์กฐํšŒ
  • request.getScheme() : http ์Šคํ‚ค๋งˆ ์กฐํšŒ (http, https)
  • request.getRequestURL() : http ์ฃผ์†Œ ์กฐํšŒ
  • request.getRequestURI() : http uri ์กฐํšŒ
  • request.getQueryString() : http ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ ์กฐํšŒ ( ex)? username=hello ์—ฌ๊ธฐ์— ์žˆ๋Š” ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ)
  • request.isSecure() : https์ธ์ง€ ์•„๋‹Œ์ง€

์‹คํ–‰ํ•˜๊ณ  localhost:8080/request-header๋กœ ์ด๋™ํ•˜๋ฉด START LINE ์ •๋ณด๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.

 

๐ŸŒ  ํ—ค๋” ์ •๋ณด

private void printHeader(HttpServletRequest request) {
        System.out.println("--- Header - start ---");

        request.getHeaderNames().asIterator()
                .forEachRemaining(headerName -> System.out.println(headerName + ": " + request.getHeader(headerName)));

        System.out.println("--- Header - end ---");
    }

๋ชจ๋“  ํ—ค๋” ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•ด์ค€๋‹ค.

http://localhost:8080/request-header? username=hello

์œ„์™€ ๊ฐ™์€ ์ฃผ์†Œ๋กœ ์กฐํšŒํ–ˆ์„ ๊ฒฝ์šฐ์— ์•„๋ž˜์™€ ๊ฐ™์€ ์ •๋ณด๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.

 

 

๐ŸŒ  Header ํŽธ๋ฆฌํ•œ ์กฐํšŒ

๐Ÿ“ header (host, port) , Accept-Language, cookie, Content ์กฐํšŒํ•˜๊ธฐ

 

โ—พ header (host, port) ์กฐํšŒ

System.out.println("[Host ํŽธ์˜ ์กฐํšŒ]");
System.out.println("request.getServerName() = " +
	request.getServerName()); //Host ํ—ค๋”
System.out.println("request.getServerPort() = " +
	request.getServerPort()); //Host ํ—ค๋”

 

โ—พ Accept-Language ์กฐํšŒ

request.getLocales().asIterator()
		.forEachRemaining(locale -> System.out.println("locale = " + locale));
System.out.println("request.getLocale() = " + request.getLocale());

 

โ—พ Cookie ์กฐํšŒ

if (request.getCookies() != null) {
		for (Cookie cookie : request.getCookies()) {
			System.out.println(cookie.getName() + ": " + cookie.getValue());
		}
}

 

โ—พ Content ์กฐํšŒ

System.out.println("request.getContentType() = " + request.getContentType());
System.out.println("request.getContentLength() = " + request.getContentLength());
System.out.println("request.getCharacterEncoding() = " + request.getCharacterEncoding());

 

๐ŸŒ  ๊ธฐํƒ€ ์ •๋ณด

๐Ÿ“ Remote ์ •๋ณด, Local ์ •๋ณด

 

โ—พ Remote ์ •๋ณด

System.out.println("request.getRemoteHost() = " + request.getRemoteHost()); 
System.out.println("request.getRemoteAddr() = " + request.getRemoteAddr()); 
System.out.println("request.getRemotePort() = " + request.getRemotePort());

โ—พ Local ์ •๋ณด

System.out.println("request.getLocalName() = " + request.getLocalName());
System.out.println("request.getLocalAddr() = " + request.getLocalAddr());
System.out.println("request.getLocalPort() = " + request.getLocalPort());

 

 

[ HTTP ์š”์ฒญ ๋ฐ์ดํ„ฐ ]

http ์š”์ฒญ ๋ฉ”์‹œ์ง€๋ฅผ ํ†ตํ•ด ํด๋ผ์ด์–ธํŠธ์—์„œ ์„œ๋ฒ„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์—๋Š” 3๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.

 

โ—พ GET - ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ

  • /url? username=hello&age=20
  • ๋ฉ”์‹œ์ง€ ๋ฐ”๋”” ์—†์ด, URL์˜ ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ์— ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จํ•ด์„œ ์ „๋‹ฌ
  • Ex) ๊ฒ€์ƒ‰, ํ•„ํ„ฐ, ํŽ˜์ด์ง•์—์„œ ๋งŽ์ด ์‚ฌ์šฉ

โ—พ POST -  HTML Form

  • content-type: application/x-www-form-unlencoded
  • ๋ฉ”์‹œ์ง€ ๋ฐ”๋””์— ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ ํ˜•์‹์œผ๋กœ ์ „๋‹ฌ username=hello&age=20
  • ํšŒ์›๊ฐ€์ž…, ์ƒํ’ˆ ์ฃผ๋ฌธ, HTML Form ์‚ฌ์šฉ

โ—พ HTTP message body์— ๋ฐ์ดํ„ฐ๋ฅผ ์ง์ ‘ ๋‹ด์•„์„œ ์š”์ฒญ

  • HTTP  API์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉ, JSON, XML, TEXT
  • ๋ฐ์ดํ„ฐ ํ˜•์‹์€ ์ฃผ๋กœ JSON ์‚ฌ์šฉ (POST, PUT, PATCH)

โœ GET๊ณผ POST๋Š” ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ ํ˜•์‹์œผ๋กœ ์ „๋‹ฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— request.getParameter()๋กœ ์กฐํšŒ ๊ฐ€๋Šฅ

 

 

๐ŸŒ   Get

http://localhost:8080/request-param?username=hello&username=kim&age=20

์œ„์™€ ๊ฐ™์ด username์„ 2๊ฐœ ๋„ฃ๊ณ , age๋ฅผ ๋„ฃ๋Š” ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ ์ฃผ์†Œ์— ๋„ฃ์—ˆ๋‹ค. get๋ฐฉ์‹์€ ์ฃผ์†Œ์— ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ ํ˜•์‹์œผ๋กœ ์กฐํšŒํ•˜๊ธฐ ๋•Œ๋ฌธ์— url์— ๋„ฃ์–ด์„œ ์กฐํšŒํ•ด์•ผ ํ•œ๋‹ค.

@WebServlet(name = " requestParamServlet", urlPatterns = "/request-param") //์„œ๋ธ”๋ฆฟ ์—ฐ๊ฒฐ
public class RequestParamServlet extends HttpServlet {

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

        System.out.println("[์ „์ฒด ํŒŒ๋ผ๋ฏธํ„ฐ ์กฐํšŒ] - start");

        request.getParameterNames().asIterator()
                .forEachRemaining(paramName -> System.out.println(paramName + "=" + request.getParameter(paramName)));

        System.out.println("[์ „์ฒด ํŒŒ๋ผ๋ฏธํ„ฐ ์กฐํšŒ] - end");
        System.out.println();

        System.out.println("[๋‹จ์ผ ํŒŒ๋ผ๋ฏธํ„ฐ ์กฐํšŒ]");
        String username = request.getParameter("username");
        System.out.println("username = " + username);
        String age = request.getParameter("age");
        System.out.println("age = " + age);
        System.out.println();

        System.out.println("[์ด๋ฆ„์ด ๊ฐ™์€ ๋ณต์ˆ˜ ํŒŒ๋ผ๋ฏธํ„ฐ ์กฐํšŒ]");
        String[] usernames = request.getParameterValues("username");
        for (String name : usernames) {
            System.out.println("username = " + name);
        }
    }
}

>> ์‹คํ–‰ํ™”๋ฉด

 

๐ŸŒ  POST

HTML Form์„ ์‚ฌ์šฉํ•ด์„œ ํด๋ผ์ด์–ธํŠธ์—์„œ ์„œ๋ฒ„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†ก

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="/request-param" method="post">
    username: <input type="text" name="username" />
    age: <input type="text" name="age" />
    <button type="submit">์ „์†ก</button>
</form>
</body>
</html>

์ด๋ ‡๊ฒŒ ๋ฉ”์‹œ์ง€ ๋ฐ”๋””์— ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ ํ˜•์‹์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•œ๋‹ค.

/request-param์œผ๋กœ ์ž‘๋™ํ•˜๋„๋ก ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ์œ„์— GET์œผ๋กœ ์กฐํšŒํ–ˆ๋˜ ๊ฒƒ๊ณผ ๊ฒฐ๊ณผ๊ฐ€ ๋˜‘๊ฐ™์ด ๋‚˜์˜จ๋‹ค.

์œ„์—์„œ ์„ค๋ช…ํ–ˆ๋“ฏ์ด GET๊ณผ POST๋Š” ๋ชจ๋‘ ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋˜‘๊ฐ™์ด ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๐ŸŒ  HTTP ์š”์ฒญ ๋ฐ์ดํ„ฐ - JSON

@Getter @Setter //getter setter๋ฅผ lombok์œผ๋กœ ์ž๋™ ์„ธํŒ…
public class HelloData {
    private String username;
    private int age;
}
@WebServlet(name = "requestBodyJsonServlet", urlPatterns = "/request-body-json")
public class RequestBodyJsonServlet extends HttpServlet {

    private ObjectMapper objectMapper = new ObjectMapper();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletInputStream inputStream = request.getInputStream();
        String messageBody = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);

        System.out.println("messageBody = " + messageBody);

        HelloData helloData = objectMapper.readValue(messageBody, HelloData.class);

        System.out.println("helloData.username = " + helloData.getUsername());
        System.out.println("helloData.age = " + helloData.getAge());

    }
}

>> ์กฐํšŒ ํ™”๋ฉด

POST MAN์„ ์ด์šฉํ•ด์„œ ์กฐํšŒํ•ด๋ณด์•˜๋‹ค. ๊ทธ๋Ÿผ ์ด๋ ‡๊ฒŒ JSON ํ˜•์‹์œผ๋กœ ์กฐํšŒ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค. 

JSON ๋ฐฉ๋ฒ•์ด ์ฝ๊ธฐ ํŽธํ•ด์„œ ๊ฐ€์žฅ ๋งŽ์ด ์“ด๋‹ค๊ณ  ํ•œ๋‹ค!

 

 

[ HttpServletResponse ]

 

๐Ÿ“ HttpServletResponse ์—ญํ• 

  • 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); // 200

        //[response-headers]
        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);
        cookie(response);
        redirect(response);
    }

    //content ํŽธ์˜ ๋ฉ”์„œ๋“œ
    private void content(HttpServletResponse response) {
        response.setContentType("text/plain");
        response.setCharacterEncoding("utf-8");
    }

    //cookie ํŽธ์˜ ๋ฉ”์„œ๋“œ
    private void cookie(HttpServletResponse response) {
        Cookie cookie = new Cookie("myCookie", "good");
        cookie.setMaxAge(600); //600์ดˆ
        response.addCookie(cookie);
    }

    //redirect ํŽธ์˜ ๋ฉ”์„œ๋“œ
    private void redirect(HttpServletResponse response) throws IOException {
        response.sendRedirect("/basic/hello-form.html");
    }
}

 

โœ response.setStatus(HttpServletResponse.SC_OK); ์ด๊ฑธ ์“ฐ๋Š” ์ด์œ ๋Š” 200 ok๋ฅผ ์ง€์ •ํ•ด์ฃผ๋Š” ๊ฒƒ์ด๋‹ค.

response.setStatus(200); ์ด๋ ‡๊ฒŒ ์จ๋„ ๋œ๋‹ค! 

๊ฐœ๋ฐœ์ž ๋„๊ตฌ์—์„œ ํ™•์ธํ•ด๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๋‹ค๋ฅธ ์ˆซ์ž๋กœ ๋ฐ”๊พธ๋ฉด ๋‹ค๋ฅธ ์ˆซ์ž๋กœ ๋‚˜์˜จ๋‹ค!

 

redirect์— ๊ด€ํ•œ ๋‚ด์šฉ์€ ์ด์ „ ํฌ์ŠคํŒ…์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค! ๐Ÿ‘‡

 

HTTP ์ƒํƒœ์ฝ”๋“œ ์ •๋ฆฌ

HTTP์˜ ์‘๋‹ต ์ฝ”๋“œ๋Š” 5๊ฐœ์˜ ํด๋ž˜์Šค(๋ถ„๋ฅ˜)๋กœ ๊ตฌ๋ถ„๋œ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ˆซ์ž๋กœ ์‘๋‹ต ํด๋ž˜์Šค๊ฐ€ ๊ฒฐ์ •๋œ๋‹ค. 1xx (์ •๋ณด, Information) : ์š”์ฒญ์„ ๋ฐ›๊ณ , ํ”„๋กœ์„ธ์Šค๋ฅผ ๊ณ„์†ํ•œ๋‹ค. 2xx (์„ฑ๊ณต, Successful) : ์š”์ฒญ์„ ์„ฑ๊ณต์ ์œผ๋กœ ๋ฐ›

dahliachoi.tistory.com

 

๐ŸŒ  HTTP ์‘๋‹ต ๋ฐ์ดํ„ฐ - ๋‹จ์ˆœ ํ…์ŠคํŠธ, HTML

HTTP ์‘๋‹ต ๋ฉ”์‹œ์ง€๋Š” HTML๋กœ ๋‹ด์•„์„œ ์ „๋‹ฌํ•˜๋Š” ๊ฒฝ์šฐ์™€ JSON์œผ๋กœ ๋‚˜๋ˆ ์„œ ์ „๋‹ฌํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค.

๋จผ์ € HTML๋กœ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด์ž.

 

@WebServlet(name = "responseHtmlServlet", urlPatterns = "/response-html")
public class ResponseHtmlServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html"); //HTML๋กœ ๋ฐ˜ํ™˜ํ• ๋•Œ๋Š” ๊ผญ 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>");
        
        // servlet์œผ๋กœ html๋ Œ๋”๋งํ• ๋•Œ๋Š” ์ด๋ ‡๊ฒŒ ์ ์–ด์•ผ๋จ
    }
}

>> ์‹คํ–‰ ํ™”๋ฉด

์ด๋ ‡๊ฒŒ ์•ˆ๋…•?์ด๋ผ๊ณ  ๋‚˜์˜ค์ง€๋งŒ ๊ฐœ๋ฐœ์ž ๋„๊ตฌ์—์„œ ์†Œ์ŠคํŒŒ์ผ์„ ์‚ดํŽด๋ณด๋ฉด ์‘๋‹ต์œผ๋กœ ๋ณด๋‚ธ html๊ฐ€ ๋“ค์–ด์žˆ๋Š” ๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค!!

 

 

๐ŸŒ  HTTP ์‘๋‹ต ๋ฐ์ดํ„ฐ - 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.setContentType("application/json"); //Content-Type: application/json ๋กœ ์ง€์ •ํ•ด์ค˜์•ผ๋จ
        response.setCharacterEncoding("utf-8");

        HelloData helloData = new HelloData();
        helloData.setUsername("kim");
        helloData.setAge(20);

        //{"username":"kim", "age":20}
        String result = objectMapper.writeValueAsString(helloData); //๊ฐ์ฒด๋ฅผ JSON๋ฌธ์ž๋กœ ๋ณ€๊ฒฝ
        response.getWriter().write(result);
    }
}

 

>> ์‹คํ–‰ํ™”๋ฉด

์†Œ์Šค์ฝ”๋“œ๋ฅผ ๋ด๋„ json ํ˜•์‹์œผ๋กœ ์‘๋‹ต๋˜์—ˆ๋‹ค!!

 

 

โœ json์€ ์ด๋ฏธ ์ดˆ๊ธฐ ์„ค์ •์ด utf-8์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ตณ์ด ์ถ”๊ฐ€ํ•ด์ค„ ํ•„์š”๊ฐ€ ์—†๋‹ค.

 

 


์„œ๋ธ”๋ฆฟ์€ ์ด๋ฆ„๋งŒ ๋“ค์—ˆ์„ ๋•Œ ๋˜๊ฒŒ ์–ด๋ ค์šธ ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ ์ด๋ ‡๊ฒŒ ์‹ค์Šต์œผ๋กœ ํ•ด๋ณด๋‹ˆ๊นŒ ์–ด๋Š ์ •๋„ ์ดํ•ด๊ฐ€ ๊ฐ„ ๊ฒƒ ๊ฐ™๋‹ค.

๋ธ”๋กœ๊ทธ๋กœ ์ •๋ฆฌํ•˜๋‹ˆ๊นŒ ํ•œ๋ฒˆ ๋” ์ •๋ฆฌํ•˜๋Š” ๋Š๋‚Œ์ด์–ด์„œ ์ข‹์€ ๊ฒƒ ๊ฐ™๋‹ค!! ๐Ÿ˜Š

mvc๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ด๊ฒƒ๋ณด๋‹ค ํ›จ์”ฌ ์ฝ”๋“œ๊ฐ€ ๋งŽ์ด ์ค„์–ด๋“ ๋‹ค๋˜๋ฐ ๋ฒŒ์จ ์‹ ๊ธฐํ•˜๋‹ค,,

์›นํŽ˜์ด์ง€๊ฐ€ ๋’ค์—์„œ ๋˜๊ฒŒ ๋ณต์žกํ•˜๊ฒŒ ๋™์ž‘ํ•˜๊ณ  ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ๋‹ค์‹œ ํ•œ๋ฒˆ ๋Š๋ผ๊ฒŒ ๋˜์—ˆ๋‹ค( ษตฬฅฬฅ ห‘ฬซ ษตฬฅฬฅ) ์•ž์œผ๋กœ ๋” ํŒŒ์ดํŒ…!

 

์ด ํฌ์ŠคํŒ…์€ ์ธํ”„๋Ÿฐ ๊น€์˜ํ•œ ๋‹˜์˜ ์Šคํ”„๋ง MVC1ํŽธ์„ ๋“ฃ๊ณ  ์ž‘์„ฑ๋œ ๋ณต์Šต ์ •๋ฆฌ ํฌ์ŠคํŒ…์ž…๋‹ˆ๋‹ค.