'context.getInitParameter("") and config.getInitParameter("") always returning null

I am a beginner in servlets and JSP and I've tried my best to get the values yet I am getting null, any help is welcomed:

This is basic code on using ServletConfig and ServletContext to get param-value from web.xml

Servlet_Ex_4.java (- servlet):

@WebServlet("/Servlet_Ex_4")
public class Servlet_Ex_4 extends HttpServlet {

    ServletContext context;
    ServletConfig config;
    String appUser ;
    String Database ;
    
    @Override
    public void init() {
        ServletContext context =getServletContext(); 
        appUser = context.getInitParameter("appUser");
        
        ServletConfig config =getServletConfig();
        Database = config.getInitParameter("Database");
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        
            response.setContentType("text/html"); 
                PrintWriter out = response.getWriter();
                //get ServletContext object.
                //get context parameter from ServletContext object.
         
                out.print("<h1>Application User: " + appUser + "</h1>");
                out.print("<h1>Database: " + Database + "</h1>");
         
                out.close();
            
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<element>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>Servlet_Exercise_4</display-name>
  <servlet>
    <servlet-name>Servlet_4</servlet-name>
    <servlet-class>Servlet_Ex_4</servlet-class>
    <context-param>
        <param-name>Database</param-name>  
        <param-value>Oracle</param-value>  
  </context-param>
  </servlet>
 
  <context-param>
    <param-name>appUser</param-name>  
    <param-value>jai</param-value>  
  </context-param>
 
  <servlet-mapping>
    <servlet-name>Servlet_4</servlet-name>
    <url-pattern>/Servlet_Ex_4</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.jsp</welcome-file>
    <welcome-file>default.htm</welcome-file>
  </welcome-file-list>
</web-app>
</element>


Solution 1:[1]

  • tag <element> must be removed from web.xml because it not part of XML definition in this case

  • <context-param> must not be used inside the <servlet>, it must be replaced by <init-param> to access parameters from ServletConfig object. This is the reason why the OP is getting null value.

  • <context-param> will work for parameter "appUser", it can be accessed using ServletContext object.

  • And mixing annotations with XML for same type of configurations can result in undesirable effects. So, @WebServlet should be removed from the source code as majority of configuration is done using XML.

So final web.xml should like below (with @WebServlet annotation removed from the Servlet_Ex_4 class)

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
id="WebApp_ID" version="4.0">
<display-name>ServletParams</display-name>
<servlet>
<servlet-name>Servlet_4</servlet-name>
<servlet-class>Servlet_Ex_4</servlet-class>
<init-param>
<param-name>Database</param-name>
<param-value>Oracle</param-value>
</init-param>
</servlet>
<context-param>
<param-name>appUser</param-name>
<param-value>jai</param-value>
</context-param>
<servlet-mapping>
<servlet-name>Servlet_4</servlet-name>
<url-pattern>/Servlet_Ex_4</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1