aspose file tools*
The moose likes Servlets and the fly likes need Help in getInitParameter() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "need Help in getInitParameter()" Watch "need Help in getInitParameter()" New topic
Author

need Help in getInitParameter()

June
Greenhorn

Joined: Aug 16, 2001
Posts: 24
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


JQ
ben rowe
Greenhorn

Joined: Aug 16, 2001
Posts: 5
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

Joined: Aug 16, 2001
Posts: 24
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

Joined: Apr 05, 2001
Posts: 118
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


Phil Hanna<BR>Sun Certified Programmer for the Java 2 Platform<BR>Author of :<BR><A HREF="http://www.amazon.com/exec/obidos/ASIN/0072127686/electricporkchop/107-3548162-1137317" TARGET=_blank rel="nofollow">JSP: The Complete Reference</A><BR><A HREF="http://www.amazon.com/exec/obidos/ASIN/0072124253/electricporkchop/107-3548162-1137317" TARGET=_blank rel="nofollow">Instant Java Servlets</A>
June
Greenhorn

Joined: Aug 16, 2001
Posts: 24
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

Joined: Aug 16, 2001
Posts: 5
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

Joined: Jul 27, 2001
Posts: 1365
I've also had this problem with Tomcat in the past.
Just my 2 bits
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

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

Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
June
Greenhorn

Joined: Aug 16, 2001
Posts: 24
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: need Help in getInitParameter()