web.xml

์ฝœ๋ผ๋ง˜ยท2023๋…„ 6์›” 21์ผ
0

JSP

๋ชฉ๋ก ๋ณด๊ธฐ
12/17
post-thumbnail

๐Ÿ XML

  • ๋ฐ์ดํ„ฐ๋ฅผ ํšจ๊ณผ์ ์œผ๋กœ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ๋ฌธ์ž์—ด ๊ตฌ์กฐ
  • ๋ฐ์ดํ„ฐ๋ฅผ XML ํ˜•ํƒœ๋กœ ๋งŒ๋“ค๋ฉด ๋‹ค์–‘ํ•œ ํ”„๋กœ๊ทธ๋žจ์œผ๋กœ ํ•ด์„ํ•  ์ˆ˜ ์žˆ๋‹ค
  • ์–ธ์–ด์— ๊ตฌ์• ๋ฐ›์ง€ ์•Š๋Š” ๋ฐ์ดํ„ฐ ์ „๋‹ฌ์ด ๊ฐ€๋Šฅํ•˜๋‹ค(์–ธ์–ด๋ฅผ ์ดˆ์›”ํ•˜๋Š” ๋ฐ์ดํ„ฐ ์ „๋‹ฌ ํ‘œ์ค€ ๊ทœ๊ฒฉ)
    ex> Java์—์„œ XML ํ˜•์‹ ํŒŒ์ผ์„ ๋งŒ๋“  ํ›„ ํŒŒ์ด์ฌ์—์„œ ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์‹œ ์ฝ์–ด์„œ ์‚ฌ์šฉ๊ฐ€๋Šฅ

๐Ÿ web.xml์— Servlet๋“ฑ๋กํ•˜๊ธฐ

๐Ÿคบ .java๋กœ ๋œ ์„œ๋ธ”๋ฆฟ ๋“ฑ๋กํ•˜๊ธฐ

webapp > WEB-INT > lib > web.xml >>

<servlet>
  	<servlet-name>myservlet</servlet-name>
  	<servlet-class>chap05.servlet.HelloServlet</servlet-class>
</servlet>

๐Ÿคบ .java๋กœ ๋œ ์„œ๋ธ”๋ฆฟ ๋“ฑ๋กํ•˜๊ธฐ

webapp > WEB-INT > lib > web.xml >>

<servlet>
  	<servlet-name>myservlet2</servlet-name>
  	<jsp-file>/encoding.jsp</jsp-file>
</servlet>

