How to get value from init() method in servlet? orhow to get initializaion time values from init() in servlet?

Plz ans !!!!!!!!!!!!!!!!!!!

Showing Answers 1 - 12 of 12 Answers

Kavitha

  • Oct 24th, 2005
 

U can use getInitParameter(java.lang.String name) method of HttpServlet class to get the value of named parameter or getInitParameterNames() to get the names of the servlet's initialization parameters as an Enumeration of string objects.

  Was this answer useful?  Yes

hello

first define variable in your servlet class.

Not in init method.

e.g.

public class ShowMessage extends HttpServlet {

private String message;

private String defaultMessage = "No message.";

private int repeats = 1;

 

now you can use this variable in init method and can change the values and then you can use the same variable in doGet or doPost method.

I hope this will help you

regards,

nilesh kamani

 

  Was this answer useful?  Yes

sprabhala

  • Oct 24th, 2005
 

The servlet container passes the ServletConfig object to the init method . This object contains the configuration values stated in the web.xml file.  Use the getInitParameterNames and getInitParameter methods to retrieve the values.

-Sri

  Was this answer useful?  Yes

Suresh Raju Ch

  • Oct 29th, 2005
 

By using config.getInitparameters() method. u can declare init parameters in web.xml file

  Was this answer useful?  Yes

Guest

  • Nov 1st, 2005
 

The init() method initializes a config variable of ServletConfig object. All the initialization time values can be obtained from this object. It can be accessed like getServletConfig().getFoo()

  Was this answer useful?  Yes

jayaprakash

  • Nov 7th, 2005
 

you need to use the initialization parameter tag using XML

  Was this answer useful?  Yes

Bhupender

  • Nov 9th, 2005
 

You need to set the init parameter in web.xml .

In that file you can initialize the value and passing Servletconfig object

as super(config) where config is the ServletConfig object you can access the initialized value/parameter.

  Was this answer useful?  Yes

shuaib

  • Nov 11th, 2005
 

To get the vlaue in init() method used the getInitparameter() method to extarct the value from XML file.

  Was this answer useful?  Yes

akantilal

  • Jun 12th, 2007
 

I hope you are referring to the init method without parameters. You can use getServletConfig() method which returns the ServletConfig object. On this object, you can invoke the getInitParameter(String name) method to get the value of a single init param or getInitParameterNames() to get the names of all the init params that are passed to this servlet. You can use this logic wherever you need in your servlet. But please remember that if you are also overriding the init method with config object as parameter, do invoke "super.init(config)" in that for the above mentioned idea to work.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions