Can we write an argument constructor in a Servlet? If yes, how can you call?
You can specify initial parameter values for servlets in web modules during or after installation of an application onto a WebSphere® Application Server deployment target. The <param-value> values specified in <init-param> statements in the web. xml file of web modules are used by default.
The init-param sub-element of servlet is used to specify the initialization parameter for a servlet.
Some ways to preinitialize a servlet are: To use the tag non-zero-integer with the deployment descriptor web. xml which loads and initialises the servlet when the server starts. 3. To load a servlet in order of number specified.
Can we write an argument constructor in a Servlet?
Yes, you can but it is useless since the servlet container won't invoke it.
The proper way to do it is to use the init()
method:
@Override public void init() throws ServletException { String foo = getInitParameter("foo"); String bar = getServletContext().getInitParameter("bar"); // ... }
In this example, getInitParameter("foo")
returns the value of the <init-param>
of the specific <servlet>
entry in web.xml
, and getServletContext().getInitParameter("bar")
returns the value of the independent <context-param>
in web.xml
.
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