๐Ÿ ์„œ๋ธ”๋ฆฟ ๋งคํ•‘ํ•˜๊ธฐ

  • ๋งคํ•‘ : ์„œ๋ธ”๋ฆฟ์˜ url์ฃผ์†Œ๋ฅผ ์‰ฝ๊ณ  ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด ์„œ๋ธ”๋ฆฟ์—๊ฒŒ ๋‹ค๋ฅธ ์ด๋ฆ„(๋ณ„๋ช…)์„ ๋ถ€์—ฌํ•˜๋Š” ๊ฒƒ
  • * : ๋ชจ๋“  ๊ฒƒ
  • /path/* : path ๋’ค์˜ ๋ชจ๋“  ๊ฒฝ๋กœ๋Š” ๋‹ค ์—ฌ๊ธฐ๋กœ ์˜จ๋‹ค
    ex> path/123, path/์•„๋ฌด๋ง ๋“ฑ๋“ฑ ๋ชจ๋‘
  • *.hello : ํ™•์žฅ์ž๊ฐ€ hello์ธ ๋ชจ๋“  ๊ฒƒ
  • / : ์•„๋ฌด๋Ÿฐ ๋งคํ•‘์—๋„ ๊ฑธ๋ฆฌ์ง€ ์•Š์€ ์š”์ฒญ์ด ๊ฐ€๊ฒŒ ๋˜๋Š” ๊ณณ (์ค‘์š”)

๐Ÿคบ ๋“ฑ๋กํ•œ ์„œ๋ธ”๋ฆฟ url ๋งคํ•‘ํ•˜๊ธฐ

webapp > WEB-INT > lib > web.xml >>

<servlet-mapping>
  	<servlet-name>myservlet</servlet-name>
  	<url-pattern>/hello</url-pattern>
</servlet-mapping>

<servlet-mapping>
  	<servlet-name>myservlet2</servlet-name>
  	<url-pattern>/encoding_test</url-pattern>
</servlet-mapping>

๐Ÿคบ ์•„๋ฌด๋Ÿฐ ๋งคํ•‘์—๋„ ๊ฑธ๋ฆฌ์ง€ ์•Š์€ ์š”์ฒญ

<!-- ์•„๋ฌด๋Ÿฐ ๋งคํ•‘์—๋„ ๊ฑธ๋ฆฌ์ง€ ์•Š์€ ์š”์ฒญ์€ elseServlet์œผ๋กœ ์ด๋™ํ•ด! -->
<servlet>
  	<servlet-name>elseServlet</servlet-name>
  	<servlet-class>chap05.servlet.ElseServlet</servlet-class>
  </servlet>

<servlet-mapping>
  	<servlet-name>elseServlet</servlet-name>
  	<url-pattern>/</url-pattern>
</servlet-mapping>

๐Ÿ ์ดˆ๊ธฐํ™” ํŒŒ๋ผ๋ฏธํ„ฐ (Init Parameter)

  • ์›น ์„œ๋ฒ„๋ฅผ ์ฒ˜์Œ ์‹œ์ž‘ํ•  ๋•Œ ์ดˆ๊ธฐํ™”๋˜๋Š” ๋ณ€์ˆ˜๋“ค์„ web.xml์—์„œ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค
  • context-param, init-param์ด ์žˆ๋‹ค

๐Ÿคบ context-param

  • context-param : ๋ชจ๋“  ์„œ๋ธ”๋ฆฟ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ „์—ญ ํŒŒ๋ผ๋ฏธํ„ฐ
  • id์™€ pw๋ฅผ context-param์œผ๋กœ ๋“ฑ๋กํ•ด๋†“์œผ๋ฉด ๋ชจ๋“  ์„œ๋ธ”๋ฆฟ์—์„œ ์ ‘๊ทผํ•˜์—ฌ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

webapp > WEB-INT > lib > web.xml >>

<context-param>
   		<param-name>account_id</param-name>
   		<param-value>hr</param-value>
</context-param>
<context-param>
  		<param-name>account_pw</param-name>
   		<param-value>1234</param-value>
</context-param>

ElseServlet.java (์„œ๋ธ”๋ฆฟ) ์—์„œ ์ ‘๊ทผํ•˜๊ธฐ >>

public class ElseServlet extends HttpServlet {

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// JSP application scope์˜ ์„œ๋ธ”๋ฆฟ ๋ฒ„์ „
		ServletContext application = request.getServletContext();
		
		// web.xml์—์„œ ๋“ฑ๋กํ•œ context-param์€ application scope์— ๋“ค์–ด์žˆ๋‹ค
		System.out.println(application.getInitParameter("account_id"));
		System.out.println(application.getInitParameter("account_pw"));
		
		
		// JSP session scope์˜ ์„œ๋ธ”๋ฆฟ ๋ฒ„์ „
		HttpSession session = request.getSession();
		
		
		response.getWriter()
		.append("<html><head><title></title></head>")
		.append("<body>404 Not Found</body>");
	}
}

๐Ÿ“บ ์ถœ๋ ฅ ๊ฒฐ๊ณผ >>


๐Ÿคบ init-param

  • init-param : ํ•ด๋‹น ์„œ๋ธ”๋ฆฟ์—์„œ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ง€์—ญ ํŒŒ๋ผ๋ฏธํ„ฐ, <servlet> ๋‚ด๋ถ€์— ๋“ฑ๋กํ•œ๋‹ค
  • init(): ์„œ๋ธ”๋ฆฟ์ด ์ฒ˜์Œ ์‹คํ–‰๋˜๋Š” ์‹œ์ ์— ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์„œ๋“œ
  • init()๋Š” ํ•ด๋‹น ์„œ๋ธ”๋ฆฟ์œผ๋กœ ์ฒซ ์š”์ฒญ์ด ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ์‹คํ–‰๋œ๋‹ค

ElseServlet.java (์„œ๋ธ”๋ฆฟ) >>

public class ElseServlet extends HttpServlet {
	Color mainColor;
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		System.out.println(config.getInitParameter("main_color"));
		System.out.println(config.getInitParameter("logo_path"));
	}
		response.getWriter()
		.append("<html><head><title></title></head>")
		.append("<body>404 Not Found</body>");
	}
}

๐Ÿ“บ ์ถœ๋ ฅ ๊ฒฐ๊ณผ >>

profile
์ฝœ๋ผ๋ง˜ ์ฝ”๋”ฉ์ผ๊ธฐ

0๊ฐœ์˜ ๋Œ“๊ธ€