import java.io.IOException;
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 T07_ServletContextTest extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext ctx = req.getServletContext();
System.out.println("서블릿 컨텍스트의 기본경로 : "+**ctx.getContextPath()**);
System.out.println("서블릿 컨텍스트의 기본경로 : "+req.getContextPath()); //같은거임
System.out.println("서버정보 : "+ctx.getServerInfo());
System.out.println("서블릿 API의 메이저버전 정보 : "+ctx.getMajorVersion());
System.out.println("서블릿 API의 마이너버전 정보 : "+ctx.getMinorVersion());
System.out.println("배포기술자에 기술된 컨텍스트명 : "+ctx.getContextPath());
System.out.println("리소스 경로목록 : "+ctx.getServletContextName());
System.out.println("모든 등록된 서블릿 목록 맵 : "+ctx.getServletRegistrations().toString());
System.out.println("파일에 대한 MIME타입 정보 : "+ctx.getMimeType("a.mp3"));
System.out.println("파일시스템상의 실제경로 : "+ctx.getRealPath("/"));
// 로깅(로그파일)
ctx.log("서블릿 컨텍스트를 이용한 로깅작업중입니다");
// 속성값 설정
ctx.setAttribute("attr1", "속성1");
// 속성값 변경
ctx.setAttribute("attr1", "속성2");
//속성값 가져오기
System.out.println("attr1 : " +ctx.getAttribute("attr1"));
// 속성값 지우기
ctx.removeAttribute("attr1");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
<servlet>
<servlet-name>T07</servlet-name>
<servlet-class>kr.or.ddit.basic.T07_ServletContextTest</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>T07</servlet-name>
<url-pattern>/T07</url-pattern>
</servlet-mapping>
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class MyHttpSessionListener implements HttpSessionAttributeListener,HttpSessionListener {
@Override
public void attributeAdded(HttpSessionBindingEvent event) {
System.out.println("[MyHttpSessionListener]" + event.getName() + "추가됨");
}
@Override
public void attributeRemoved(HttpSessionBindingEvent event) {
System.out.println("[MyHttpSessionListener]" + event.getName() + "삭제됨");
}
@Override
public void attributeReplaced(HttpSessionBindingEvent event) {
System.out.println("[MyHttpSessionListener]" + event.getName() + "대체됨");
}
@Override
public void sessionCreated(HttpSessionEvent se) {
System.out.println("[MyHttpSessionListener]" + "세션이 생성됨. 세션ID => "+se.getSession().getId());
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
System.out.println("[MyHttpSessionListener]" + "세션이 소멸됨. 세션ID => "+se.getSession().getId());
}
}
<listener>
<listener-class>kr.or.ddit.basic.MyHttpSessionListener</listener-class>
</listener>
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class T_09SessionListnerTest extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// HttpSession 객체 생성 및 소멸
HttpSession session = req.getSession(); //true가 디폴트
//session.invalidate(); // 세션 삭제
session.setAttribute("ATTR1", "속성1");
session.setAttribute("ATTR1", "속성11");
session.setAttribute("ATTR2", "속성2");
session.removeAttribute("ATTR1");
session.invalidate(); // 세션삭제
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
<servlet>
<servlet-name>T09</servlet-name>
<servlet-class>kr.or.ddit.basic.T_09SessionListnerTest</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>T09</servlet-name>
<url-pattern>/T09</url-pattern>
</servlet-mapping>
http://localhost/ServletTest/T09 접속
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class MyServletContextListener implements ServletContextListener, ServletContextAttributeListener{
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("[MyServletContextListener] 소멸자 호출됨.");
}
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("[MyServletContextListener] 생성자 호출됨.");
}
@Override
public void attributeAdded(ServletContextAttributeEvent event) {
System.out.println("[MyServletContextListener] 속성 추가됨. => " + event.getName());
}
@Override
public void attributeRemoved(ServletContextAttributeEvent event) {
System.out.println("[MyServletContextListener] 속성 삭제됨. => "+event.getName());
}
@Override
public void attributeReplaced(ServletContextAttributeEvent event) {
System.out.println("[MyServletContextListener] 속성 변경됨. => "+event.getName());
}
}
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 T10_ServletContextListenerTest extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getServletContext().setAttribute("ATTR1", "속성1"); //ATTR1 속성을 추가
req.getServletContext().setAttribute("ATTR1", "속성11"); //ATTR1 속성을 변경
req.getServletContext().setAttribute("ATTR2", "속성2"); //ATTR2 속성을 추가
this.getServletContext().removeAttribute("ATTR1"); //ATTR1속성을 삭제
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
<listener>
<listener-class>kr.or.ddit.basic.MyServletContextListener </listener-class>
</listener>
<servlet>
<servlet-name>T10</servlet-name>
<servlet-class>kr.or.ddit.basic.T10_ServletContextListenerTest</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>T10</servlet-name>
<url-pattern>/T10</url-pattern>
</servlet-mapping>
서버 시작시 이런 콘솔이 찍히는데, 서블릿 콘텍스트가 만들어졌다는것! 이제 갖다 쓰면돼
http://localhost/ServletTest/T10 << 접속하면
서버 종료시키면
톰캣 중지시 어플리케이션 종료..... 호출