package lesson03.servlets;
import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class HelloWorld implements Servlet {
ServletConfig config;
@Override
public void destroy() {
System.out.println("destroy() 호출됨");
}
@Override
public ServletConfig getServletConfig() {
System.out.println("getServletConfig() 호출됨");
return this.config;
}
@Override
public String getServletInfo() {
System.out.println("getServletInfo() 호출됨");
return null;
}
@Override
public void init(ServletConfig config) throws ServletException {
System.out.println("init() 호출됨");
this.config = config;
}
@Override
public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
System.out.println("service() 호출됨");
}
}
서블릿의 생명주기와 관련된 메서드
init(): 서블릿 컨테이너가 서블릿 생성 후 초기화 작업을 수행하기 위해 호출됨.
service(): 클라이언트가 요청할 때 마다 호출됨.
destroy(): 서블릿 컨테이너의 종료, 웹 어플리케이션의 정지, 해당 서블릿을 비활성화 시킬 때 호출됨.
서블릿의 기타 메서드
getServletConfig(): 서블릿 설정 정보를 다루는 ServletConfig 객체를 반환.
getServletInfo(): 서블릿을 작성한 사람에 대한 정보, 서블릿 버전, 권리 등을 담은 문자열을 반환.
<?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">
<display-name>web03</display-name>
<!-- 서블릿 선언 -->
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>lesson03.servlets.HelloWorld</servlet-class>
</servlet>
<!-- 서블릿을 URL과 연결 -->
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/Hello</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
