[Servlet] ServletConfig / ServletContext


ServletConfig


  • ์„œ๋ธ”๋ฆฟ API๋Š” Servlet๊ณผ ServletConfig ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•ด ์ œ๊ณต, GenericSrvlet ์ถ”์ƒํด๋ž˜์Šค๊ฐ€ ์ด ๋‘ ์ธํ„ฐํŽ˜์ด์Šค์˜ ์ถ”์ƒ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„

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

  • ํ•˜๋‚˜์˜ ์„œ๋ธ”๋ฆฟ ๊ฐ๊ฐ์˜ ํŒŒ์ผ์—์„œ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ, ๊ณต์œ  ๋ถˆ๊ฐ€๋Šฅ

  • getโ€ฆ()๋ฉ”์„œ๋“œ๋งŒ ์ง€์› > Read-Only

  • ์„œ๋ธ”๋ฆฟ์ด ์ƒ์„ฑ๋  ๋•Œ ์ƒ์„ฑ, ์„œ๋ธ”๋ฆฟ์ด ์†Œ๋ฉธ๋˜๋ฉด ๊ฐ™์ด ์†Œ๋ฉธ

  • web.xml ํŒŒ์ผ์— ์ž‘์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ servlet ํŒŒ์ผ์— @WebServlet ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•ด ์ž‘์„ฑ ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

  • ์›น ํŽ˜์ด์ง€๊ฐ€ ์‹คํ–‰๋  ๋•Œ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ(ex) ๊ฒฝ๋กœ, ์•„์ด๋”” ์ •๋ณด, โ€ฆ)๋ฅผ ์ „๋‹ฌ, ์„œ๋ธ”๋ฆฟ ํ”„๋กœ๊ทธ๋žจ์ด ์ตœ์ดˆ ์‹คํ–‰๋  ๋•Œ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์•„๋‹Œ ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ๋ฐ›๋Š” ๊ฐ’



ServletConfig - web.xml


  • init-param ํƒœ๊ทธ ์‚ฌ์šฉ

  • ex)



ServletConfig - @WebInitParam


  • @WebServlet, @WebInitParam ์–ด๋…ธํ…Œ์ด์…˜ ์‚ฌ์šฉ

  • @WebServlet(urlPatterns = {"/"},
                initParams = {
                        @WebInitParam(name = "", value = ""),
                        @WebInitParam(name = "", value = "")
                })
    
  • ex)

      import java.io.IOException;
      import java.io.PrintWriter;
    
      import javax.servlet.ServletException;
      import javax.servlet.annotation.WebInitParam;
      import javax.servlet.annotation.WebServlet;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
    
      @WebServlet(urlPatterns = {"/InitParam"},
                  initParams = {
                          @WebInitParam(name = "uid", value = "admin"),
                          @WebInitParam(name = "upw", value = "12345")
                  })
    
      public class InitParam extends HttpServlet {
          private static final long serialVersionUID = 1L;
    
          public InitParam() {
    
          }
    
          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
              String uid = this.getInitParameter("uid");
              String upw = this.getInitParameter("upw");
                
              response.setContentType("text/html; charset=UTF-8");
              PrintWriter out = response.getWriter();
                
              out.println("<html><body>");
              out.println("uid : " + uid + "<br>");
              out.println("upw : " + upw);
              out.println("</body></html>");
                
              out.close();
          }
    
          protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
          }
    
      }
    



ServletContext


  • ServletContext ํด๋ž˜์Šค๋Š” ์ปจํ…Œ์ด๋„ˆ(ํ†ฐ์บฃ) ์‹คํ–‰ ์‹œ ๊ฐ ์ปจํ…์ŠคํŠธ(์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜)๋งˆ๋‹ค ํ•œ ๊ฐœ์˜ ServletContext ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ, ์ปจํ…Œ์ด๋„ˆ ์ข…๋ฃŒ ์‹œ ServletContext ๊ฐ์ฒด๋„ ์†Œ๋ฉธ

  • ์„œ๋ธ”๋ฆฟ๊ณผ ์ปจํ…Œ์ด๋„ˆ ๊ฐ„์˜ ์—ฐ๋™์„ ์œ„ํ•ด ์‚ฌ์šฉ

  • ServletConfig๋Š” ํ•˜๋‚˜์˜ ์„œ๋ธ”๋ฆฟ ์•ˆ์—์„œ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, ServletContext๋Š” ์„œ๋ธ”๋ฆฟ๋ผ๋ฆฌ ๋ฐ์ดํ„ฐ(์ž์›)๋ฅผ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๋‹ค.

  • getโ€ฆ() / setโ€ฆ() ๋ฉ”์„œ๋“œ ๋ชจ๋‘ ์ง€์› > Read/Write ๊ฐ€๋Šฅ



ServletContext


  • context-param ํƒœ๊ทธ ์‚ฌ์šฉ

  • ํŠน์ • ์„œ๋ธ”๋ฆฟ์„ ๋งคํ•‘ํ•˜๋Š” ์ฝ”๋“œ ์—†์Œ -> ๋‹ค๋ฅธ ํŒจํ‚ค์ง€ ๋‹ค๋ฅธ ์„œ๋ธ”๋ฆฟ ํŒŒ์ผ์—์„œ๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ!

  • ex)

    • web.xml

        ...
      
                <context-param>
                <param-name>uid</param-name>
                <param-value>user_id</param-value>
            </context-param>
            <context-param>
                <param-name>upw</param-name>
                <param-value>user_pw</param-value>
            </context-param>
        </web-app>
      
    • Servlet

        import java.io.IOException;
        import javax.servlet.ServletException;
        import javax.servlet.annotation.WebServlet;
        import javax.servlet.http.HttpServlet;
        import javax.servlet.http.HttpServletRequest;
        import javax.servlet.http.HttpServletResponse;
      
        @WebServlet("/ContextParam")
        public class ContextParam extends HttpServlet {
            private static final long serialVersionUID = 1L;
      
            public ContextParam() {
                      
            }
      
            protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                  
                String uid = this.getServletContext().getInitParameter("uid");
                String upw = this.getServletContext().getInitParameter("upw");
                      
                System.out.println(uid + "\n" + upw);
                      
            }
      
            protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      
            }
      
        }
      

Categories:

JSP/Servlet