구 분 | 적용 | 범위 설정 |
ServletConfig |
| <servlet> <servlet-name> </servlet-name> <servlet-class> </servlet-class> <init-param> <param-name> </param-name> <param-value> </param-value> </init-param> </servlet> |
ServletContext | 동일 웹 애플리케이션 내 모든 서블릿(또는 JSP)에서 사용 할 수 있다. | <context-param> <param-name> </param-name> <param-value> </param-value> </context-param> |
● 예제
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <servlet> <servlet- name >test</servlet- name > <servlet-class>com.test1.TestServlet</servlet-class> <init-param> <param- name > name </param- name > <param-value>han</param-value> </init-param> <init-param> <param- name >age</param- name > <param-value>20</param-value> </init-param> </servlet> <servlet-mapping> <servlet- name >test</servlet- name > <url-pattern>/test</url-pattern> </servlet-mapping> <context-param> <param- name >city</param- name > <param-value>seoul</param-value> </context-param> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | protected void process(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletConfig config = getServletConfig(); //해당 서블릿에서만 사용가능 ServletContext context = getServletContext(); //동일한 웹어플리케이션 어디서든 접근가능 String name = config.getInitParameter( "name" ); String age = config.getInitParameter( "age" ); String city = context.getInitParameter( "city" ); //로그설정 context.log( "로그를 출력합니드아" ); resp.setContentType( "text/html;charset=utf-8" ); PrintWriter out = resp.getWriter(); out .print( "name:" + name + "<br>" ); out .print( "age:" + age + "<br>" ); out .print( "city:" + city + "<br>" ); } |
'JSP > JspServlet' 카테고리의 다른 글
XML 기본구조 (0) | 2013.08.16 |
---|---|
Tomcat web.xml (0) | 2013.08.16 |
자바클래스 메서드에 접근 (0) | 2013.08.16 |
서블릿에서 out.print() (0) | 2013.08.16 |
tag : core (0) | 2013.08.16 |