๐Ÿ“–
Aaron's TECH BOOK
  • Intro
    • About me
  • Lecture
    • Kubernetes
      • Begin Kubernetes
    • Kafka
      • Begin Kafka
    • Kotlin
      • TDD, Clean Code Preview
      • woowa Kotlin
    • Java
      • Multithread Concurrency
      • The Java
    • Toby
      • Toby Spring 6
      • Toby Spring Boot
    • MSA
      • 01.Micro Service
      • 02.DDD ์„ค๊ณ„
      • 03.DDD ๊ตฌํ˜„
      • 04.EDA ๊ตฌํ˜„
    • Spring Boot
    • Spring Batch
    • Spring Core Advanced
    • Spring DB Part II
    • Spring DB Part I
    • JPA API and Performance Optimization
    • JPA Web Application
    • JPA Programming Basic
    • Spring MVC Part 2
      • 01.Thymeleaf
      • 02.ETC
      • 03.Validation
      • 04.Login
      • 05.Exception
    • Spring MVC Part 1
      • 01.Servlet
      • 02.MVC
    • Http
      • 01.Basic
      • 02.Method
      • 03.Header
    • Spring Core
    • Study
      • Concurrency issues
      • First Come First Served
      • Performance Test
      • TDD
      • IntelliJ
  • Book
    • Kafka Streams in Action
      • 01.์นดํ”„์นด ์ŠคํŠธ๋ฆผ์ฆˆ
      • 02.์นดํ”„์นด ์ŠคํŠธ๋ฆผ์ฆˆ ๊ฐœ๋ฐœ
      • 03.์นดํ”„์นด ์ŠคํŠธ๋ฆผ์ฆˆ ๊ด€๋ฆฌ
    • Effective Kotlin
      • 01.์ข‹์€ ์ฝ”๋“œ
      • 02.์ฝ”๋“œ ์„ค๊ณ„
      • 03.ํšจ์œจ์„ฑ
    • ์ด๋ฒคํŠธ ์†Œ์‹ฑ๊ณผ MSA
      • 01.๋„๋ฉ”์ธ ์ฃผ๋„ ์„ค๊ณ„
      • 02.๊ฐ์ฒด์ง€ํ–ฅ ์„ค๊ณ„ ์›์น™
      • 03-04.์ด๋ฒคํŠธ ์†Œ์‹ฑ
      • 05.๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค ํ˜‘์—…
      • 06.๊ฒฐ๊ณผ์  ์ผ๊ด€์„ฑ
      • 07.CQRS
      • 08.UI
      • 09.ํด๋ผ์šฐ๋“œ ํ™˜๊ฒฝ
    • ๋ชฝ๊ณ DB ์™„๋ฒฝ ๊ฐ€์ด๋“œ
      • I. ๋ชฝ๊ณ DB ์‹œ์ž‘
      • II. ๋ชฝ๊ณ DB ๊ฐœ๋ฐœ
    • Kotlin Cookbook
      • ์ฝ”ํ‹€๋ฆฐ ๊ธฐ์ดˆ
      • ์ฝ”ํ‹€๋ฆฐ ๊ธฐ๋Šฅ
      • ETC
    • Kotlin in Action
      • ํ•จ์ˆ˜/ํด๋ž˜์Šค/๊ฐ์ฒด/์ธํ„ฐํŽ˜์ด์Šค
      • ๋žŒ๋‹ค์™€ ํƒ€์ž…
      • ์˜ค๋ฒ„๋กœ๋”ฉ๊ณผ ๊ณ ์ฐจ ํ•จ์ˆ˜
      • ์ œ๋„ค๋ฆญ์Šค, ์• ๋…ธํ…Œ์ด์…˜, ๋ฆฌํ”Œ๋ ‰์…˜
    • Kent Beck Tidy First?
    • ๋Œ€๊ทœ๋ชจ ์‹œ์Šคํ…œ ์„ค๊ณ„ ๊ธฐ์ดˆ
      • 01.์‚ฌ์šฉ์ž ์ˆ˜์— ๋”ฐ๋ฅธ ๊ทœ๋ชจ ํ™•์žฅ์„ฑ
      • 02.๊ฐœ๋žต์ ์ธ ๊ทœ๋ชจ ์ถ”์ •
      • 03.์‹œ์Šคํ…œ ์„ค๊ณ„ ๊ณต๋žต๋ฒ•
      • 04.์ฒ˜๋ฆฌ์œจ ์ œํ•œ ์žฅ์น˜ ์„ค๊ณ„
      • 05.์•ˆ์ • ํ•ด์‹œ ์„ค๊ณ„
      • 06.ํ‚ค-๊ฐ’ ์ €์žฅ์†Œ ์„ค๊ณ„
      • 07.์œ ์ผ ID ์ƒ์„ฑ๊ธฐ ์„ค๊ณ„
      • 08.URL ๋‹จ์ถ•๊ธฐ ์„ค๊ณ„
      • 09.์›น ํฌ๋กค๋Ÿฌ ์„ค๊ณ„
      • 10.์•Œ๋ฆผ ์‹œ์Šคํ…œ ์„ค๊ณ„
      • 11.๋‰ด์Šค ํ”ผ๋“œ ์‹œ์Šคํ…œ ์„ค๊ณ„
      • 12.์ฑ„ํŒ… ์‹œ์Šคํ…œ ์„ค๊ณ„
      • 13.๊ฒ€์ƒ‰์–ด ์ž๋™์™„์„ฑ ์‹œ์Šคํ…œ
      • 14.์œ ํŠœ๋ธŒ ์„ค๊ณ„
      • 15.๊ตฌ๊ธ€ ๋“œ๋ผ์ด๋ธŒ ์„ค๊ณ„
      • 16.๋ฐฐ์›€์€ ๊ณ„์†๋œ๋‹ค
    • ์‹ค์šฉ์ฃผ์˜ ํ”„๋กœ๊ทธ๋ž˜๋จธ๐Ÿ“–
    • GoF Design Patterns
    • ๋„๋ฉ”์ธ ์ฃผ๋„ ๊ฐœ๋ฐœ ์‹œ์ž‘ํ•˜๊ธฐ
      • 01.๋„๋ฉ”์ธ ๋ชจ๋ธ ์‹œ์ž‘ํ•˜๊ธฐ
      • 02.์•„ํ‚คํ…์ฒ˜ ๊ฐœ์š”
      • 03.์• ๊ทธ๋ฆฌ๊ฑฐํŠธ
      • 04.๋ฆฌํฌ์ง€ํ„ฐ๋ฆฌ์™€ ๋ชจ๋ธ ๊ตฌํ˜„
      • 05.Spring Data JPA๋ฅผ ์ด์šฉํ•œ ์กฐํšŒ ๊ธฐ๋Šฅ
      • 06.์‘์šฉ ์„œ๋น„์Šค์™€ ํ‘œํ˜„ ์˜์—ญ
      • 07.๋„๋ฉ”์ธ ์„œ๋น„์Šค
      • 08.์• ๊ทธ๋ฆฌ๊ฑฐํŠธ ํŠธ๋žœ์žญ์…˜ ๊ด€๋ฆฌ
      • 09.๋„๋ฉ”์ธ ๋ชจ๋ธ๊ณผ ๋ฐ”์šด๋””๋“œ ์ปจํ…์ŠคํŠธ
      • 10.์ด๋ฒคํŠธ
      • 11.CQRS
    • Effective Java 3/E
      • ๊ฐ์ฒด, ๊ณตํ†ต ๋ฉ”์„œ๋“œ
      • ํด๋ž˜์Šค, ์ธํ„ฐํŽ˜์ด์Šค, ์ œ๋„ค๋ฆญ
    • ์†Œํ”„ํŠธ์›จ์–ด ์žฅ์ธ
    • ํ•จ๊ป˜ ์ž๋ผ๊ธฐ
    • Modern Java In Action
      • 01.๊ธฐ์ดˆ
      • 02.ํ•จ์ˆ˜ํ˜• ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ
      • 03.์ŠคํŠธ๋ฆผ๊ณผ ๋žŒ๋‹ค๋ฅผ ์ด์šฉํ•œ ํšจ๊ณผ์  ํ”„๋กœ๊ทธ๋ž˜๋ฐ
      • 04.๋งค์ผ ์ž๋ฐ”์™€ ํ•จ๊ป˜
    • Refactoring
      • 01.๋ฆฌํŽ™ํ„ฐ๋ง ์ฒซ ๋ฒˆ์งธ ์˜ˆ์‹œ
      • 02.๋ฆฌํŽ™ํ„ฐ๋ง ์›์น™
      • 03.์ฝ”๋“œ์—์„œ ๋‚˜๋Š” ์•…์ทจ
      • 06.๊ธฐ๋ณธ์ ์ธ ๋ฆฌํŽ™ํ„ฐ๋ง
      • 07.์บก์Аํ™”
      • 08.๊ธฐ๋Šฅ ์ด๋™
      • 09.๋ฐ์ดํ„ฐ ์กฐ์งํ™”
      • 10.์กฐ๊ฑด๋ถ€ ๋กœ์ง ๊ฐ„์†Œํ™”
      • 11.API ๋ฆฌํŒฉํ„ฐ๋ง
      • 12.์ƒ์† ๋‹ค๋ฃจ๊ธฐ
    • ๊ฐ์ฒด์ง€ํ–ฅ์˜ ์‚ฌ์‹ค๊ณผ ์˜คํ•ด
      • 01.ํ˜‘๋ ฅํ•˜๋Š” ๊ฐ์ฒด๋“ค์˜ ๊ณต๋™์ฒด
      • 02.์ด์ƒํ•œ ๋‚˜๋ผ์˜ ๊ฐ์ฒด
      • 03.ํƒ€์ž…๊ณผ ์ถ”์ƒํ™”
      • 04.์—ญํ• , ์ฑ…์ž„, ํ˜‘๋ ฅ
      • 05.์ฑ…์ž„๊ณผ ๋ฉ”์‹œ์ง€
      • 06.๊ฐ์ฒด ์ง€๋„
      • 07.ํ•จ๊ป˜ ๋ชจ์œผ๊ธฐ
      • ๋ถ€๋ก.์ถ”์ƒํ™” ๊ธฐ๋ฒ•
    • Clean Code
    • ์ž๋ฐ” ORM ํ‘œ์ค€ JPA ํ”„๋กœ๊ทธ๋ž˜๋ฐ
