I find that this.getServletName() fails in a constructor but works in a method. Note getServletName() is provided by the parent of the parent. This was observed in Google App Engine. What is the rationale for this.getServletName() behaving this way?
(The failure is a null pointer dereferencing but I note that this is not null at the time so I think the null may be something internal to the JRE. Furthermore and as expected, assignments of the sort this.myprivate = myconstructorarg; do not produce a null dereferencing in constructors.)
public class ResponderServlet extends HttpServlet
{
public ResponderServlet()
{
String ss = this.getServletName(); // RUNTIME ERROR
}
public void doMethod(HttpServletRequest req, HttpServletResponse resp)
throws IOException
{
String ss = this.getServletName(); // WORKS WELL
}
}
The
getServletName()method will work after the web container calls theinit(config)method. You should likely put your initialization logic there instead of the constructor. E.g.: