config.getInitParameter always return null

前端 未结 5 1133
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-12 18:40

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

public void init(ServletConfig config) t         


        
相关标签:
5条回答
  • 2021-01-12 19:21

    It's never a good idea to override the init(config) method. Instead use the provided init() convenience method and do a getServletConfig() to get the configuration parameters:

    http://docs.oracle.com/javaee/1.2.1/api/javax/servlet/GenericServlet.html#init() http://docs.oracle.com/javaee/1.2.1/api/javax/servlet/GenericServlet.html#getServletConfig()

    0 讨论(0)
  • 2021-01-12 19:24

    Ensure your servlet is calling super.init(config) on its init method, else it won't work.

    0 讨论(0)
  • 2021-01-12 19:38

    Make sure you have really deployed the proper web.xml. Also check with config.getInitParameterNames() what parameters have been found.

    0 讨论(0)
  • 2021-01-12 19:40

    If use the IDE STS4, checkout if annotation on the class name exists, use BOTH "annotation" and "web.xml" may cause the value null.

    0 讨论(0)
  • 2021-01-12 19:45

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

    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.

    0 讨论(0)
提交回复
热议问题