본문 바로가기

Developer/Java-was

#005. HttpServlet Class 실습을 통해 알아보기...

#005. HttpServlet Class 실습을 통해 알아보기...

 

1. 이전시간에 우리는 지루한 이론 Servlet에 대해서 알아봤다. 그러면 이번 시간엔 실습을 통해서 알아보자. 실습을 위해 hello package 를 만들고, 그 안에 HelloServlet class 를 만들어 보자.

1) hello package 만든다.

 

2) HelloServlet class 만든다.

코드

package hello;

import java.io.IOException;

 

import javax.servlet.ServletException;

import javax.servlet.http.*;

 

public class HelloServlet extends HttpServlet {

    @Override

    protected void doGet(HttpServletRequest req, HttpServletResponse resp)

            throws ServletException, IOException {

        System.out.println("doGet() is Running...");

    }

}

설명

문장7: HttpServlet 로부터 상속을 받는다.

문장9~12: HttpServlet 의 메서드중 doGet() 메서드 재정의 한다.

문장11: 실행이 되는지 확인을 위해 콘솔로 내용을 입력한다.

 

2. Servlet 을 실행하기 위해서는 web.xml 파일에 매핑을 시켜줘야 한다.

1) web.xml 파일을 더블클릭 하면 아래와 같이 이미지가 표시된다.

 

2) Source 탭을 선택 후 블럭 지정된 내용을 지우고 아래와 같이 입력한다.

  

코드

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

    <servlet>

        <servlet-name>kyumboy</servlet-name>

        <servlet-class>hello.HelloServlet</servlet-class>

    </servlet>

    

    <servlet-mapping>

        <servlet-name>kyumboy</servlet-name>

        <url-pattern>/hello.dh</url-pattern>

    </servlet-mapping>

</web-app>

설명

첫 번째 그림의 박스안의 내용을 모두 지우고 위 코드와 같이 입력을 한다.

두 번째 그림은 /hello.dh 주소를 입력하게 되면 바로 위 kyumboy 이름을 가진 servlet 을 찾아가게 되고, hello.HelloServlet 클래스를 실행하게 된다.

아래 Servers 탭에 있는 벌레모양의 아이콘을 누르게 되면, tomcat 서버가 재시작 하게 된다.

 

3. 설정이 완료 되었다. Internet Explorer 에서 아래와 같이 주소를 입력해보자.

1) http://localhost:8080/web/hello.dh

아래 이미지는 소스 보기를 한 화면이다.

 

4. Setvlet 의 Life Cycle (생명주기)를 알아보자..

1) GenericServlet 에서 상속받은 init(), destroy() 와 HelloServlet() 생성자를 구현 해보자...

코드

package hello;

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.http.*;

public class HelloServlet extends HttpServlet {

    @Override

    public void init() throws ServletException {

        System.out.println("init() is Running...");

    }

    @Override

    public void destroy() {

        System.out.println("destroy() is Running...");

    }

    public HelloServlet() {

        System.out.println("helloServlet 객체 생성...");

    }

    @Override

    protected void doGet(HttpServletRequest req, HttpServletResponse resp)

            throws ServletException, IOException {

        System.out.println("doGet() is Running...");

    }

}

설명

생명주기를 알아보기 위해, GenericServlet 의 init(), destroy() 재정의와 생성자 HelloServlet() 을 구현해본 내용이다.

 

2) HelloServlet Class 를 변경 후 저장하면, 자동으로 tomcat server 가 재시작 하는 것을 알 수 있다.

 

3) Internet Explorer 에서 아래의 주소로 접속을 하면, 아래 이미지와 같이 결과가 표시가 된다.

http://localhost:8080/web/hello.dh

결과

helloServlet 객체 생성...

init() is Running...

doGet() is Running...

설명

웹 페이지에 접속을 하면, servlet 객체가 로딩이 되고, init() 초기화가 되고, doGet()을 호출하는 것을 확인 할 수 있다.

 

4) Internet Explorer 에서 다시 아래의 주소로 접속을 하면, 아래 이미지와 같이 결과가 표시가 된다.

http://localhost:8080/web/hello.dh

결과

helloServlet 객체 생성...

init() is Running...

doGet() is Running...

doGet() is Running...

설명

웹 페이지를 새로고침 하면, 객체 생성과 초기화가 되지 않는 것을 알 수 있다.

 

5) HelloServlet Class 의 내용을 변경 후 저장해보자.

결과

helloServlet 객체 생성...

init() is Running...

doGet() is Running...

doGet() is Running...

2011. 4. 25 오후 11:15:44 org.apache.catalina.core.StandardContext reload

정보: Reloading Context with name [/web] has started

destroy() is Running...

설명

문장8: 내용을 수정하고 나서 저장을 하면, 자동으로 tomcat이 재시작 하고, 객체가 사라지면서 destroy() 메서드가 실행되는 것을 확인 할 수 있다.

 

6) Internet Explorer 에서 다시 아래의 주소로 접속을 하면, 아래 이미지와 같이 결과가 표시가 된다.

http://localhost:8080/web/hello.dh

결과

helloServlet 객체 생성...

init() is Running...

doGet() is Running...

doGet() is Running...

2011. 4. 25 오후 11:15:44 org.apache.catalina.core.StandardContext reload

정보: Reloading Context with name [/web] has started

destroy() is Running...

helloServlet 객체 생성...

init() is Running...!

doGet() is Running...

설명

객체가 소멸됐기 때문에 웹 페이지에 다시 접속할 때 객체가 다시 생성 되는 것을 확인 할 수 있다.

 

지금까지 Servlet 의 작동방법과 각 메서드의 생성주기에 대해서 알아봤다.

다음시간엔 doGet() 메서드에 대해 자세히 알아보자...

 

잡담. 와우! 이번 시간 강좌 준비는 무려 3시간이나 걸렸네요.. 오늘도 수고하셨습니다.^^"