0

当我运行时:

Enumeration<String> enums = this.getServletConfig().getInitParameterNames();

我在浏览器中收到HTTP Status 500错误消息。

HTTP 状态 500 -

类型异常报告

信息

描述 服务器遇到一个内部错误,阻止它完成这个请求。

例外

java.lang.NullPointerException com.ypd.config.ConfigDemo.doGet(ConfigDemo.java:32) javax.servlet.http.HttpServlet.service(HttpServlet.java:622) javax.servlet.http.HttpServlet.service(HttpServlet.java :729) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

note Apache Tomcat/9.0.0.M17 日志中提供了根本原因的完整堆栈跟踪。Apache Tomcat/9.0.0.M17

快照:

在此处输入图像描述

我的配置代码在web.xml

<servlet>
    <servlet-name>ConfigDemo</servlet-name>
    <servlet-class>com.ypd.config.ConfigDemo</servlet-class>
    <init-param>
        <param-name>path</param-name>
        <param-value>/Users/luowensheng/Downloads/图片1.png</param-value>
    </init-param>
    <init-param>
        <param-name>aaa</param-name>
        <param-value>aaa's value</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>ConfigDemo</servlet-name>
    <url-pattern>/configDemo</url-pattern>
</servlet-mapping>

在我的ConfigDemo.java

public class ConfigDemo extends HttpServlet{

    private ServletConfig config;

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

        this.config = config;
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Enumeration<String> enums = this.getServletConfig().getInitParameterNames(); // this is 32th line, here I get the error.
        while(enums.hasMoreElements()) {
            String paramName = enums.nextElement();
            String paramValue = this.getServletConfig().getInitParameter(paramName);

            resp.getWriter().write(paramName + "=" + paramValue);

        }

    }
}

但最后我发现如果我注释下面的代码,就不会出现错误:</p>

private ServletConfig config;

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

    this.config = config;
}

为什么?为什么这里不能有这个 ServletConfig 成员变量?

4

0 回答 0