171114 ServletConfig - RYUDONGJIN/Memo_wiki GitHub Wiki

์„œ๋ธ”๋ฆฟ ์ดˆ๊ธฐํ™” ํŒŒ๋ผ๋ฏธํ„ฐ : ServletConfig

  • ํŠน์ • Servlet์ด ์ƒ์„ฑ๋  ๋•Œ ์ดˆ๊ธฐ์— ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋“ค์„ ์ดˆ๊ธฐํ™” ํŒŒ๋ผ๋ฏธํ„ฐ๋ผ๊ณ  ํ•œ๋‹ค.

  • ์‚ฌ์šฉ ๋ฐฉ๋ฒ•

  1. web.xml์— ๊ธฐ์ˆ ํ•˜๊ณ  Servlet ํŒŒ์ผ์—์„œ getInitParameter() ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ์ ‘๊ทผ
  2. Servlet ํŒŒ์ผ์— ์ง์ ‘ ๊ธฐ์ˆ  -> @WebInitParam์— ์ดˆ๊ธฐํ™” ํŒŒ๋ผ๋ฏธํ„ฐ ๊ธฐ์ˆ ํ•˜๊ณ  ServletConfig ๋ฉ”์†Œ๋“œ ์ด์šฉํ•ด์„œ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
  <servlet>
  	<servlet-name>ServletInitParam</servlet-name>
  	<servlet-class>com.javalec.ex.ServletInitParam</servlet-class>
  	<init-param>
  		<param-name>id</param-name>
  		<param-value>abcde</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:\\CodeLab\\JSP\\projects\\hellojsp</param-value>
  	</init-param>
  </servlet>
  <servlet-mapping>
	<servlet-name>ServletInitParam</servlet-name>
	<url-pattern>/InitParam</url-pattern>  
  </servlet-mapping>

getInitParameter() ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ๊ฐ’์„ ๋ถˆ๋Ÿฌ์˜จ๋‹ค (์ƒ์†๋ฐ›์€ ํด๋ž˜์Šค์˜ ๋ฉ”์†Œ๋“œ)

   String id = getInitParameter("id");
   String pw = getInitParameter("pw");
   String path = getInitParameter("path");
@WebServlet(urlPatterns = {"/ServletInitParamEx02"},
		initParams = {@WebInitParam(name="id", value="starwars"),
				@WebInitParam(name="pw", value="MaytheForcebeWithYou"),
				@WebInitParam(name="path", value="C:\\CodeLab\\workspace_jsp")
				
		})
โš ๏ธ **GitHub.com Fallback** โš ๏ธ