• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to access init parameter from web.xml in servlet

 
berender mavi
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
berender mavi
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still getting the same null.

My code is as follows



and output on console is

 
Victor M. Pereira
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"));

}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic