This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Servlets and the fly likes How to access init parameter from web.xml in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to access init parameter from web.xml in servlet" Watch "How to access init parameter from web.xml in servlet" New topic
Author

How to access init parameter from web.xml in servlet

berender mavi
Ranch Hand

Joined: Feb 18, 2011
Posts: 31
Hi All,

I am using a init param named as Name in servlet parameter.But when i am running follwing code in doGet



getting null.why ? can you please tell me?

my web.xml is as follows:

one more thing when i am overriding the init() method and using the same code in it.
why am getting 2 times printing for one statement in init().First time i am getting the correct result from web.xml but in second printing it's again null.

Please help.Thanks
Victor M. Pereira
Ranch Hand

Joined: Mar 02, 2012
Posts: 50
If your using the same code in the init, why override it?

The init method sets the servletConfig, you should check if your servletConfig has what is supposed to have.

When you make changes in the DD, you should try to redeploy it. Some servers don't have the automatic hot redeploy.

Try stopping the server, wait 30 seconds and start it again.


regards,
Victor M. Pereira
berender mavi
Ranch Hand

Joined: Feb 18, 2011
Posts: 31
Still getting the same null.

My code is as follows



and output on console is

Victor M. Pereira
Ranch Hand

Joined: Mar 02, 2012
Posts: 50
May sound dumb, but could you try deleting <load-on-startup>1</load-on-startup> line from your DD. Is a running theory bu tjust try it.
Bharat Kasodariya
Ranch Hand

Joined: Aug 19, 2011
Posts: 36
I think you should override
public void init(ServletConfig config) method to get config param.

The code will be like

public void init(ServletConfig config){
// Store the ServletConfig object and log the initialization
super.init(config);

System.out.println("Name is"+config.getInitParameter("Name"));

}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to access init parameter from web.xml in servlet
 
Similar Threads
Starting a JSF 2.0 project with PrimeFaces 2.2. M1 and Glassfish 3.0
how to set domain namin on jboss as 4.0.5
Using Vaadin and Struts 1 together
struts1
ELResolvers not registered with the JSP container