gpt4 book ai didi

java.lang.异常 : ServletConfig has not been initialized

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:30:38 24 4
gpt4 key购买 nike

我有一个问题是:

java.lang.Exception: ServletConfig has not been initialized

我搜索了将近 2 天,但没有找到适合我的解决方案。每个人都说过super.init(config) 必须被使用。我试过了,但对我来说没有任何改变。

我的初始化方法;

@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);
}

和我的 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());
}
}

这一行抛出异常;

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

在 AppServiceServlet 中说:

java.lang.Exception: ServletConfig has not been initialized.

请帮忙。

最佳答案

这个调用:

AppServiceServlet service = new AppServiceServlet();

通过 new 实例化一个 servlet 实例,这绕过了正常的容器管理的 servlet 创建。因此,关键类变量(例如,servlet 配置)未正确初始化。

稍后,您正在调用 getServletContext,它只是重定向到 getServletConfig().getServletContext(),但是因为 servlet 配置从未完成,您得到一个异常(exception)。

事实上,以您现在的方式在 servlet 上调用 new 是不符合规范的 - servlet 应该由 Web 应用程序容器维护。启动启动 servlet 的正确方法是通过 web.xml 文件中的配置或注释。

关于java.lang.异常 : ServletConfig has not been initialized,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14518089/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com