웹 프로그래밍 03(Servlet)-파라미터 초기화 방법 3가지
*파라미터 초기화 방법 3가지
-첫번째 서블릿 파라미터
web.xml -> 초기화 파라미터 정의
java소스 -> 메소드를 이용하여 web.xml 에서 정의한 데이터 불러오기
-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 34 35 36 |
<?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>jsp_8_1_ex1_initparamex</display-name> <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>
<servlet> <servlet-name>ServletInitParam</servlet-name> <servlet-class>com.javalec.ex.ServletInitParam</servlet-class>
<init-param> <param-name>id</param-name> <param-value>abcdef</param-value> </init-param> <init-param> <param-name>pw</param-name> <param-value>1234</param-value> </init-param> <init-param> <param-name>path</param-name> <param-value>C:\\javalec\\workspace</param-value> </init-param>
</servlet> <servlet-mapping> <servlet-name>ServletInitParam</servlet-name> <url-pattern>/InitParam</url-pattern> </servlet-mapping>
</web-app> |
-java 소스 중 doGet() 부분
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub System.out.println("doGet");
String id = getInitParameter("id"); String pw = getInitParameter("pw"); String path = getInitParameter("path");
response.setContentType("text/html; charset=EUC-KR"); PrintWriter writer = response.getWriter(); writer.println("<html><head></head><body>"); writer.println("아이디 : " + id + "<br />"); writer.println("비밀번호 : " + pw + "<br />"); writer.println("path : " + path); writer.println("</body></html>");
writer.close(); } |
-출력결과
-두번째 자바소스 자체에 초기화 파라미터
java소스 -> 매핑부분에 초기화 파라미터까지 같이 기술
-기존 매핑 방식
1 |
@WebServlet("/listener") |
-기존 매핑 방식 + 초기화 파라미터 추가
1 |
@WebServlet(urlPatterns={"/listener"}, initParams={@WebInitParam(name="id", value="abcdef"), @WebInitParam(name="pw", value="1234"), @WebInitParam(name="path", value="C:\\javalec\\workspace")}) |
-java 소스 중 doGet() 부분
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub System.out.println("doGet");
String id = getInitParameter("id"); String pw = getInitParameter("pw"); String path = getInitParameter("path");
response.setContentType("text/html; charset=EUC-KR"); PrintWriter writer = response.getWriter(); writer.println("<html><head></head><body>"); writer.println("아이디 : " + id + "<br />"); writer.println("비밀번호 : " + pw + "<br />"); writer.println("path : " + path); writer.println("</body></html>");
writer.close(); } |
-출력결과
-세번째 서로 다른 서버끼리 공유하기 위해 초기화 파라미터
servlet 플랫폼말고 또 다른 서버와 공유가 필요할경우
Context를 사용한다.
-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 |
<?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>jsp_8_2_ex1_contextparamex</display-name> <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>
<context-param> <param-name>id</param-name> <param-value>abcdef</param-value> </context-param> <context-param> <param-name>pw</param-name> <param-value>1234</param-value> </context-param> <context-param> <param-name>path</param-name> <param-value>C:\javalec\workspace</param-value> </context-param>
</web-app> |
-java 소스 중 doGet() 부분
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub System.out.println("doGet");
String id = getServletContext().getInitParameter("id"); String pw = getServletContext().getInitParameter("pw"); String path = getServletContext().getInitParameter("path");
response.setContentType("text/html; charset=EUC-KR"); PrintWriter writer = response.getWriter();
writer.println("<html><head></head><body>"); writer.println("아이디 : " + id + "<br />"); writer.println("비밀번호 : " + pw + "<br />"); writer.println("path : " + path); writer.println("</body></html>");
writer.close(); } |
-출력결과
*웹어플리 케이션 감지 ServletContextListener 사용하기
-서버가 실행되고 종료되는 이벤트 감지
-java 소스(class) 새로 생성
Interfaces는 ServletContextListener 추가
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.servlet.ex;
import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener;
@WebListener public class listener implements ServletContextListener {
@Override public void contextDestroyed(ServletContextEvent arg0) { // TODO Auto-generated method stub System.out.println("contextDestroyed"); }
@Override public void contextInitialized(ServletContextEvent arg0) { // TODO Auto-generated method stub System.out.println("contextInitialized"); }
} |
-웹 프로그래밍 시작_2에서 했던 init,destroy,initPostConstruct, destroyPreDestroy 메소드를
기존 java소스에 추가
-누가 가장먼저 실행되고 중단되는지 확인
-결과 순서
=>누가 먼저 시작 되는지 확인
1번 contextInitialized(Listener)
2번 initPostConstruct
3번 init
=>누가 먼저 종료 되는지 확인
1번 destroy
2번 destroyPreDestroy
3번 contextDestroyed(Listener)