lesson05_JSP_Servlet_DataShare.zip
0.01MB
Servlet이 생성될 때, 초기에 필요한 데이터 혹은 어디에서나 사용할 데이터가 있다면, web.xml 에서 설정해 줄 수 있습니다.
예를 들어 계정 정보 및 특정 경로를 넣을 수 있습니다.
Dynamic Project 를 생성한 후, WebContent/WEB-INF/web.xml에서 환경 설정 할 수 있습니다.
특정 Servlet 안에 데이터 초기화 방법
1. web.xml 파일 안에 <servlet></servlet>을 선언 후, servlet 파일이 있다면 servlet-class 안에 경로를 입력하며, jsp 파일이 servlet의 역활을 한다면 jsp-file 안에 경로를 입력해 줍니다.
web.xml 설정 패턴
<servlet>
<servlet-name>Servlet닉네임</servlet-name>
<servlet-class>특정 Servlet 경로를 입력</servlet-class>
<init-param>
<param-name>Servlet 호출시 파라메터 호출시, 명칭</param-name>
<param-value>위 명칭의 값</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Servlet닉네임</servlet-name>
<url-pattern>URL창에 입력시 호출되는 경로</url-pattern>
</servlet-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>lesson05_JSP_Servlet_Share</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>ServletJSP</servlet-name>
<jsp-file>/ServletJSP_SetAttribute.jsp</jsp-file>
<init-param>
<param-name>adminId</param-name>
<param-value>Admin</param-value>
</init-param>
<init-param>
<param-name>adminPw</param-name>
<param-value>1234</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ServletJSP</servlet-name>
<url-pattern>/ServletJSP_SetAttribute.jsp</url-pattern>
</servlet-mapping>
</web-app>
2. 위와같이 web.xml 파일에 초기화 데이터를 입력하였다면, Servlet 역활을 하는 곳에서 호출 할 수 있습니다.
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%!
String AdminId;
String AdminPw;
%>
<%
AdminId=config.getInitParameter("adminId");
AdminPw=getServletConfig().getInitParameter("adminPw");
%>
<h6>init Param에 담긴 값입니다.</h6>
<p>AdminId : <%= AdminId%></p>
<p>AdminPw : <%= AdminPw%></p>
</body>
</html>
어디에서나 사용 할 수 있는 데이터 초기화 방법
여러 Servlet에서 데이터를 공유할 경우, web.xml에서 Context-param 을 통해 공유 할 수 있습니다.
1. web.xml 파일 안에 context-param을 선언 후 , 공유할 데이터를 입력해 줍니다.
web.xml 설정 패턴
<context-param>
<param-name>호출시 파라메터 호출시, 명칭</param-name>
<param-value>위 명칭의 값</param-value>
</context-param>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>lesson05_JSP_Servlet_Share</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>imageDirectory</param-name>
<param-value>/upload/images</param-value>
</context-param>
<context-param>
<param-name>devServer</param-name>
<param-value>127.0.0.1</param-value>
</context-param>
<context-param>
<param-name>prodServer</param-name>
<param-value>127.0.0.1</param-value>
</context-param>
<context-param>
<param-name>developer</param-name>
<param-value>kyleNoh</param-value>
</context-param>
</web-app>
2. 위와같이 web.xml 파일에 데이터를 입력하였다면, 어디서든 데이터를 호출 할 수 있습니다.
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%!
String imageDirectory;
String devServer;
String developer;
%>
<%
imageDirectory = application.getInitParameter("imageDirectory");
devServer = application.getInitParameter("devServer");
developer = application.getInitParameter("developer");
%>
<h6>Context-Param에 담긴 값입니다.</h6>
<p>imageDirectory : <%= imageDirectory%></p>
<p>devServer : <%= devServer%></p>
<p>developer : <%= developer%></p>
</body>
</html>
'Web Programming > JSP' 카테고리의 다른 글
7. JSP DB Connect (0) | 2020.03.23 |
---|---|
6. JSP Cookie & Session (0) | 2020.03.21 |
4. JSP 스크립트 태그 (0) | 2020.03.18 |
3. JSP Servlet Request Response (0) | 2020.03.17 |
2. JSP Servlet Basic (0) | 2020.03.17 |