본문으로 바로가기

웹브라우저 ---> 웹서버 ----> 웹어플리케이션 서버 ---> Servlet 컨테이너


1) 스레드 생성

2) Servlet 객체 생성




요청이 들어올때마다 객체를 생성한다. 

적은 숫자의 갯수일경우에는 서버의 부하가 심하지 않지만

많게 되면 객체가 메모리상에 상주하므로 부하가 심하게 된다. 



java를 이용하게 되면 jvm을 이용하여 요청이 들어올때미다 멀티 쓰레드를 이용(스레드 생성)한다  요청할 때마다 스레드가 계속 생성되는것이아니라 하나가지고 재활용하는것이다.

서버의 부하가 적게 걸려 효율적으로 운용이 가능하다. 


servlet 라이프 사이클 (생명주기)


servlet의 사용도가 높은 이유는 빠른 응답속도때문

servlet은 최초 요청시 객체가 만들어져 메모리에 로딩되고, 이후 요청 시에는 기존의 객체를 재활용하게 된다. 따라서 동작 속도가 빠르다.


1                             2                              3                                    4

Servlet 객체생성----> init()호출 ---> service(), doGet(), doPost()호출, ----->destroy호출()

최초 한번                최초요청시 한번            요청시 매번                        자원 해제 될때                                                          doget,dopost메소드없으면 서비스 메소드 실행


package com.javalec.ex;


import java.io.IOException;


import javax.annotation.PostConstruct;

import javax.annotation.PreDestroy;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


/**

 * Servlet implementation class LifeCycleEx

 */

@WebServlet("/LC") 어노테이션을 LC로 사용

public class LifeCycleEx extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public LifeCycleEx() {

        super();

        // TODO Auto-generated constructor stub

    }


// @Override

// public void service(ServletRequest arg0, ServletResponse arg1)

// throws ServletException, IOException {

// // TODO Auto-generated method stub

// System.out.println("service");

// }

    

    @Override

    public void init() throws ServletException { 최초호출시

    // TODO Auto-generated method stub

    System.out.println("init");

    }


@Override

public void destroy()  { 호출해제시

// TODO Auto-generated method stub

System.out.println("destroy");

}



/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

*/             

                      웹클라이언트가 직접 들어가므로 doget메소드  sysout이 출력된다.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

요청시 매번 객체 생성

// TODO Auto-generated method stub

System.out.println("doGet"); 

}


/**

* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

*/

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

                           요청시 매번 객체 생성

// TODO Auto-generated method stub

System.out.println("doPost");

}


}


출력 결과


정보: Server startup in 1424 ms


init

doGet

doGet

...


9월 26, 2017 11:36:44 오후 org.apache.catalina.core.ApplicationContext log

정보: ContextListener: contextDestroyed()

destroy

9월 26, 2017 11:36:44 오후 org.






package com.javalec.ex;


import java.io.IOException;


import javax.annotation.PostConstruct;

import javax.annotation.PreDestroy;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


/**

 * Servlet implementation class LifeCycleEx

 */

@WebServlet("/LC") 어노테이션을 LC로 사용

public class LifeCycleEx extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public LifeCycleEx() {

        super();

        // TODO Auto-generated constructor stub

    }


// @Override

// public void service(ServletRequest arg0, ServletResponse arg1)

// throws ServletException, IOException {

// // TODO Auto-generated method stub

// System.out.println("service");

// }

    

    @Override

    public void init() throws ServletException { 최초호출시

     // TODO Auto-generated method stub

     System.out.println("init");

    }


@Override

public void destroy()  { 호출해제시

// TODO Auto-generated method stub

System.out.println("destroy");

}



/**

 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

 */             

                      웹클라이언트가 직접 들어가므로 doget메소드  sysout이 출력된다.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

요청시 객체 생성

// TODO Auto-generated method stub

System.out.println("doGet"); 

}


/**

 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

 */

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

                           요청시객체 생성

// TODO Auto-generated method stub

System.out.println("doPost");

}

@PostConstruct  init메소드 실행 전에 이 메소드가 실행된다. 

private void initPostConstruct() {

// TODO Auto-generated method stub

System.out.println("initPostConstruct");

}

@PreDestroy   destroy 실행 이후 메소드 실행

private void destoryPreDestory() {

// TODO Auto-generated method stub

System.out.println("destoryPreDestory");

}


}




출력 결과


정보: Server startup in 1424 ms

initPostConstrust

init

doGet

doGet

...


9월 26, 2017 11:36:44 오후 org.apache.catalina.core.ApplicationContext log

정보: ContextListener: contextDestroyed()

destroy

destroyPreDestory

9월 26, 2017 11:36:44 오후 org.