[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 { } }
-