2012-11-30 10 views
6

मैं init ओवरराइड करने के लिए कोशिश कर रहा हूँ (ServletConfig config) method.My कोड है:जावा सर्वलेट ओवरराइड init (ServletConfig config)

public void init(ServletConfig config) throws ServletException { 
    ServletContext sc = getServletContext(); // ----- NullPointerException 
} 

इस NullPointerException दे रहा है।

तो मैं के रूप में इसे संशोधित:

public void init(ServletConfig config) throws ServletException { 
    ServletContext sc = config.getServletContext(); // ----- works fine 
} 

यह ठीक काम करता है। मुझे पता है कि हमें init() विधि को ओवरराइड करना चाहिए और इनिट (ServletConfig कॉन्फ़िगर) नहीं है लेकिन
क्या कोई मुझे उचित कारण बता सकता है कि यह क्यों हो रहा है?

+1

आपको यह कहां मिला * हमें इनिट() 'विधि को ओवरराइड करना चाहिए और' init (ServletConfig config) '* नहीं करना चाहिए? –

+0

मैं यह नहीं कह रहा हूं कि हम init (ServletConfig config) विधि को ओवरराइड नहीं कर सकते हैं, हम कर सकते हैं, लेकिन मैंने पढ़ा है कि init() विधि को आंतरिक रूप से init (ServletConfig कॉन्फ़िगर) विधि से बुलाया जाता है, इसलिए हमें init() विधि को ओवरराइड करना चाहिए। –

+0

ऐसा लगता है कि आपके पास पहले से ही आपका जवाब है। आपको और क्या चाहिए? –

उत्तर

19

init(ServletConfig) के लिए दस्तावेज़ की तुलना करें:

 
public void init(ServletConfig config)throws ServletException 
Called by the servlet container to indicate to a servlet that the servlet 
is being placed into service. 

See Servlet#init. This implementation stores the ServletConfig object 
it receives from the servlet container for later use. When overriding 
this form of the method, call super.init(config). 

और init() के लिए प्रलेखन के साथ कि तुलना:

 
public void init() throws ServletException 
A convenience method which can be overridden so that there's no need to 
call super.init(config). 

Instead of overriding init(ServletConfig), simply override this method 
and it will be called by GenericServlet.init(ServletConfig config). The 
ServletConfig object can still be retrieved via getServletConfig(). 

जब init(ServletConfig) अधिभावी, पहली बात यह है कि किया जाना चाहिए कॉल करने के लिए है:

super.init(config); 

यदि आप डी o फिर यह आपकी विधि में getServletContext() पर सीधे कॉल करने के लिए एनपीई में परिणाम नहीं देगा।

+0

का मतलब है कॉल करने के बाद - super.init (config); सर्वलेट को servletcontext का संदर्भ मिलेगा? –

+0

@ रितेश कौशिक हां, इसका मतलब यही है। –

1

क्योंकि में:

public void init(ServletConfig config) throws ServletException 
{ 
    ServletContext sc = getServletContext(); 
} 

आप super.init(ServletConfig) आह्वान नहीं है। नतीजतन, ServletConfig सर्वलेट उदाहरण में संग्रहीत नहीं है, और बाद में कॉल प्राप्त करने के लिए ServletConfig शून्य वापस आ जाएगा।

2

यह इसलिए होता है क्योंकि आप गलत विधि तंत्र अधिभावी कर रहे हैं आप

 public void init(ServletConfig config) throws ServletException { 
     super.init(config); 
      ServletContext sc = getServletContext(); 
    } 

इसके बजाय अधिभावी हैं init(ServletConfig) अधिभावी के लिए, बस नीचे दी गई विधि को ओवरराइड और इसके द्वारा GenericServlet.init(ServletConfig config)

public void init() throws ServletException { 
ServletContext sc = getServletContext(); 
} 
0

बस डाल बुलाया जाएगा आपकी ओवरराइड विधि की बहुत पहली पंक्ति में सुपर इनिट (कॉन्फ़िगरेशन)

public void init(ServletConfig config) throws ServletException 
संबंधित मुद्दे