Config.getInitParameter always returns null

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

This is the 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

+5
source share
5 answers

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

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

If this still does not work, then your web.xml not been properly deployed, or you have a typo in the parameter name, or you really accessed a different instance variable than filename to use / test it.

+8
source

Make sure your servlet calls super.init (config) in the init method, otherwise it will not work.

+2
source

Make sure you really deployed the correct web.xml. Also check with config.getInitParameterNames() which parameters were found.

+1
source

Overriding the init(config) method is not recommended. Instead, use the provided convenience method init() and execute 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 ()

+1
source

Ur 用 δΊ† "url-pattern", 在 能再 在 servlet η±» 名 上 加 "@WebServlet", 加 δΊ† 就会 导致 取 "init-param" δΈΊ null.

since "url-pattern" is used, you should not use "@WebServlet" in the servlet class name, so it returns zero

-1
source

Source: https://habr.com/ru/post/1384930/


All Articles