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

2023. 3. 27. 09:41·Java/JSP
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>
 
Colored by Color Scripter
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>
Colored by Color Scripter
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");    
%>
Colored by Color Scripter
cs

 

 


기타 메모

- WEB-INF : 보안 상의 이유로 외부에서 바로 접근할 수 없는 폴더 (URI 방식으로 접근 불가)
    → WEB-INF 내에 있는 jsp 파일은 서블릿으로 접근시켜야 함
320x100
반응형
저작자표시 비영리 변경금지 (새창열림)

'Java > 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
'Java/JSP' 카테고리의 다른 글
  • [JSP] 쿠키와 세션
  • [JSP] JSP 내장 객체 3 (exception) & 에러 페이지
  • [JSP] JSP 스크립트
  • [JSP] JSP 내장 객체 1 (request, response, session, out)
스응
스응
    반응형
    250x250
  • 스응
    이서영의 개발 블로그
    스응
  • 전체
    오늘
    어제
  • 글쓰기 관리
    • 분류 전체보기 (385)
      • Java (134)
        • Base (54)
        • Spring Boot (37)
        • JSP (16)
        • Swing (GUI) (20)
        • Design Pattern (7)
      • C# (13)
      • PHP (18)
      • SQL (27)
      • Vue.js (9)
      • Tailwind CSS (4)
      • TypeScript (7)
      • HTML & CSS (27)
      • JavaScript (26)
      • jQuery (10)
      • Android (3)
      • - - - - - - - - - - - - - - (0)
      • Hotkeys (5)
      • CS (30)
      • IT Notes (13)
      • Error Notes (17)
      • Team Project (24)
        • Airlines Web Project (12)
        • University Web Project (6)
        • Strikers 1945 GUI Project (6)
      • My Project (18)
        • Library Web Project (8)
        • Pet Shopping Mall GUI Project (10)
      • etc. (0)
  • 블로그 메뉴

    • Home
    • Write
  • 링크

    • 깃허브
  • 공지사항

  • 인기 글

  • 태그

    오블완
    Swing
    tailwindcss
    면접
    http
    js
    vuejs
    java
    HTML
    SWAGGER
    git
    jQuery
    C#
    jsp
    Android
    개발일지
    Hotkeys
    SpringBoot
    Codeigniter
    SEO
    cs
    CSS
    php
    errorNote
    Wordpress
    티스토리챌린지
    SQL
    zapier
    typeScript
  • 최근 댓글

  • hELLO· Designed By정상우.v4.10.0
스응
[JSP] web.xml 파일 & JSP 내장 객체 2 (config, application)
상단으로

티스토리툴바