나의 생각을 끄적이는 공간

블로그 이미지

Wooni0477

카테고리

  • 분류 전체보기 (118)
    • 프로그래밍 정리 (102)
      • Front (5)
      • Java (2)
      • JSP, Servlet, DB(oracle) (33)
      • JavaScript (0)
      • Spring (22)
      • Oracle (28)
      • Vue (1)
      • API (1)
      • err (5)
      • ERD (1)
      • etc.. (4)
    • BoostCourse (15)
      • HTML/CSS (7)
      • JavaScript (4)
      • JSP (4)
    • 공부 자료들.. (0)
    • 기타 (0)
    • --------------------------- (0)
    • 끄적이는공간.. (0)
      • 이벤트 (0)
      • 여행 (0)

    최근...

  • 포스트
  • 댓글
  • 트랙백
  • 더 보기

웹 프로그래밍 03(Servlet)-파라미터 초기화 방법 3가지

프로그래밍 정리/JSP, Servlet, DB(oracle) 2020. 1. 2. 18:17
반응형

*파라미터 초기화 방법 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>

Colored by Color Scripter

-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();

}

Colored by Color Scripter

-출력결과

-두번째 자바소스 자체에 초기화 파라미터

​

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();

}

Colored by Color Scripter

-출력결과

-세번째 서로 다른 서버끼리 공유하기 위해 초기화 파라미터

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>

Colored by Color Scripter

-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();

}

Colored by Color Scripter

-출력결과

​

*웹어플리 케이션 감지 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");

}

}

Colored by Color Scripter

-웹 프로그래밍 시작_2에서 했던 init,destroy,initPostConstruct, destroyPreDestroy 메소드를

기존 java소스에 추가

​

-누가 가장먼저 실행되고 중단되는지 확인

-결과 순서

=>누가 먼저 시작 되는지 확인

1번 contextInitialized(Listener)

2번 initPostConstruct

3번 init

​

=>누가 먼저 종료 되는지 확인

1번 destroy

2번 destroyPreDestroy

3번 contextDestroyed(Listener)

반응형
저작자표시 비영리 변경금지 (새창열림)

'프로그래밍 정리 > JSP, Servlet, DB(oracle)' 카테고리의 다른 글

웹 프로그래밍 06(JSP)-요청과 응답  (0) 2020.01.02
웹 프로그래밍 05(JSP)-기본예제  (0) 2020.01.02
웹 프로그래밍 04(JSP)-표현식과 내부객체  (0) 2020.01.02
웹 프로그래밍 02(Servlet)-라이프사이클, html(form,input)  (0) 2020.01.02
웹 프로그래밍 시작 01  (0) 2020.01.02
Posted by Wooni0477
방명록 : 관리자 : 글쓰기
Wooni0477's Blog is powered by daumkakao
Skin info material T Mark3 by 뭐하라
favicon

나의 생각을 끄적이는 공간

  • 태그
  • 링크 추가
  • 방명록

관리자 메뉴

  • 관리자 모드
  • 글쓰기
  • 분류 전체보기 (118)
    • 프로그래밍 정리 (102)
      • Front (5)
      • Java (2)
      • JSP, Servlet, DB(oracle) (33)
      • JavaScript (0)
      • Spring (22)
      • Oracle (28)
      • Vue (1)
      • API (1)
      • err (5)
      • ERD (1)
      • etc.. (4)
    • BoostCourse (15)
      • HTML/CSS (7)
      • JavaScript (4)
      • JSP (4)
    • 공부 자료들.. (0)
    • 기타 (0)
    • --------------------------- (0)
    • 끄적이는공간.. (0)
      • 이벤트 (0)
      • 여행 (0)

카테고리

PC화면 보기 티스토리 Daum

티스토리툴바