서블릿은 웹 브라우저의 요청을 처리하기 위한 java로 구현한 API 이다.

서블릿의 생명주기.

서블릿 클래스가 생성될 때 재정의한 메서드들의 생명주기.

init(): 해당클래스가 생성 될 때 최초 한번 실행 된다.

doGet(), doPost(): 웹 브라우저 요청시 수시로 실행된다.

destory(): 객체가 소멸될 때 실행된다.

Servlet 구현.

1. Servlet-api 설정하기.


2. Servlet API 구현.

package sec01.ex01;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FirstServlet extends HttpServlet {

    @Override
    public void init() throws ServletException {
        System.out.println("init 메서드 호출");
    }        

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("doGet 메서드 호출");
    }

    @Override
    public void destroy() {
        System.out.println("destory 메서드 호출");
    }
}

3. 웹 브라우저 요청 시 서블릿 맵핑하기 - web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
    <servlet>
        <servlet-name>aaa</servlet-name>
        <servlet-class>sec01.ex01.FirstServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>aaa</servlet-name>
        <url-pattern>/first</url-pattern>
    </servlet-mapping>
</web-app>
  • 브라우저에서 요청이 오면 해당 클래스로 맵핑.


4. 서블릿 추가하기.

package sec01.ex01;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SecondServlet extends HttpServlet{

    @Override
    public void init() throws ServletException {
        System.out.println("init 메서드 호출");
    }        

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("doGet 메서드 호출");
    }

    @Override
    public void destroy() {
        System.out.println("destory 메서드 호출");
    }


}
  • 같은 기능의 두번 째 서블릿 클래스를 추가하였다.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
    <servlet>
        <servlet-name>aaa</servlet-name>
        <servlet-class>sec01.ex01.FirstServlet</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>bbb</servlet-name>
        <servlet-class>sec01.ex01.SecondServlet</servlet-class>
    </servlet>    
    <servlet-mapping>
        <servlet-name>aaa</servlet-name>
        <url-pattern>/first</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>aaa</servlet-name>
        <url-pattern>/Second</url-pattern>
    </servlet-mapping>    
</web-app>
  • 새로운 클래스로 서블릿 맵핑 추가.


@어노테이션으로 서블릿 맵핑하기.

web.xml 에 서블릿을 맵핑하는 것은 가독성도 안좋고 불편하다.
톰캣 7이상부터는 어노테이션을 이용한 서블릿 맵핑을 지원한다. 거의 대부분 이렇게 사용한다.


새로운 서블릿 클래스 추가.


어노테이션이 적용된 서블릿 클래스가 생성됨.

package sec01.ex01;

import java.io.IOException;

import javax.servlet.ServletConfig;
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 ThirdServlet
 */
@WebServlet("/Third")
public class ThirdServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see Servlet#init(ServletConfig)
     */
    public void init(ServletConfig config) throws ServletException {
        System.out.println("ThirdServlet init 호출");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // TODO Auto-generated method stub
        System.out.println("ThirdServlet doGet");
    }

    /**
     * @see Servlet#destroy()
     */
    public void destroy() {
        System.out.println("ThirdServlet destory 호출");
    }

}

출처: 자바 웹을 다루는 기술.

'프로그래밍 > 자바 웹 프로그래밍' 카테고리의 다른 글

서블릿과 DB 연동  (2) 2019.12.19
서블릿 Response객체  (0) 2019.12.15
서블릿 기초 실습  (0) 2019.12.14
웹 애플리케이션 구동하기  (0) 2019.12.11
프로그래밍의 발전과정  (0) 2019.12.10
블로그 이미지

파니동

,