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 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.
Reading Form Data using ServletgetParameter() − You call request. getParameter() method to get the value of a form parameter. getParameterValues() − Call this method if the parameter appears more than once and returns multiple values, for example checkbox.
Initialization parameters are stored in an initialization parameter file and can be applied to all database instances on a server. Parameters that affect system performance involve cursor sharing, the policy that determines work area size, the number of concurrent processes, and memory area sizes. Setting.
To retrieve initialization parameters, call the getInitParameter(String name) method from the parent javax. servlet. GenericServlet class. When passed the name of the parameter, this method returns the parameter's value as a String .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With