config.getInitParameter始终返回null [英] config.getInitParameter always return null

查看:1067
本文介绍了config.getInitParameter始终返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 config.getInitParameter(String)总是在以下代码示例中返回 null

Why does config.getInitParameter(String) always return null in the following code example?

public void init(ServletConfig config) throws ServletException
{
    super.init(config);
    filename = config.getInitParameter("addressfile");

这是web.xml文件

This is web.xml file

<servlet>
<servlet-name>ListManagerServlet</servlet-name>
<servlet-class>savva.listmanagerservlet.ListManagerServlet</servlet-class>
<init-param>
    <param-name>addressfile</param-name>
    <param-value>d:\temp\demo.txt</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ListManagerServlet</servlet-name>
<url-pattern>/ListManagerServlet</url-pattern>
</servlet-mapping>

UPD:Eclipse EE Indigo,Java 1.6,Tomcat 7.0

UPD: Eclipse EE Indigo, Java 1.6, Tomcat 7.0

推荐答案

规范的方法是使用继承的 GenericServlet#getInitParameter() 在无参数 init() 方法(以及删除任何 init(config)方法)。

The canonical way is to just use the inherited GenericServlet#getInitParameter() in the argumentless init() method (and remove any init(config) method).

@Override
public void init() throws ServletException {
    filename = getInitParameter("addressfile");
}

如果仍然无效,那么 web.xml 未正确部署,或者参数名称中有拼写错误,或者您实际访问的是与 filename 不同的实例变量使用/测试它。

If that still doesn't work, then your web.xml is not properly been deployed, or you have a typo in the parameter name, or you actually accessed a different instance variable than filename to use/test it.

这篇关于config.getInitParameter始终返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