7.1 web.xml์˜ ์ผ๋ฐ˜ ์Šคํ‚ค๋งˆ

web.xml ํŒŒ์ผ์€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ตฌ์„ฑ ์ •๋ณด๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ํ•„์ˆ˜ ๋ถ€๋ถ„์€ ์•„๋‹ˆ์ง€๋งŒ ์›น ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์„ ๊ตฌ์„ฑํ•˜๋Š” ๋ฐ ๋งค์šฐ ๋„๋ฆฌ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

์ด ํŒŒ์ผ์€ WEB-INF ํด๋” ์— ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค . Tomcat์ด ์‹œ์ž‘๋˜๋ฉด ๋‚ด์šฉ์„ ์ฝ๊ณ  ํฌํ•จ๋œ ๊ตฌ์„ฑ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ผ์— ์˜ค๋ฅ˜๊ฐ€ ์žˆ์œผ๋ฉด Tomcat๋„ ์˜ค๋ฅ˜๋ฅผ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ web.xml:



<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
  
  <servlet>
       <servlet-name>HelloWorld</servlet-name>
       <servlet-class>HelloServlet</servlet-class>
   </servlet>
    
  <servlet-mapping>
      <servlet-name>HelloWorld</servlet-name>
      <url-pattern>/welcome</url-pattern>
  </servlet-mapping>
 
  <welcome-file-list>
      <welcome-file>index.html</welcome-file>
  </welcome-file-list>
  
</web-app>

"HelloWorld"์„œ๋ธ”๋ฆฟ ์ด๋ฆ„ ๊ณผ ์„œ๋ธ”๋ฆฟ ํด๋ž˜์Šค ์˜ ๋งคํ•‘์€ ์—ฌ๊ธฐ์—์„œ ๋…น์ƒ‰์œผ๋กœ ์ž‘์„ฑ๋ฉ๋‹ˆ๋‹ค"HelloServlet" . ์„œ๋ธ”๋ฆฟ ์ด๋ฆ„ ๊ณผ URL ์ฒญํฌ ์˜ ๋งคํ•‘์€ ํŒŒ๋ž€์ƒ‰์œผ๋กœ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค"HelloWorld""http://localhost/welcome" . ๋”ฐ๋ผ์„œ ์—ฌ๊ธฐ์„œ๋Š” /welcome ๊ฒฝ๋กœ์— ์ ‘๊ทผํ•  ๋•Œ servlet ์„ ํ˜ธ์ถœํ•ด์•ผ ํ•œ๋‹ค๊ณ  ๋งํ•ฉ๋‹ˆ๋‹ค HelloServlet.class.

๋นจ๊ฐ„์ƒ‰์€ ์š”์ฒญ ์‹œ ์ œ๊ณตํ•ด์•ผ ํ•˜๋Š” ํŒŒ์ผ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค http://localhost/. ์†Œ์œ„ ํ™˜์˜ ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค . ์‚ฌ์šฉ์ž๊ฐ€ ๋‹จ์ˆœํžˆ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋ฃจํŠธ์— ํ•ด๋‹นํ•˜๋Š” ์ด๋ฆ„์„ ๋ธŒ๋ผ์šฐ์ €์— ์ž…๋ ฅํ•˜๋ฉด index.html.

7.2 ์„œ๋ธ”๋ฆฟ, ์„œ๋ธ”๋ฆฟ ๋งคํ•‘

ํ•˜๋‚˜์˜ ์„œ๋ธ”๋ฆฟ์€ ๋‹ค๋ฅธ URL์—์„œ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ web-xml์—์„œ๋Š” ์„œ๋ธ”๋ฆฟ๊ณผ URL์— ๋Œ€ํ•œ ๋งคํ•‘์ด ๋ณ„๋„๋กœ ์ž‘์„ฑ๋ฉ๋‹ˆ๋‹ค. ๋จผ์ € ์„œ๋ธ”๋ฆฟ์„ ์„ค๋ช…ํ•˜๊ณ  ๊ฐ๊ฐ ๊ณ ์œ ํ•œ ๋ฌธ์ž์—ด ์ด๋ฆ„์„ ์ง€์ •ํ•œ ๋‹ค์Œ ๊ฐ ์„œ๋ธ”๋ฆฟ์ด ์–ด๋–ค URL์— ๋งคํ•‘๋˜๋Š”์ง€ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ web.xml:



