Web을 시작하면서 Servlet부터 명확히 알고 시작해봐야겠다 생각이 들었다.
Oracle 공식문서는 Servlet을 아래와 같이 설명한다.
A servlet is a Java programming language class used to extend the capabilities of servers that host applications accessed by means of a request-response programming model.
- What Is a Servlet?
문맥을 이해하려면 Host가 무엇인지 알아야한다.
Web hosting is a service that provides storage for the files that make up your website and the software, physical hardware, and network infrastructure that makes your website available to others on the internet.
- IBM
웹 사이트를 구성하는 파일과 인터넷상의 다른 사람들이 웹 사이트를 사용할 수 있도록 하는 소프트웨어, 물리적 하드웨어 및 네트워크 인프라에 대한 저장소를 제공하는 서비스라 설명한다.
내가 이해한 Servlet은 다음과 같다.
요청-응답 모델로서 웹을 통해 어플리케이션을 제공하는 서버의 기능을 확장하는 자바 언어 클래스 이다.
자바에서는 Servlet을 인터페이스로 제공되며 생애 주기동안 여러 메서드들을 제공한다.
이 중 init()
, service()
, destroy()
에 대해 알아보자
이 메서드는 Web Container에 Servlet 인스턴스가 없으면 처음에 한번만 호출된다.
ServletException이 발생하거나 웹 서버에서 정해진 기한 내에 반환되지 않는 경우 서비스에 배치할 수 없다.
public void init() throws ServletException {
// Initialization code like set up database etc....
}
init()
메서드가 성공적으로 완료된 후에만 호출될 수 있다.
클라이언트의 요청 중 HTTP 요청 Type(GET, POST, PUT, DELETE...)을 해석하고 그에 걸맞는 적절한 메서드를 호출한다.(doGet, doPost, doPut...)
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
// ...
}
Servlet 사용이 종료되면 Servlet Container에 의해 호출된다.
service()
메서드 내 모든 스레드가 종료되거나 제한 시간이 경과된 후에만 호출 가능하다.
Container에서 호출한 후 Servlet에서 service()
를 호출하지 않는다.
public void destroy() {
//
}
대부분 Servlet의 사용은 HttpServlet을 이용하지만 Servlet 기술은 HTTP 프로토콜에 한정되어 있지 않음을 알아야 한다.
17.1 What Is a Servlet? - Java Platform, Enterprise Edition (Java EE) 7
What is web hosting? - IBM
Introduction to Java Servlets - Baeldung
Introduction to Servlets and Servlet Containers - Baeldung
글 맛있게 써주세요
좀 더 맛있는 요리 부탁해요 🥰