Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet Config Param

 
Narendhiran Nagarajan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<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>

Is it correct?
Can we declare the servlet config parameters inside the <servlet-mapping> tag?
 
Deepali Anand
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No
 
Narendhiran Nagarajan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the question from the MockUp Exam (JavaBeat)


Given the deployment descriptor for a Web Application is
<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"));
}
...
}
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.


The answer is A.
How?
 
Chinmaya Chowdary
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Narendhiran. I tested this, and I got null for both 'name' and 'no-name'.
 
Narendhiran Nagarajan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
 
Mateus Lucio
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you run the application the result is really null null, since servlets init parameters are configured in the <servlet> tag and not in the <servlet-mapping>.

This question is not well written.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic