분류 전체보기 563

HTTP 응답 데이터 - 단순 텍스트, HTML / API JSON

HTTP 응답 메시지는 주로 단순 텍스트 응답 앞에서 살펴봄 ( writer.println("ok"); ) HTML 응답 HTML API - MessageBody-JSON 응답 HTML 응답하는 거를 알아보자. package hello.setvlet.basic.response; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException;..

Spring/SpringMVC 2022.04.22

HttpServletResponse - 기본 사용법

Cache-Control: no-cache, no-store, must-revalidate Connection: keep-alive Content-Length: 3 Content-Type: text/plain;charset=utf-8 Date: Thu, 21 Apr 2022 16:48:03 GMT Keep-Alive: timeout=60 my-header: hello Pragma: no-cache Set-Cookie: myCookie=good; Max-Age=600; Expires=Thu, 21-Apr-2022 16:58:03 GMT 응답 HttpServletResponse에 대해서 알아보자. HTTP 응답 메시지 생성 HTTP 응답코드 지정 200 400 500 401 403 등등 헤더 생성 바디 생성..

Spring/SpringMVC 2022.04.22

HTTP 요청 데이터 - API 메시지 바디 - JSON

HTTP API에서 사용하는 JSON 형식으로 데이터를 전달해보자. JSON 형식 전송 POST http://localhost:8080/request-body-json content-type: application/json message body: {"username": "hello", "age": 20} 결과: messageBody = {"username": "hello", "age": 20} 출력되게 만들어 보자. HelloData Class를 만들어주고 lombok 라이브러리가 있으니까 getter setter을 직접 쓰는 대신에 어노테이션으로 넣어준다. // basic - HelloData Class package hello.setvlet.basic; import lombok.Getter; imp..

Spring/SpringMVC 2022.04.22

HTTP 요청 데이터 - API 메시지 바디 - 단순 텍스트

HTTP 요청 데이터 - API 메시지 바디 - 단순 텍스트 HTTP message body에 데이터를 직접 담아서 요청 HTTP API에서 주로 사용, JSON, XML, TEXT 데이터 형식은 주로 JSON 사용 POST, PUT, PATCH 먼저 가장 단순한 텍스트 메시지를 HTTP 메시지 바디에 담아서 전송하고, 읽어보자. HTTP 메시지 바디의 데이터를 InputStream을 사용해서 직접 읽음 간단하게 코드로 작성을 해보자. // RequestBodyStringServlet class package hello.setvlet.basic.request; import org.springframework.util.StreamUtils; import javax.servlet.ServletExceptio..

Spring/SpringMVC 2022.04.22

HTTP 요청 데이터 - POST HTML Form - 로그 에러

이제 HTML의 Form을 이용해 클라이언트에서 서버로 데이터를 전송해보자. 회원가입, 상품 주문 등에서 사용하는 방식이다. content-type: application/x-www-form-urlencoded 메시지 바디에 쿼리 파라미터 형식으로 데이터를 전달한다. username=hello&age=20 기본적으로 html 파일을 하나 만들어주자.(hello-form.html) username: age: 전송 Form Data는 안보이네??,,,, 결과를 확인하게 되면 // RequestParamServlet Class System.out.println("[전체 파라미터 조회] - start"); request.getParameterNames().asIterator() // paramName은 user..

Spring/SpringMVC 2022.04.21

HTTP 요청 데이터 - GET 쿼리 파라미터

데이터를 클라이언트에서 서버로 전송해보자. 데이터가 있는데 username=hello age=20 메시지 바디 없이, URL의 "쿼리 파라미터"를 사용해서 데이터를 전달해보자. 예) 검색, 필터, 페이징등에서 많이 사용하는 방식 쿼리파라미터는URL에다음과같이 ?를시작으로보낼수있다.추가파라미터는 &로구분하면된다. http://localhost:8080/request-param?username=hello&age=20 그렇게 하면 서버에서는 HttpServletRequest 가 제공하는 다음 메서드를 통해 쿼리 파라미터를 편리하게 조회할 수 있다. 코드를 한번 작성을 해보자. package hello.setvlet.basic.request; import javax.servlet.ServletException..

Spring/SpringMVC 2022.04.21

HTTP 요청 데이터 - 개요

HTTP 요청 메시지를 통해서 클라이언트에서 서버로 데이터를 전달하는 방법을 알아보자. 3가지 방법이 있다. GET - 쿼리 파라미터 / url*?username=hello&age=20 메시지 바디 없이, URL의 쿼리 파라미터에 데이터를 포함해서 전달 예) 검색, 필터, 페이징등에서 많이 사용하는 방식 POST - HTML Form content-type: application/x-www-form-urlencoded 메시지 바디에 쿼리 파라미터 형식으로 전달 username=hello&age = 20 예) 회원 가입, 상품 주문, HTML Form 사용 HTTP message body HTTP API(REST API)에서 주로 사용, JSON, XML, TEXT 데이터 형식은 주로 JSON 사용 POS..

Spring/SpringMVC 2022.04.21

HttpServletRequest - 기본 사용법

HttpServletRequest를 사용하는 방법에 대해서 알아보자. 아래코드를 Extract Method로 해서 extracting을 해주고 package hello.setvlet.basic.request; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet(name = "requestHeaderServl..

Spring/SpringMVC 2022.04.21

HttpServletRequest - 개요

HttpServletRequest 역할 HTTP 요청 메시지를 개발자가 직접 파싱해서 사용해도 되지만, 매우 불편하다 서블릿은 개발자가 HTTP 요청 메시지를 편리하게 사용할 수 있도록 개발자 대신에 HTTP 요청 메시지를 파싱한다. 결과를 HttpServletRequest 객체에 담아서 제공한다. START LINE HTTP 메소드 URL 쿼리 스트링 스키마, 프로토콜 헤더 헤더 조회 바디 from 파라미터 형식 조회 message body 데이터 직접 조회 HttpServletRequest 객체는 추가로 여러가지 부가기능도 함께 제공한다. 임시 저장소 기능을 알아보면 해당 HTTP 요청이 시작부터 끝날 때 까지 유지되는 임시 저장소 기능 저장 : request.setAttribute(name, val..

Spring/SpringMVC 2022.04.21

Hello 서블릿

스프링 부트 환경에서 서블릿 등록하고 사용해보자. 서블릿은 톰캣 같은 웹 애플리케이션 서버를 직접 설치해서 서블릿 코드를 클래스 파일로 빌드해서 올리고 톰캣 서버를 실행하면되지만 매우 번거롭다. 스프링 부트는 톰캣 서버를 내장하고 있어서 톰캣 서버 설치 없이 편리하게 서블릿 코드를 실행 할 수 있다. 코드를 작성해보자. ServletApplication에서는 @ServletComponentScan // 서블릿 자동 등록 을 넣어주고 package hello.setvlet; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org..

Spring/SpringMVC 2022.04.21