본문 바로가기

Programming/SCWCD

문제4 풀이

  1.  Given:

    String value = getServletContext().getInitParameter("foo");

in an HttpServlet and a web application deployment descriptor that contains :

<context-param>

<param-name>foo</param-name>

<param-value>frodo</param-value>

</context-param>

Which two are true?(Choose two.)

A.The foo initialization parameter CANNOT be set programmatically.

B. Compilation fails because getInitParameter returns type Object.

C. The foo initialization parameter is NOT a servlet initialization parameter.

D. Compilation fails because Servlet Context dose NOT have a getInitParameter method.

E. The foo parameter must be defined within the <servlet>element of the deployment descriptor.

F. The foo initialization parameter can also be retrieved using getServletConfig().getInitParameter("foo").

 

 

위의 문제는 ServletContext에 대해서 물어보는 문제입니다.

ServletContext는 웹 애플리케이션 안에 있는 모든 서블릿이나 JSP에서 이용 가능한 초기화 파라미터를 가지고 있습니다.

이것을 사용하는 방법에 대해 코드를 한번 살펴보면

 

ServletContext value = getServletContext();

out.println(value.getInitParameter("foo");                       <- 결과값은 frodo가 나올 것입니다.

서블릿에서는 위와 같이 코드를 작성하고,

 

<context-param>

<param-name>foo</param-name>

<param-value>frodo</param-value>

</context-param>

 DD파일에는 위와 같이 작성을 합니다.

이 글은 스프링노트에서 작성되었습니다.