HttpServletRequest
기존 서블릿은 WAS 직접 설치한 후, 클래스 파일로 빌드한 서블릿 코드를
WAS에 넣어 실행해야 함. But, 스프링 부트로 이런 불편함 해소 !
WAS 내장하고 있는 Spring Boot통해 바로 서블릿 사용이 가능해졌다
(= 어노테이션을 통한 설정 요함)
// @ServletComponentScan
// 스프링이 하위 패키지에서 서블릿 찾아 자동 등록 진행
@ServletComponentScan
@SpringBootApplication
public class ServletApplication {
public static void main(String[] args) {
SpringApplication.run(ServletApplication.class, args); }
}
// 서버 실행시 localhost:8080/hello 확인 가능
// name(서블릿명), urlPatterns(URL매핑)
@WebServlet(name = "helloServlet", urlPatterns = "/hello")
public class HelloServlet extends HttpServlet {
// public이 아닌 protected service Method를 override 해야함
@Override
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {}
}
그 外 자세한 내용 하단 블로그 참고
서블릿이 개발자 대신 http 요청 메시지를 파싱하고
그 결과를 HttpServletRequest 객체에 담아 제공
HttpServletRequest으로 편리하게 조회하는 HTTP 요청메시지
/** About HTTP 요청 메시지 **/
// -------- Start Line --------
// Http Method, URL
// 쿼리 스트링, 스키마, 프로토콜
// ------------------------------
POST/save HTTP/1.1
// ------ Header (헤더조회) ------
Host : localhost:8080
Content-Type : application/x-www-form-urlencoded
// ----------- Body -------------
// form parameter 형식 조회
// message body 데이터 직접 조회
//--------------------------------
username=kim?age=20
...
그 外 HttpServletRequest이 제공하는 부가기능