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

getInitParameterNames() w/o super.init(ServletConfig config) ?

 
Xiaolu Gao
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am confused by a example about getting servlet initial parameters in Jason Hunter & William Crawford's "Java Sevelet Programing". Here is the example:
import java.io.*;
import java.util.*;
import javax.servlet.*;
public class InitSnoop extends GenericServlet {
// No init() method needed
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
res.setContentType("text/plain");
PrintWriter out = res.getWriter();
out.println("Init Parameters:");
Enumeration enum = getInitParameterNames();
while (enum.hasMoreElements()) {
String name = (String) enum.nextElement();
out.println(name + ": " + getInitParameter(name));
}
}
}
Why "No init() method is needed" in this case?
My understanding is that in order to use GenericServlet.getInitParameterNames() method, a ServletConfig object has to be passed to GenericServlet.init() first. Otherwise, a NullPointerException will be thrown when any ServletConfig method is used. Usually it is done by using super.init(ServletConfig config) in the init() method of the class which subclasses from GenericServlet. Is my understanding right?
Thanks for any clarification and help.
 
Neetu Gupta
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To my understanding, just to access functions like getparameternames(), you don't need servlet config. It is only when you try to access functions like getservletcontext(), you need servlet config by using getservletconfig(). It is then you need to use init() function and initialize the config for later use in doget(), dopost() methods.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic