This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Servlets and the fly likes Servlet can't find intialisation paramters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet can Watch "Servlet can New topic
Author

Servlet can't find intialisation paramters

Richard Scothern
Ranch Hand

Joined: May 25, 2001
Posts: 83
Hi All,
I'm having some problems with Tomcat 3.2.3.
I am creating a servlet which uses intialisation parameters called from the init() method in a servlet. I have set up a web.xml file as follows:
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2.2.dtd">

<web-app>
<servlet>
<servlet-name>
ShowMsg
</servlet-name>

<servlet-class>
servlets.ShowMessage
</servlet-class>

<init-param>
<param-name>
message
</param-name>
<param-value>
Dragon!
</param-value>
</init-param>

<init-param>
<param-name>
repeats
</param-name>
<param-value>
5
</param-value>
</init-param>
</servlet>
</web-app>
This is in the directory:
%TOMCAT_HOME%/webapps/root/web-inf/
The class that uses the intialisation paramters is in the directory:
%TOMCAT_HOME%/webapps/root/web-inf/classes/servlets.ShowMessage.java
However, the init method in ShowMessage.java always sets message as null and repeats never gets set.
public void init(ServletConfig config) throws ServletException {
super.init(config);
message = config.getInitParameter("message");
if (message == null) {
message = defaultMessage;
}
try {
String repeatString = config.getInitParameter("repeats");
repeats = Integer.parseInt(repeatString);
} catch(NumberFormatException nfe) {

}
}
I suspect the problem is with my file setup in Tomcat, but I can't figure out what. Should there be a web.xml file in every WEB-INF directory? Do I need to edit server.xml? Any suggestions are appreciated.
Richard Scothern
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Each webapp you deploy should have its own web.xml file. And it should be in the WEB-INF directory of that webapp. This way, each webapp you make can have its own deployment settings.

Your webapp cannot read the web.xml file of a different webapp, so the settings you have under <tomcat-home>/root/WEB-INF will NOT be read by any other webapp.

You shouldn't need to change anything in server.xml as long as you keep with the webapps directory. Anything under webapps has a convenient "auto-loading" supplied by Tomcat.

[This message has been edited by Mike Curwen (edited November 19, 2001).]
Richard Scothern
Ranch Hand

Joined: May 25, 2001
Posts: 83
Thanks for your reply. Just´┐Ż@one more thing,
The webapp under ROOT should use the web.xml also under ROOT, which is where the classes that use web.xml are. Can I use ROOT as a directory for a webapp.
Thanks,
Richard
chanoch wiggers
Author
Ranch Hand

Joined: May 24, 2001
Posts: 245
if you want to use ROOT you can go ahead and do so. this sometimes can simplify your life. All your URLs will simply be in localhost:8080/ without a webapp name. Note though, that the root web applicatino does not have su privilidges so you can still not have cross context references within your code.
------------------
Chanoch Wiggers
Architect for Professional Java Mobile Programming


chanoch<p><a href="http://www.amazon.com/exec/obidos/ASIN/1861007736/" target="_blank" rel="nofollow">Author of Professional Apache Tomcat</a></p>
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Servlet can't find intialisation paramters