Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

need Help in getInitParameter()

 
June
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I have a problem when calling config.getInitParameter(s), it always returns null when I start my servlet.
I appreciate your time and your help!

Part of my web.xml:
<web-app>
<init-param>
<param-name>xxx.configLocation</param-name>
<param-value>D:\xxx\cfg</param-value>
</init-param>
<init-param>
<param-name>xxx.logLocation</param-name>
<param-value>D:\xxx\log</param-value>
</init-param>
</web-app>
part of my code:
public void init(ServletConfig config) throws ServletException
{
super.init(config);
String s = config.getInitParameter("test"); // I don't need config here since the super.init(config) was called but just to be sure.
}

------------------
JQ
 
ben rowe
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I somewhat new at Java but I don't see where there is a parameter called "test", which means null would be returned.
There's a method getInitParameterNames which returns all the names. Are you sure there's a parameter called "test"?
 
June
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
It can be anything that you define in the web.xml. It was my fault that I just put something there when posting in here. It should be either xxx.configLocation or xxx.logLocation in this case.
So it would be like this:
config.getInitParameter("xxx.configLocation");
In addition, if I call to load my servlet at the time the server starts in web.xml, it returns me the value, then when calling the servlet later on, it returns me null, the init() is called again!
I'm using Tomcat stand alone 3.2.3.
Anybody knows if this is Tomcat bug??
I think the init() is called only once.
Thanks!
 
Phil Hanna
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your web.xml file, did you put the <init-param> blocks inside a <servlet> block?
------------------
Phil Hanna
Sun Certified Programmer for the Java 2 Platform
Author of :
JSP: The Complete Reference
Instant Java Servlets
 
June
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Phil,
Yes, I did include the <ini-param> inside the servlet tag, it didn't work, I tried to take that out, didn't work either.
If I load the servlet on startup the value is not null, but as I described, this servlet is called again later, the value is null.

Thanks,

------------------
JQ
 
ben rowe
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm the guy who's somewhat new to Java, but what about making String s a private member variable to hold the initParameter(you'd define it above the init() method):
private String s = "";
public void init(ServletConfig config) throws ServletException
{
super.init(config);
//you won't need the String data type here
s = config.getInitParameter("test"); // I don't need config here since the super.init(config) was called but just to be sure.
__________________________
If this doesn't sound doable, where in your servlet are you referencing this variable?
Ben
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've also had this problem with Tomcat in the past.
Just my 2 bits
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have just found that in the Tomcat test
folder under webapps, there are servlets that
use < init-param > successfully. I would suggest
looking at the webapps/test/WEB-INF/web.xml
file as a reference.
Also, try the URL: http://localhost:8080/test/servlet/servletParam1
regds.
- satya
 
June
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all!!
Ben: your solution was what I thought if nothing can fix the problem. Sometimes, we can't get "short cut" huh? . Thanks!
David and Madhav: I looked at the test that Madhav pointed out. It wasn't the web xml but the servlet itself helped me! If we call getParameterNames() instead of getting individual init param (getInitParam()), then go thru the while loop of the param vector to get the ones we want, it works!! and it's only called once not twice as the getInitParam() is.
Thank you very very much for helping me out!
Phewwwww

------------------
JQ
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic