Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize a Servlet during startup with parameters?

Tags:

Can we write an argument constructor in a Servlet? If yes, how can you call?

like image 517
Biju CD Avatar asked Aug 14 '09 04:08

Biju CD


People also ask

How do you set servlet to initial parameters?

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.

Is used for initializing the servlet parameters?

The init-param sub-element of servlet is used to specify the initialization parameter for a servlet.

How would you pre initialize 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.


1 Answers

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.

like image 164
dfa Avatar answered Sep 29 '22 03:09

dfa