Powered by GitBook
On this page
  • Web Application
  • Web Server
  • Web Application Server (WAS)
  • Web System
  • Servlet
  • Multi Thread
  • HTML, API, SSR, CSR
  • Servlet
  • Project
  • HttpServletRequest
  • HTTP Request Data
  • HttpServletResponse
  • HTTP Response Data
  1. Lecture
  2. Spring MVC Part 1

01.Servlet

Web Application

Web Server

  • HTTP ๊ธฐ๋ฐ˜์œผ๋กœ ๋™์ž‘

  • ์ •์  ๋ฆฌ์†Œ์Šค(HTML, CSS, JS, Img, Video ..) ์ œ๊ณต

  • ex) NGINX, APACHE

Web Application Server (WAS)

  • HTTP ๊ธฐ๋ฐ˜ ๋™์žฅ

  • Web server ๊ธฐ๋Šฅ ํฌํ•จ + ์ •์  ๋ฆฌ์†Œ์Šค

  • Application Logic ์ˆ˜ํ–‰

    • ๋™์  HTML, REST API, Servlet, JSP, Spring MVC

  • ex) Tomcat, Undertow

Web System

  • Client -> Web Server -> WAS -> DB

    • WAS์˜ ์„œ๋ฒ„ ๊ณผ๋ถ€ํ•˜ ๋ฌธ์ œ ํ•ด๊ฒฐ

    • Web Server ์—์„œ ์ •์  ๋ฆฌ์†Œ์Šค ์ฒ˜๋ฆฌ

    • WAS ์—์„œ๋Š” Application Logic ๋™์  ์ฒ˜๋ฆฌ

    • ํšจ์œจ์ ์ธ ๋ฆฌ์†Œ์Šค ๊ด€๋ฆฌ (ํ•„์š”์— ๋”ฐ๋ผ ์„œ๋ฒ„ ์ฆ์„ค)

Servlet

Servlet

  • urlPatterns์˜ URL์ด ํ˜ธ์ถœ๋˜๋ฉด ์„œ๋ธ”๋ฆฟ ์ฝ”๋“œ ์‹คํ–‰

  • HttpServletRequest ๋กœ HTTP ์š”์ฒญ ์ •๋ณด๋ฅผ ํŽธ๋ฆฌํ•˜๊ฒŒ ์‚ฌ์šฉ

  • HttpServletResponse ๋กœ HTTP ์‘๋‹ต ์ •๋ณด๋ฅผ ํŽธ๋ฆฌํ•˜๊ฒŒ ์ œ๊ณต

Servlet Container

  • Servlet ์„ ์ง€์›ํ•˜๋Š” WAS

  • Servlet ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ/์ดˆ๊ธฐํ™”/ํ˜ธ์ถœ/์ข…๋ฃŒํ•˜๋Š” ์ƒ๋ช…์ฃผ๊ธฐ ๊ด€๋ฆฌ

  • Servlet ๊ฐ์ฒด๋Š” ์‹ฑ๊ธ€ํ†ค์œผ๋กœ ๊ด€๋ฆฌ

  • JSP ๋„ Servlet ์œผ๋กœ ๋ณ€ํ™˜ ๋˜์–ด ์‚ฌ์šฉ

  • ๋™์‹œ ์š”์ฒญ์„ ์œ„ํ•œ ๋ฉ€ํ‹ฐ ์“ฐ๋ ˆ๋“œ ์ฒ˜๋ฆฌ ์ง€์›

