[ servlet 초기화 파라미터, 컨텍스트 파라미터, 서블릿 컨텍스트 리스너]
1. 서블릿 초기화 파라미터
: 특정 서블릿에서만 사용될 수 있는 초기화 파라미터를 지정해서 가져다 사용할 수 있다.
2가지 방법)
1) web.xml의 servlet 태그 내에 정의하기
servlet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <?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>ex</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>ServletInitParam</servlet-name> <servlet- class >com.jsp.study.ServletConfigTest</servlet- class > <init-param> <param-name>id</param-name> <param-value>sooingkr</param-value> </init-param> <init-param> <param-name>pw</param-name> <param-value>abcd</param-value> </init-param> <init-param> <param-name>path</param-name> <param-value>c:\</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>ServletInitParam</servlet-name> <url-pattern>/ServletConfigTest</url-pattern> </servlet-mapping> </web-app> |
web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | package com.jsp.study; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletConfigTest extends HttpServlet { private static final long serialVersionUID = 1L; public ServletConfigTest() { super (); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println( "doGet" ); // [ 1. web.xml에 특정 servlet에 있는 init-param(서블릿 초기화 변수) 으로 설정한 값들 가져오기 ] String id = getInitParameter( "id" ); String pw = getInitParameter( "pw" ); String path = getInitParameter( "path" ); /////////////////////////////////////////////////////////////// response.setContentType( "text/html;charset=utf-8" ); System.out.println( "id : " + id + " pw : " + pw + " path : " + path); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } } |
2) 어노테이션으로 @WebServlet(initParams={@WebInitParam(name="",value="")} 방식으로 지정하기
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | package com.jsp.study; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebInitParam; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; // 2. [ 어노테이션을 이용한 초기화 파라미터를 지정하는 방법 ] : initParams = {@WebInitParam(name="",value="")} @WebServlet (urlPatterns={ "/ServletInitParamAnno" }, initParams={ @WebInitParam (name= "id" ,value= "aaaa" ) , @WebInitParam (name= "pw" ,value= "pwValue" )}) public class ServletInitParamAnno extends HttpServlet { private static final long serialVersionUID = 1L; public ServletInitParamAnno() { super (); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String id = getInitParameter( "id" ); String pw = getInitParameter( "pw" ); System.out.println( "id : " + id + " pw : " + pw); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } } |
2. 모든 Servlet에서 사용할 수 있는 초기화 변수 설정하기(컨텍스트 파라미터)
: web.xml에 context-param 태그 내에 선언한다.
서블릿에서 가져다 사용할 때는 getServletContext() 를 가져온 뒤 getInitParameter("")을 사용해 값을 꺼내온다.
web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <?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>ex</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 모든 서블릿에서 사용할 수 있는 컨텍스트 파라미터 지정 --> <context-param> <param-name>contextParamId</param-name> <param-value>contextId</param-value> </context-param> <context-param> <param-name>contextParamPw</param-name> <param-value>contextParamPw</param-value> </context-param> <servlet> <servlet-name>ServletInitParam</servlet-name> <servlet- class >com.jsp.study.ServletConfigTest</servlet- class > </servlet> <servlet-mapping> <servlet-name>ServletInitParam</servlet-name> <url-pattern>/ServletConfigTest</url-pattern> </servlet-mapping> </web-app> |
servlet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | package com.jsp.study; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletConfigTest extends HttpServlet { private static final long serialVersionUID = 1L; public ServletConfigTest() { super (); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println( "doGet" ); // [ 모든 서블릿에서 공통으로 접근할 수 있는 컨텍스트 파라미터 가져오기 ] String paramId = getServletContext().getInitParameter( "contextParamId" ); String paramPw = getServletContext().getInitParameter( "contextParamPw" ); System.out.println( "contextId : " + paramId + " contextPw : " + paramPw); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } } |
3. ServletContextListener 를 이용한 어플리케이션 시작과 종료 감시
: web.xml에 listener를 등록해 놓으면 어플리케이션 시작과 종료시에 contextDestroyed 메서드와
contextInitialized 메서드가 호출된다.
이는 각 서블릿의 init() 메서드보다 먼저 호출되는 것이며
최종적으로 destroy() 메서드보다 이후에 호출된다.
구현 방식은
ServletContextListener 인터페이스를 implement해서 클래스를 작성하고
해당 클래스를 web.xml에 리스너 태그 내에 등록한다.
servlet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package com.jsp.study; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class ServletListenerTest implements ServletContextListener { @Override public void contextDestroyed(ServletContextEvent paramServletContextEvent) { // 어플리케이션 실행이 종료될 때 호출된다. System.out.println( "contextDestroyed" ); // 서블릿의 destroy() 메서드가 호출된 이후에 최종적으로 어플리케이션 종료 때 호출됨. } @Override public void contextInitialized(ServletContextEvent paramServletContextEvent) { // 어플리케이션 실행될 때 호출된다. 즉, 각 서블릿의 init() 메서드가 호출되기 전에 호출됨 System.out.println( "contextInitialized" ); } } |
web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?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>ex</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <listener> <description>서블릿 리스너를 등록합니다. 어플리케이션의 시작과 종료를 감시함</description> <listener- class >com.jsp.study.ServletListenerTest</listener- class > </listener> </web-app> |
'스터디 > JSP' 카테고리의 다른 글
JSP 지시자(directive) : page지시자,include지시자,taglib 지시자 (0) | 2017.08.10 |
---|---|
JSP 스크립트 요소(스크립트릿,선언식,표현식) (0) | 2017.08.10 |
5. Servlet에서의 HTML Form태그, servlet Parameter, 한글처리 (0) | 2017.08.08 |
4. servlet의 라이프사이클(lifecycle) (0) | 2017.08.08 |
3. servlet의 doGet(), doPost()와 PrintWriter 객체 (0) | 2017.08.08 |