servlet life style 스프링 수업노트

 

SERVLET life cycle

  1. init() 초기화

The servlet is initialized by calling the init() method.

서블릿은 클라이언트가 요청해준후 바로 호출이 되는 것이 아니라 객체를 생성하고 초기화 작업을 거친 후 , 요청을 처리한다.

  1. servlet

service() → doxxx() ,The servlet calls service() method to process a client's requesst.

servlet 에서 service() 를 호출 ⇒ doxxx() 들에서 어떤것을 선택한다 ( get, post , )

  1. destory()

The servlet is terminated by calling the destroy() method.

The destroy() method is called only once at the end of the life cyle of a servlet. This method gives your servlet a chance to close database , halt background threads , write cookie lists or hit counts to disk, and perform other such cleanup activities

After the destroy() method is called, the servlet object is marked for garbage collection.

  1. Finally, servlet is garbage collectd by the garbage collector of the JVM

be cited from https://www.tutorialspoint.com/servlets/servlets-life-cycle.htm

Type of Http request type

The servlet container(i.e web server) calls the service() method to handle request coming from the client(browsers) and to write the formatted response back to the client

Each time the server receives a request for servlet, the server spawns a new thread and calls service . The service() method checks the HTTP request type(Get, Post, Put, Delete etc. )

you override either doGet() or doPost() depending on what type of request you receive from the client

  1. The doGet() Method

A GET request results from a normal request for a URL or from an HTML form that has no METHOD specified and it should be handled by doGet() method

  1. The doPost() Method

A POST request results from an HTML form that specifically lists POST as the Method and it should be handled by doPost() Method

servlet life-cycle scenario

first the HTTP requests coming to the server are delegated to the servlet container.

The servlet container loads the servlet before invoking the service() method

Then the servlet container handles multiple requests by spawning multiple threads, each executing the service() method of a single instance of the servlet

servlet 의 라이프 사이클

init() ==> service() ==> destroy()

init 은 로딩시 최초 1회 , service 메서드는 사용자가 요청을 할 때마다 , destroy() 는 서버 종료 or reload init 메서드는 해당 서블릿에서 사용하는 자원을 초기화 할 때 사용

로딩시 최초 1회 호출 : 로딩 되는 시점 ==> 해당 서블릿으로 최초 요청이 들어왔을 때 단 web.xml 의 servlet 엘레메트의 하위 엘레멘트이 load-on-startup 엘레멘트 값으로 양의 정수값을 입력할 경우 서버가 가동하면서 바로 init 메소드 호출

servlet container 가 요청을 처리하는 방법 등록된 url 매핑을 참고하여 등록된 서블릿으로 요청을 전달(service 메소드 호출) localhost/basicServlet ==> BasicSrervlet의 service 메소드를 통해 응답생성 localhost/index.jsp ==> servlet 설정에 있는 web.xml 에 등록된 *.jsp , *.jspx url-pattern 에 따라 jsp 라는 이름의 서블릿에서 처리 ( JspServlet) localhost/doc/20201223.txt 정적인 자료 ==>

web.xml 도

댓글