본문 바로가기
Java Web/JSP

[JSP] web.xml 파일 & JSP 내장 객체 2 (config, application)

by 스응 2023. 3. 27.
728x90
728x90

web.xml 파일

  - Java 웹 애플리케이션에서 사용하는 배치 지시자 파일

  - 웹 애플리케이션이 서버에 배포되는 시점의 설정 정보를 담고 있는 XML 파일

  - 기본 설정을 하나의 프로젝트마다 재정의할 수 있음

  - 웹 서버가 시작될 때 단 한 번만 로딩됨

      → 서버가 시작된 후에는 수정해도 수정 사항이 적용되지 않음

      → 설정을 변경하려면 해당 애플리케이션을 다시 배포해야 함

 

  ▶ welcome-file

      : 처음 서버에 접속했을 때 표시하는 파일

      · 순서대로 확인해서 가장 처음 발견된 파일을 표시함

     · 파일 경로를 포함하지 않고 root context(프로젝트명)까지만 작성해도 그 파일로 들어가짐

 

1
2
3
4
5
6
7
8
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.jsp</welcome-file>
    <welcome-file>default.htm</welcome-file>
  </welcome-file-list>
cs

 

 


config 객체

  - 현재 JSP 페이지의 환경 정보(서블릿 설정 정보 등)를 다루는 객체

  - ServletConfig 객체

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!-- web.xml 파일 -->
 
    
 <servlet>
       <servlet-name>myServlet</servlet-name> <!-- 임의로 지정해주는 이름 -->
       <servlet-class>com.tenco.MyServlet</servlet-class>
     <!-- 초기 파라미터 설정하기 -->
     <init-param>
         <param-name>adminId</param-name> <!-- key -->
         <param-value>tenco</param-value> <!-- value -->
     </init-param>
     <init-param>
         <param-name>adminPw</param-name>
         <param-value>asd123</param-value>
     </init-param>
 </servlet>
 
cs

 

1
2
3
4
// Servlet 파일에서 초기 파라미터 값 가져오기
 
String adminId = getServletConfig().getInitParameter("adminId"); // 매개변수로 key
String adminPw = getServletConfig().getInitParameter("adminPw");
cs

 

 

반응형
728x90

application 객체

  - 웹 애플리케이션 정보를 다루는 객체

  - 웹 서버 내에 동일 애플리케이션 정보를 저장 및 처리함

  - 주로 전체 서블릿 객체에서 공유할 필요가 있는 값들을 설정할 수 있음

  - ServletContext

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!-- web.xml 파일 -->
 
    
 <!-- 서블릿 전체에서 공유할 수 있는 데이터를 선언 -->
 <context-param>
     <param-name>imgDir</param-name> <!-- key -->
     <param-value>/upload/img</param-value> <!-- value -->
 </context-param>
 
 <context-param>
     <param-name>testServerIp</param-name>
     <param-value>127.0.0.1</param-value>
 </context-param>
 
 <context-param>
     <param-name>realServerIp</param-name>
     <param-value>88.0.13.1</param-value>
 </context-param>
cs

 

1
2
3
4
5
// Servlet 파일 
 
String imgDir = getServletContext().getInitParameter("imgDir");
String testServerIp = getServletContext().getInitParameter("testServerIp");
String realServerIp = getServletContext().getInitParameter("realServerIp");
cs

 

1
2
3
4
5
6
7
8
9
10
11
12
<!-- jsp 파일 -->
 
<%    
    // 멤버변수와 같은 개념이라서 null로 자동 초기화됨
    String imgDir;
    String testServerIp;
    String realServerIp;
    
    imgDir = application.getInitParameter("imgDir");
    testServerIp = application.getInitParameter("testServerIp");
    realServerIp = application.getInitParameter("realServerIp");    
%>
cs

 

 


기타 메모

- WEB-INF : 보안 상의 이유로 외부에서 바로 접근할 수 없는 폴더 (URI 방식으로 접근 불가)
    → WEB-INF 내에 있는 jsp 파일은 서블릿으로 접근시켜야 함
320x100
반응형

'Java Web > JSP' 카테고리의 다른 글

[JSP] 쿠키와 세션  (0) 2023.03.27
[JSP] JSP 내장 객체 3 (exception) & 에러 페이지  (0) 2023.03.27
[JSP] JSP 스크립트  (0) 2023.03.23
[JSP] JSP 내장 객체 1 (request, response, session, out)  (0) 2023.03.22
[JSP] URL Mapping  (0) 2023.03.22

댓글