Event 모델
☞ Event : 컴포넌트에 가해진 동작, 변화
1. 컴포넌트에서 Event가 발생하는 것을 감시하는 Container
2. 발생한 Event를 처리하는 동작을 가진 객체(Listener Class)
- Event Handler : 발생한 Event 처리 동작(메소드)
- Event Source : Event가 발생한 컴포넌트
ex) 버튼을 클릭하면 안녕이라고 출력한다.
Event Source : 버튼
Event : 클릭
Event Handler : 안녕을 출력하는 동작
MyServletContextListener.java
더보기 접기
package servlet.listener;
import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener;
public class MyServletContextListener implements ServletContextListener{ //Web Application이 시작시 Web Container에 의해 호출됨 public void contextInitialized(ServletContextEvent sce){ System.out.println("--------------------------------"); System.out.println("MyServletContextListener.contextInitialized실행"); System.out.println("--------------------------------"); } //Web Application이 종료될때 Web container에 의해 호출됨 public void contextDestroyed(ServletContextEvent sce){ System.out.println("--------------------------------"); System.out.println("MyServletContextListener.contextDestroyed 실행"); System.out.println("--------------------------------"); }
}
접기
DriverLoadingListener.java
더보기 접기
package servlet.listener;
import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener;
public class DriverLoadingListener implements ServletContextListener { public DriverLoadingListener() { } public void contextInitialized(ServletContextEvent sce) { // DriverLoading // ServletContext 조회 ServletContext ctx = sce.getServletContext(); String driverClass = ctx.getInitParameter("driver class"); System.out.println("driverClass : " + driverClass);
try { Class.forName(driverClass); System.out.println("------------driverLoading 성공----------------"); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public void contextDestroyed(ServletContextEvent arg0) { } }
접기
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 " 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"> <listener> <listener-class>servlet.listener.MyServletContextListener</listener-class> </listener> <listener> <listener-class>servlet.listener.DriverLoadingListener</listener-class> </listener> <context-param> <param-name>driver class</param-name> <param-value>oracle.jdbc.driver.OracleDriver</param-value> </context-param> </web-app>
접기
Attribute(속성)
☞ Attribute란?
- Web Application 구성 컴포넌트들(Servlet, JSP, Listener)에 공유하는 객체
☞ Scope
- Attribute들을 공유하기 위한 공유 장소의 영역(저장 장소)
- 공유 범위에 따라 3가지 영역이 있다.
1. request scope : HttpServletRequest 이용
- 요청 ~ 응답까지 공유
2. session scope : HttpSession 이용
- 한명의 클라이언트(웹브라우저)가 로그인 ~ 로그아웃까지 공유
3. application scope : SevletContext 이용
- Application 시작 ~ 종료까지 공유
☞ 관련 메소드
- Attribute는 key-value 쌍으로 관리된다.
- setAttribute(String key, Object value) : 공유영역에 Attribute 저장
- getAttribute(String key, Object value) : 저장된 Attribute 조회
- getAttributeNames() : Enumeration : Attribute들에 연결된 name들 조회
GetAttributeServlet.java
더보기 접기
package servlet.attribute;
import java.io.IOException; import java.io.PrintWriter;
import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
public class SetAttributeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //Application Scope에 greeting -안녕하세요 설정 //Application Scope - SevletContext //Attribute : 안녕하세요 - String ServletContext ctx = getServletContext(); ctx.setAttribute("greeting", "안녕하세요"); ctx.setAttribute("name", "홍길동"); ctx.setAttribute("age", 30); // 30: Integer PrintWriter out = response.getWriter(); out.println("<html><body>"); out.println("<a href='/myweb/get_attribute'>Get Attribute</a>"); out.println("</body></html>"); }
}
접기
SetAttributeServlet.java
더보기 접기
package servlet.attribute;
import java.io.IOException; import java.io.PrintWriter;
import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
public class GetAttributeServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //Application Scope에 설정된(binding) Attribute를 조회하여 출력 ServletContext ctx = getServletContext(); //lookup String name = (String)ctx.getAttribute("name"); String greeting = (String)ctx.getAttribute("greeting"); Integer age = (Integer)ctx.getAttribute("age"); response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); out.println("<html><body>"); out.println("인사말 : " + greeting +"<br>"); out.println("이름 : " + name + "<br>"); out.println("나이 : " + age + "<br>"); out.println("</body></html>"); }
}
접기
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 " 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> <description></description> <display-name>SetAttributeServlet</display-name> <servlet-name>SetAttributeServlet</servlet-name> <servlet-class>servlet.attribute.SetAttributeServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>SetAttributeServlet</servlet-name> <url-pattern>/set_attribute</url-pattern> </servlet-mapping> <servlet> <description></description> <display-name>GetAttributeServlet</display-name> <servlet-name>GetAttributeServlet</servlet-name> <servlet-class>servlet.attribute.GetAttributeServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>GetAttributeServlet</servlet-name> <url-pattern>/get_attribute</url-pattern> </servlet-mapping> </web-app>
접기
SetAttributeServlet을 통해 name, greeting, age의 값을 미리 지정해주고 그 값을 getServletContext()를 통해서 같은 Web Application 내에 존재하는 LifecycleServlet을 통해 name, greeting, age의 value를 조회 할 수 있다.
SetAttributeServlet 에서 설정한 name, greeting, age 값을 LifecycleServlet.java에서 조회
더보기 접기
package servlet.lifecycle;
import javax.servlet.ServletContext; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
public class LifecycleServlet extends HttpServlet{ public LifecycleServlet() { System.out.println("LifecycleServlet 객체생성"); } //init() : 객체 생성 직후 public void init() { System.out.println("LifecycleServlet.init() 메소드 실행"); } //destroy() : 객체 소멸 직전 public void destroy() { System.out.println("LifecycleServlet.destroy() 메소드 실행"); } //service() -> doGet(): 클라이언트 요청이 들어올 때 마다 호출 public void doGet(HttpServletRequest request, HttpServletResponse response) { ServletContext ctx = getServletContext(); String name = (String)ctx.getAttribute("name"); String greeting = (String)ctx.getAttribute("greeting"); Integer age = (Integer)ctx.getAttribute("age"); System.out.println(name + " , " + greeting + " , " + age); } }
접기