• Post Reply Bookmark Topic Watch Topic
  • New Topic

Unable to access config value in servlet

 
sahil kumar garg sangrur
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
web.xml file:-


<init-param>
<param-name> n1 </param-name>
<param-value> 100 </param-value>
</init-param>

<init-param>
<param-name> n2 </param-name>
<param-value> 200 </param-value>
</init-param>

servlet file :-

ServletConfig conf=getServletConfig();

String s1=conf.getInitParameter("n1");
String s2=conf.getInitParameter("n2");
out.println("n1 value is " +s1+ " and n2 is " +s2);


it returns the null value for both
please correct me wherer i am wrong i tried a lot
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried removing the excess spaces?
 
shivam singhal
Ranch Hand
Posts: 231
Java Notepad Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi sahil,

you can define <init-param> inside a servlet, which can be accessed from the same servlet like:
<servlet>
<servlet-name>abc</servlet-name>
<servlet-class>abc</servlet-class>

<init-param>
<param-name>n1</param-name>
<param-value>100</param-value>
</init-param>
</servlet>
Access : int x = getInitParameter("n1");

it can be accessed within the same servlet.

In your case you should use <context-param>
<context-param>
<param-name>n1</param-name>
<param-value>100</param-value>
</context-param>

Access : int i = getServletContext.getInitParameter("n1");

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
He should not have to do that to be able to access init params.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!