• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Query from mock exam

 
Ashu Jain
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kathiresan Chinna
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I did same declaration in DD, and i did not get any error.
But the output was
null
null

Kathir
 
Krishna Srinivasan
Ranch Hand
Posts: 1876
Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes..servlet mapping is not allowed init-param. you can read more here:http://e-docs.bea.com/wls/docs81/webapp/components.html#148787
 
Ashu Jain
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strange...there is no option of "null null" and "not even problem with DD".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic