ecsimsw
내장 객체 / ServletContext / ServletConfig 본문
Server application/Web, Servlet
내장 객체 / ServletContext / ServletConfig
JinHwan Kim 2020. 5. 13. 04:25ServletContext
웹 어플리케이션 전역에서 사용할 공동의 자원을 미리 바인딩하여 서블릿들이 이를 공유할 수 있도록 한다. 톰캣 컨테이너가 실행되면 웹 어플리케이션에 한 개씩 Context 객체를 생성한다.
Read and Write : parameter / attribute
String getServerInfo()
String getInitParameter(name)
Object getAttribute(name)
void setAttribute(name, object)
String getMimeType(fileName)
String RealPath(path)
void log(message)
ServletConfig
ServletContext는 범위를 application으로 한다면, ServletConfig는 servlet 내부로 한정된다.
ReadOnly : parameter
Enumeration getInitParameterNames()
String getInitParameter(name)
String getServletName()
ServletContext getServletContext()
Parameter
1. ServletContext
web.xml에 parameter 등록
<context-param>
<description>made by ECSIMSW</description>
<param-name>defaultDIR</param-name>
<param-value>/download</param-value>
</context-param>
getServletContext로 context 객체 가져오고, getInitParameter 메소드로 값 반환
ServletContext context = this.getServletContext();
context.getInitParameter("defaultDIR"));
JSP에서는 ServletContext를 application으로 내장하고 있어, 바로 사용할 수 있음
<% application.getInitParameter("defaultDIR") %>
2. ServletConfig
web.xml/ parameter를 등록할 servlet에 parameter 등록
<servlet>
<servlet-name>myServlet</servlet-name>
<servlet-class>com.ecsimsw.ServletEX</servlet-class>
<init-param>
<param-name>defaultId</param-name>
<param-value>jinhwan</param-value>
</init-param>
<init-param>
<param-name>defaultPw</param-name>
<param-value>1234</param-value>
</init-param>
</servlet>
getServletConfig로 config 객체 가져오고, getInitParameter 메소드로 값 반환
ServletConfig config = this.getServletConfig();
config.getInitParameter("defaultId");
JSP에서는 ServletConfig를 config로 내장하고 있어, 바로 사용할 수 있음
<%= config.getInitParameterNames() %>
<%= config.getInitParameter() %>
Attribute
(String)getServletContext().setAttribute("name","jinhwan");
(String)application.setAttribute("name","jinhwan");
(String)getServletContext().getAttribute("name");
(String)application.getAttribute("name");
'Server application > Web, Servlet' 카테고리의 다른 글
JSP, Servlet / 쿠키 (0) | 2020.05.14 |
---|---|
내장 객체 / out / exception (0) | 2020.05.13 |
JSP / 스크립트 (0) | 2020.05.11 |
WEB / HTTP 메시지 구조 (0) | 2020.05.11 |
WEB / URL 예약 문자와 인코딩 (0) | 2020.05.11 |
Comments