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()
?
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.
<?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>
getServletContext().getInitParameter(“email”);
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.