开发者

at what point in time does this.getServletName() begin to work and what is the rationale

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 der开发者_如何学Pythoneferencing 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 the init(config) method. You should likely put your initialization logic there instead of the constructor. E.g.:

public void init(ServletConfig config) {
    super.init(config);
    String ss = this.getServletName();
    // put your logic here instead of constructor
}


getServletName() method belongs to ServletConfig interface.

You should know when the ServletConfig object is created, which will then give proper value for getServletName().For this learning of servlet lifecycle is important.

If an instance of the servlet does not exist, the Web container
  -  Loads the servlet class.
  -  Creates an instance of the servlet class. // not available here.
  -  Initializes the servlet instance by calling the init method. // available here.
   Initialization is covered in Initializing a Servlet. 

The servlet container will inject the servletConfig object(by reading web.xml ,once deployment is done) in to the init method of servlet, so it is available in init and not at the constructor level.

ServletContainer

GenericServlet

 /**
  *
  * Called by the servlet container to indicate to a servlet that the
  * servlet is being placed into service.  See {@link Servlet#init}.
  *
  * <p>This implementation stores the {@link ServletConfig}
  * object it receives from the servlet container for later use.
  * When overriding this form of the method, call 
  * <code>super.init(config)</code>.
  *
  * @param config           the <code>ServletConfig</code> object
  *                 that contains configutation
  *                 information for this servlet
  *
  * @exception ServletException     if an exception occurs that
  *                 interrupts the servlet's normal
  *                 operation
  *
  * 
  * @see                UnavailableException
  *
  */

     public void init(ServletConfig config) throws ServletException {
    this.config = config;
        this.init();
   }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