Getting the init parameters in a servlet
Asked Answered
T

2

30

I am new to servlets. I got the init parameters in DD within the init() method using getInitParameter("name"). I tried a lot from within doGet() method to access the init parameters, but it always returns null.

I tried with

getServletContext().getInitParametr("name")

and with

getServletConfig().getInitParametr("name")

but they all return null. Can I get the init parameters in the doGet()?

To answered 2/2, 2013 at 18:27 Comment(1)
there's no reason this.getInitParameter() returns something else when called from doGet() than what it returns when called from init(). Maybe you had a typo in your parameter name.Dyke
B
74

The answer is - Yes, you can.

OK, besides the JB Nizet's comment here are a few suggestions.

1) Have you added your init parameters while the Web Container / Application Server was running?

Quote from "Head First Servlets & JSP: Passing the Sun Certified Web Component Developer Exam":

The servlet init parameters are read only ONCE - when the Container initializes the servlet. ...
When the Container makes a servlet, it reads the DD and creates the name/value pairs for the ServletConfig. The Container never reads the init parameters again! Once the parameters are in the ServletConfig, they won’t be read again until/unless you redeploy the servlet.


2) There are two types of init parameters available. Another quote from "Head First Servlets and JSP" (emphasis mine):

There are context init parameters (defined in <context-param> element) and servlet init parameters (defined in <init-param> element). They are both referred to as init parameters, although defined in different elements.

  • Context init parameters are available to any servlet or JSP that are part of the current web app.

  • Servlet init parameters are available to only the servlet for which the <init-param> was configured.

  • Context init parameters are defined within the <web-app> element.

  • Servlet init parameters are defined within the <servlet> element for each specific servlet.


Example:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

    <display-name>Servlet testing app</display-name>

    <!-- This is a context init parameter -->
    <context-param>
        <param-name>email</param-name>
        <param-value>[email protected]</param-value>
    </context-param>

    <servlet>
        <servlet-name>Info Servlet</servlet-name>
        <servlet-class>com.example.InfoServlet</servlet-class>
        <!-- This is a servlet init parameter -->
        <init-param>
            <param-name>name</param-name>
            <param-value>John Doe</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>Info Servlet</servlet-name>
        <url-pattern>/test/ShowInfo.do</url-pattern>
    </servlet-mapping>

</web-app>


  • Accessing context init parameter in a servlet:
    getServletContext().getInitParameter(“email”);
  • Accessing servlet init parameter in a servlet for which it was defined in the deployment descriptor:
    getServletConfig().getInitParameter("name");

An alternative way of getting servlet init parameter is using a method defined in the abstract class GenericServlet:
public String getInitParameter(String name);
This method is supplied for convenience. It gets the value of the named parameter from the servlet's ServletConfig object.

And there is also Enumeration<String> getInitParameterNames() method for both ServletContext and ServletConfig to get all init parameters.

Birch answered 3/2, 2013 at 21:57 Comment(1)
Bang-on! Great explanation. I was refactoring some code, and this issue started happening. Accidentally replaced a getServletConfig() with getServletContext(), and didn't realize that they have different sets of init-parameters.Disorientate
P
9

if you have overrided the default init() method, make sure that you pass Servlet config parameter to it and also call the super init method . cause if you dont do that , there no way that your code can find your servlet configuration.

here is the code for servlet init() code:

   public void init(ServletConfig config) throws ServletException {
    super.init(config);
    // Rest of your code ...
    }

also i noticed that you used Servlet version 3, i am not sure if it supports defining servlet tags, so if the above solution dosen work , try to remove web-app attributes too.

Perspiration answered 31/5, 2014 at 11:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.