Multi Thread

Thread

  • Application ์ฝ”๋“œ๋ฅผ ํ•˜๋‚˜ํ•˜๋‚˜ ์ˆœ์ฐจ์ ์œผ๋กœ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ

Thread pool

  • ํ•„์š”ํ•œ Thread ๋ฅผ Thread Pool ์— ๋ณด๊ด€ํ•˜๊ณ  ๊ด€๋ฆฌ

  • Thread Pool ์— ์ƒ์„ฑ ๊ฐ€๋Šฅํ•œ Thread ์˜ ์ตœ๋Œ€์น˜๋ฅผ ๊ด€๋ฆฌ

    • Tomcat default : ์ตœ๋Œ€ 200๊ฐœ

  • WAS ์ฃผ์š” ํŠœ๋‹ ํฌ์ธํŠธ๋Š” Max Thread

    • ๋„ˆ๋ฌด ๋‚ฎ์„ ๊ฒฝ์šฐ : ๋™์‹œ ์š”์ฒญ์ด ๋งŽ์œผ๋ฉด, ์„œ๋ฒ„ ๋ฆฌ์†Œ์Šค๋Š” ์—ฌ์œ ๋กญ์ง€๋งŒ, ํด๋ฆฌ์ด์–ธํŠธ๋Š” ์‘๋‹ต ์ง€์—ฐ

    • ๋„ˆ๋ฌด ๋†’์„ ๊ฒฝ์šฐ : ๋™์‹œ ์š”์ฒญ์ด ๋งŽ์œผ๋ฉด, CPU, ๋ฉ”๋ชจ๋ฆฌ ๋ฆฌ์†Œ์Šค ์ž„๊ณ„์  ์ดˆ๊ณผ๋กœ ์„œ๋ฒ„ ๋‹ค์šด

    • ์žฅ์•  ๋ฐœ์ƒ ์‹œ : ํด๋ผ์šฐ๋“œ์ผ ๊ฒฝ์šฐ ์„œ๋ฒ„๋ฅผ ๋Š˜๋ฆฐ ํ›„ ํŠœ๋‹, ์•„๋‹ˆ๋ฉด ๊ทธ๋ƒฅ ๋นกํŠœ๋‹..

  • ๊ฐœ๋ฐœ์ž๊ฐ€ Multi Thread ๊ด€๋ จ ์ฝ”๋“œ๋ฅผ ์‹ ๊ฒฝ์“ฐ์ง€ ์•Š์•„๋„ ๋จ (Multi Thread ์— ๋Œ€ํ•œ ๋ถ€๋ถ„์€ WAS๊ฐ€ ์ฒ˜๋ฆฌ)

    • ๋‹จ, Max Thread ํŠœ๋‹์ด ์ค‘์š”

  • ์„ฑ๋Šฅ ํ…Œ์ŠคํŠธ Tool : nGrinder, Apache ab, JMeter

HTML, API, SSR, CSR

HTMl

Static Resources

  • HTML, CSS, JS, image, vedio ..

  • Web Browser โžœ Web Server โžœ Static Resources

HTML ํŽ˜์ด์ง€

  • ๋™์  HTML ํŒŒ์ผ์„ ์ƒ์„ฑํ•ด์„œ ์ „๋‹ฌ

  • JSP, Thymeleaf ..

  • Web Browser โžœ WAS (HTML) โžœ DB

HTML API

  • ๋ฐ์ดํ„ฐ(์ฃผ๋กœ JSON) ์ „๋‹ฌ

  • Web Browser โžœ WAS (DATA) โžœ DB

  • Web/App Client, Server to Server

SSR

  • Server Side Rendering

  • ์„œ๋ฒ„์—์„œ ์ตœ์ข… HTML์„ ์ƒ์„ฑํ•ด์„œ ํด๋ผ์ด์–ธํŠธ์— ์ „๋‹ฌ (์ฃผ๋กœ ์ •์  ํ™”๋ฉด)

  • JSP, Thymeleaf ..

CSR

  • HTML ๊ฒฐ๊ณผ๋ฅผ Javascript๋ฅผ ์‚ฌ์šฉํ•ด ์›น ๋ธŒ๋ผ์šฐ์ €์—์„œ ๋™์ ์œผ๋กœ ์ƒ์„ฑํ•ด์„œ ์ ์šฉ (Google Map, Gmail ..)

  • React, Vue.js

Servlet

Project

IntelliJ

  • JSP ์‚ฌ์šฉ์„ ์œ„ํ•ด War Packaging ์‚ฌ์šฉ

  • Java ์ง์ ‘ ์‹คํ–‰ ์„ค์ •

    • Preferences Build โžœ Execution โžœ Deployment Build Tools Gradle

  • Lombok ์ ์šฉ

    • Preferences โžœ plugin โžœ lombok

    • Preferences โžœ Annotation Processors โžœ Enable annotation processing check

HTTP ์š”์ฒญ ๋ฉ”์‹œ์ง€ ๋กœ๊ทธ ํ™•์ธ

application.properties

logging.level.org.apache.coyote.http11=debug

HttpServletRequest

HTTP Start Line ์กฐํšŒ

private void printStartLine(HttpServletRequest request) {
    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());  // username=park
    System.out.println("request.isSecure() = " + request.isSecure()); //https ์‚ฌ์šฉ ์œ ๋ฌด
}

HTTP Header ํŽธ์˜ ์กฐํšŒ

private void printHeaderUtils(HttpServletRequest request) {
    System.out.println("[Host]");
    System.out.println("request.getServerName() = " + request.getServerName()); //Host ํ—ค๋”
    System.out.println("request.getServerPort() = " + request.getServerPort()); //Host ํ—ค๋”

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

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

    System.out.println("[Content]");
    System.out.println("request.getContentType() = " + request.getContentType()); //Get Method ์ผ ๊ฒฝ์šฐ null
    System.out.println("request.getContentLength() = " + request.getContentLength());
    System.out.println("request.getCharacterEncoding() = " + request.getCharacterEncoding());
}

HTTP ๊ธฐํƒ€ ์ •๋ณด

private void printEtc(HttpServletRequest request) {

    System.out.println("[Remote ์ •๋ณด]");
    System.out.println("request.getRemoteHost() = " + request.getRemoteHost());
    System.out.println("request.getRemoteAddr() = " + request.getRemoteAddr());
    System.out.println("request.getRemotePort() = " + request.getRemotePort());

    System.out.println("[Local ์ •๋ณด]");
    System.out.println("request.getLocalName() = " + request.getLocalName());
    System.out.println("request.getLocalAddr() = " + request.getLocalAddr());
    System.out.println("request.getLocalPort() = " + request.getLocalPort());
}

HTTP Request Data

HTTP ์š”์ฒญ ๋ฉ”์‹œ์ง€๋ฅผ ํ†ตํ•ด ํด๋ผ์ด์–ธํŠธ์—์„œ ์„œ๋ฒ„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ๋ฒ•

