Java.lang.Exception: ServletConfig was not initialized

I have a problem:

java.lang.Exception: ServletConfig was not initialized

I searched for it for almost 2 days, but I didn't have a solution for me. Everyone said super.init (config) . I tried this but nothing changed for me.

My init method;

@Override
public void init(ServletConfig config) throws ServletException {

    super.init(config);

    AppServiceServlet service = new AppServiceServlet();
    try {
        service.getir();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    AutoCheckStatus.autoCheckStatus(600000);
}

      

and my AppServiceServlet;

    public List<SswAppServiceDto> getir() throws Exception {
    try {
        final WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(this
                .getServletContext());
        setiAppServiceBusinessManager((IAppServiceBusinessManager) context.getBean(BEAN_ADI));

        List<SswAppService> result = getiAppServiceBusinessManager().getir();

        List<SswAppServiceDto> list = DtoConverter.convertSswAppServiceDto(result);

        for (int i = 0; i < result.size(); i++) {
            AppService appService = new AppService();
            appService.setServiceName(result.get(i).getName());
            appService.setUid(result.get(i).getServiceUid());
            appService.setHost(result.get(i).getHost());
            appService.setPort((int) result.get(i).getPort());
            SystemConfiguration.appServiceList.put(appService.getUid(), appService);
        }
        return list;

    } catch (RuntimeException e) {
        throw new Exception(e.getMessage(), e.getCause());
    }
}

      

An exception is thrown on this line;

          final WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());

      

in AppServiceServlet and says:

java.lang.Exception: ServletConfig was not initialized.

Pls help.

+3


source to share


1 answer


This call:

AppServiceServlet service = new AppServiceServlet();

      

Instantiates a servlet via new

, which bypasses the normal creation of container-managed servlets. Thus, critical class variables (such as the config of a servlet) are not initialized correctly.



Later, you make a call getServletContext

that just redirects to getServletConfig().getServletContext()

, but since the servlet configuration has not been completed, you get an exception.

Infact, calling new

on a servlet like you is not spec-compliant - servlets must be supported by the web application container. The correct way to start the startup servlet is either through the configuration in the web.xml file or through the annotation.

+2


source







All Articles