<web-app>
  
  <servlet>
    <servlet-name>remoting</servlet-name>
    <servlet-class>com.codegym.RemotingServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
 
  <servlet-mapping>
    <servlet-name>remoting</servlet-name>
    <url-pattern>/remoting/*</url-pattern>
  </servlet-mapping>
 
  <servlet>
    <servlet-name>restapi</servlet-name>
    <servlet-class>com.codegym.RestApiServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
  </servlet>
 
  <servlet-mapping>
    <servlet-name>restapi</servlet-name>
    <url-pattern>/api/*</url-pattern>
  </servlet-mapping>  
 
</web-app>

์ด ์˜ˆ์—์„œ๋Š” ๋‘ ๊ฐœ์˜ ์„œ๋ธ”๋ฆฟ์ด ์„ ์–ธ๋˜๊ณ  ๊ฐ๊ฐ ๋‹ค๋ฅธ URL ํ…œํ”Œ๋ฆฟ์— ๋งคํ•‘๋ฉ๋‹ˆ๋‹ค. ์„œ๋ธ”๋ฆฟ์€ RemotingServlet๋กœ ์ด๋™ํ•˜๋Š” ๋ชจ๋“  ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค/remoting/* . ์„œ๋ธ”๋ฆฟ์€ RestApiServlet๋กœ ์ด๋™ํ•˜๋Š” ๋ชจ๋“  ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค/api/* . ์„œ๋ธ”๋ฆฟ์—๋Š” ๋กœ๋“œ ์‹œ ์‹œ์ž‘ ๋งค๊ฐœ๋ณ€์ˆ˜์ธ ๋กœ๋“œ ์ˆœ์„œ๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

7.3 ์„œ๋ธ”๋ฆฟ ์˜ต์…˜

web.xml์˜ ๋„์›€์œผ๋กœ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ์ดˆ๊ธฐํ™” ์ค‘์— ์„œ๋ธ”๋ฆฟ์— ์ „๋‹ฌ๋  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ†ตํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค ServletConfig. ์ „์ฒด ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ๋Œ€ํ•œ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•  ์ˆ˜๋„ ์žˆ์œผ๋ฉฐ ServletContext.

์˜ˆ web.xml:



<web-app>
  <context-param>
     <description>Server production mode</description>
     <param-name>productionMode</param-name>
     <param-value>false</param-value>
  </context-param>
 
  <context-param>
     <param-name>appPropertiesConfig</param-name>
     <param-value>
        classpath:local-app.properties
        classpath:web-app.properties
     </param-value>
  </context-param>
 
  <servlet>
     <servlet-name>mainservlet</servlet-name>
     <servlet-class>com.codegym.ApplicationServlet</servlet-class>
     <init-param>
        <param-name>application</param-name>
        <param-value>com.codegym.App</param-value>
     </init-param>
     <init-param>
        <param-name>widgetset</param-name>
        <param-value>com.codegym.WidgetSet</param-value>
     </init-param>
     <init-param>
        <param-name>ui</param-name>
        <param-value>com.codegym.AppUI</param-value>
     </init-param>
  </servlet>
</web-app>

๋…น์ƒ‰์œผ๋กœ ๊ฐ•์กฐ ํ‘œ์‹œ๋œ ์ฝ”๋“œ๋Š” ์— ๋Œ€ํ•œ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•œ ๊ณณ์ž…๋‹ˆ๋‹คServletContext . ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

  • productionMode๊ฑฐ์ง“ ๊ฐ’์œผ๋กœ
  • appPropertiesConfig๋‘ ๋ฌธ์ž์—ด์˜ ๋ฐฐ์—ด:
    • classpath:local-app.properties
    • classpath:web-app.properties

์„œ๋ธ”๋ฆฟ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ํŒŒ๋ž€์ƒ‰์œผ๋กœ ํ‘œ์‹œ๋˜๋ฉฐApplicationServlet ๋‹ค์Œ์„ ํ†ตํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค ServletConfig.

  • applicationcom.codegym.App ๊ฐ’ ์‚ฌ์šฉ
  • widgetsetcom.codegym.WidgetSet ๊ฐ’ ์‚ฌ์šฉ
  • uicom.codegym.AppUI ๊ฐ’ ์‚ฌ์šฉ

7.4 ํ•„ํ„ฐ, ํ•„ํ„ฐ ๋งคํ•‘

์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—๋Š” ํŠน๋ณ„ํ•œ utility servlets - filters. ํ†ตํ™” ๋ฆฌ๋””๋ ‰์…˜, ์ธ์ฆ ํ™•์ธ ๋“ฑ ๋‹ค์–‘ํ•œ ์„œ๋น„์Šค ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ web.xml:



<web-app>
 
  <servlet>
      <servlet-name>remoting</servlet-name>
      <servlet-class>RemotingServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
  </servlet>
 
  <servlet-mapping>
      <servlet-name>remoting </servlet-name>
      <url-pattern>/remoting/*</url-pattern>
  </servlet-mapping>
 
  <filter>
      <filter-name>total_filter</filter-name>
      <filter-class>com.javrush.TotalFilter</filter-class>
  </filter>
 
  <filter-mapping>
      <filter-name>total_filter</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>
 
</web-app>

์š”์ฒญ์ด ์„œ๋ธ”๋ฆฟ์— ๋„๋‹ฌํ•˜๊ธฐ ์ „์— RemotingServletํ•„ํ„ฐ์— ์˜ํ•ด ์ฒ˜๋ฆฌ๋ฉ๋‹ˆ๋‹ค TotalFiler. ์ด ํ•„ํ„ฐ๋Š” ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์œผ๋กœ ์ด๋™ํ•˜๋Š” ๋ชจ๋“  ์š”์ฒญ์„ ๊ฐ€๋กœ์ฑ„๋„๋ก ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค . ์ด๊ฒƒ์€ ๋งคํ•‘๋œ URL ํ…œํ”Œ๋ฆฟ์— ์˜ํ•ด ๋ช…ํ™•ํ•˜๊ฒŒ ์•”์‹œ๋ฉ๋‹ˆ๋‹ค: /*.

๋‹ค์Œ ๊ฐ•์˜์—์„œ ์„œ๋ธ”๋ฆฟ ๋ฐ ํ•„ํ„ฐ์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์•Œ์•„๋ณผ ๊ฒƒ์ž…๋‹ˆ๋‹ค.