Get(URL Query Parameter)

  • ๋ฉ”์‹œ์ง€ ๋ฐ”๋”” ์—†์ด URL ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ์— ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จํ•ด์„œ ์ „๋‹ฌ

  • HTTP Message Body ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ Content-type ์ด ์—†์Œ

  • ex) ๊ฒ€์ƒ‰, ํ•„ํ„ฐ, ํŽ˜์ด์ง• ...

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

// ๋‹จ์ผ ํŒŒ๋ผ๋ฏธํ„ฐ ์กฐํšŒ
String username = request.getParameter("username");

// ํŒŒ๋ผ๋ฏธํ„ฐ ์ด๋ฆ„ ๋ชจ๋‘ ์กฐํšŒ
Enumeration<String> parameterNames = request.getParameterNames(); 

// ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ Map์œผ๋กœ ์กฐํšŒ
Map<String, String[]> parameterMap = request.getParameterMap(); 

// ๋ณต์ˆ˜ ํŒŒ๋ผ๋ฏธํ„ฐ ์กฐํšŒ
String[] usernames = request.getParameterValues("username");

Post(HTML Form)

  • HTTP Message Body ์— ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จํ•ด์„œ ์ „๋‹ฌํ•˜๋ฏ€๋กœ Content-type ์— ํฌํ•จ๋œ ๋ฐ์ดํ„ฐ ํ˜•์‹์„ ์ง€์ •

  • ex) ํšŒ์›๊ฐ€์ž…, ์ƒํ’ˆ์ฃผ๋ฌธ, HTML Form ...

/** 
 * http://localhost:8080/basic/hello-form.html
 * 
 * content-type: application/x-www-form-urlencoded
 * message body: username=hello&age=20
 */ 

// URL Query Parameter ํ˜•์‹๊ณผ ๋™์ผํ•˜๊ฒŒ ์กฐํšŒ
String username = request.getParameter("username");

HTTP Message Body

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

  • HTTP API ์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉ(JSON, XML, TEXT)

  • ๋ฐ์ดํ„ฐ ํ˜•์‹์€ ์ฃผ๋กœ JSON(POST, PUT, PATCH)

/**
 * POST http://localhost:8080/request-body-string
 * 
 * content-type: text/plain
 * message body: hello
 */
ServletInputStream inputStream = request.getInputStream();
String messageBody = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);
response.getWriter().write("ok");

/**
 * POST http://localhost:8080/request-body-json
 * 
 * content-type: application/json
 * message body: {"username": "hello", "age": 20}
 */
ServletInputStream inputStream = request.getInputStream();
String messageBody = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);
HelloData helloData = objectMapper.readValue(messageBody, HelloData.class);
response.getWriter().write("ok");

HttpServletResponse

Status-line

response.setStatus(HttpServletResponse.SC_OK); //200

Content-type

// = response.setHeader("Content-Type", "text/plain;charset=utf-8");
response.setContentType("text/plain");
response.setCharacterEncoding("utf-8");

Response-headers

response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // ์บ์‹œ ๋ฌดํšจํ™”
response.setHeader("Pragma", "no-cache"); // ์บ์‹œ ๋ฌดํšจํ™”(๊ณผ๊ฑฐ ๋ฒ„์ „)
response.setHeader("my-header","hello");

Cookie

// = response.setHeader("Set-Cookie", "myCookie=good; Max-Age=600");
Cookie cookie = new Cookie("myCookie", "good");
cookie.setMaxAge(600); // sec
response.addCookie(cookie);

Redirect

// response.setStatus(HttpServletResponse.SC_FOUND); // 302
// response.setHeader("Location", "/basic/hello-form.html");
response.sendRedirect("/basic/hello-form.html");

Mmessage body

PrintWriter writer = response.getWriter();
writer.println("ok");

HTTP Response Data

HTTP ์‘๋‹ต ๋ฉ”์‹œ์ง€

๋‹จ์ˆœ ํ…์ŠคํŠธ

response.getWriter().println("ok");

HTML

//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>HTML Test</div>");
writer.println("</body>");
writer.println("</html>");

HTML API (MessageBody JSON)

//..
  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 data = new HelloData();
      data.setUsername("kim");
      data.setAge(20);

      //Object to Json
      //{"username":"kim","age":20}
      String result = objectMapper.writeValueAsString(data);
      response.getWriter().write(result);
  }

Last updated 1 year ago