There was question in amock exam on net. I am unable to understand this
Q: - Given following deployment discriptor -
<web-app version="2.4">
<
servlet>
<servlet-name>InitParamsServlet</servlet-name>
<servlet-class>scwcd14.chap02.InitParamsServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>InitParamsServlet</servlet-name>
<url-pattern>/InitParamsServlet</url-pattern>
<init-param>
<param-name>name</param-name>
<param-value>javabeat</param-value>
</init-param>
</servlet-mapping>
</web-app>
What will be the output of the following Servlet?
package scwcd14.chap02;
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class InitParamsServlet extends HttpServlet {
public void init(){
ServletConfig config = getServletConfig();
System.out.println(config.getInitParameter("name"));
System.out.println(config.getInitParameter("no-name"));
}
...
}
Options are:-
(A)The Servlet will output 'javabeat null' in the Server Console.
(B)The Servlet will fail to load as the init param property 'no-name' is not mentioned in the deployment descriptor.
(C)A NullPointerException will be raised as calling getServletConfig() in the init() method will return a null reference.
answer is (A). My concern is "Are we allowed to have <init-param> in <servlet-mapping>. I thoight we can place <init-param> only in <servlet> element. Isn't DD incorrect.