wood burning stoves 2.0*
The moose likes Servlets and the fly likes ServletConfig getInitParameter() ???? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "ServletConfig getInitParameter() ????" Watch "ServletConfig getInitParameter() ????" New topic
Author

ServletConfig getInitParameter() ????

preeti dengri
Ranch Hand

Joined: Nov 30, 2000
Posts: 111
Hi all,
This is just a snippet of making a mailing list servlet
public void init(ServletConfig config) throws ServletException
{
super.init(config);
filename = config.getInitParameter("addressfile");
if(filename == null)
throw new UnavailableException(this, "The \"addressfile\" property "+"must be set to a file name");
}
what does a ServletConfig object does and what is the addressfile parameter in case of tomcat server???
Karthik Guru
Ranch Hand

Joined: Mar 06, 2001
Posts: 1209
There needs to be an entry like this in your
web.xml file (under <your app directory>/WEB-INF.)
<init-param>
<param-name>addressfile</param-name>
<param-value>your address</param-value>
</init-param>
tomcat w'd pick up the init parameter corresponding to
"addressfile" in your case
karthik.
meenu gupta
Greenhorn

Joined: May 10, 2001
Posts: 3
A servlet configuration object used by a servlet container used to pass information to a servlet during initialization.
The configuration information contains initialization parameters, which are a set of name/value pairs, and a ServletContext object, which gives the servlet information about the server.

preeti dengri
Ranch Hand

Joined: Nov 30, 2000
Posts: 111
Thanks Kartik n meenu,
there is still a confusion,Kartik --Am i supposed to create some file (say a text file )to be mapped with the name addressfile as u mentioned
<param-value>your address</param-value>
or it will be itself created bcoz in my program that is supposed to be file to store email ids.
and where in the web.xml i have to insert that???
I did try it this way (the below is just a part of the web.xml)
<web-app>
<servlet>
<servlet-name>
snoop
</servlet-name>
<servlet-class>
SnoopServlet
</servlet-class>
<!--
<init-param>
<param-name>foo</param-name>
<param-value>bar</param-value>
</init-param>

-->
<init-param>
<param-name>addressfile</param-name>
<param-value>address.txt</param-value>
</init-param>
</servlet>
the error i am receiving is 503
the "addressfile" prop must be set to a filename
please help
thank you
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Your web.xml seems to be fine. This is how we have to edit and insert init-params. Did you try to printout the value for "addressfile" param in your servlet code? What's printed?
Here is another related discussion with sample code. http://www.javaranch.com/ubb/Forum7/HTML/001790.html
regds
maha anna
preeti dengri
Ranch Hand

Joined: Nov 30, 2000
Posts: 111
Thanks maha for providing a helpful link but it couldn't solve my problem.
i am still getting the same error 503
i want to know
1) if we donot set the servlet name,class,mapping tags in web.xml,can we call getInitParameter() at all??? (bcoz my other servlets are running fine,even without making any changes in web.xml, but they don't need init parameters ,of course),i am calling my servlets with the same name as servlet class
to quote
"if no match at all in web.xml, then the default ../basic/servlet/SnoopServlet will be invoked. In this case no way to send init params."
i tried this by changing the servlet-name in web.xml and making other changes as follows
<servlet>
<servlet-name>
ListManager
</servlet-name>
<servlet-class>
ListManagerServlet
</servlet-class>
<init-param>
<param-name>addressfile</param-name>
<param-value>address.txt</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ListManager</servlet-name>
<url-pattern>/ListManagerServlet</url-pattern>
</servlet-mapping>
But the same error is coming of "the addressfile prop must be set to a filename".
2) i am having a little confusion over when we need to change the server.xml file???
please help
regards
preeti
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
How did you invoke your servlet? Did you call straight from browser by typing one of the following?
http://localhost/yourWebAppnName/ListManagerServlet ? OR
http://localhost:8080/yourWebAppnName/ListManagerServlet
Try this. Give 2 more set of init-params like this and try to get them from servlet code. Also please do one more thing. Just comment off all your code inside doGet(...) method and put this following code inside your doGet(...) method of ListManagerServlet.java code. Which servlet container are you using? If it is Tomcat, complile .java file and place the new .class file and stop tomcat server and restart it again.
About your other doubts, we need to put an entry in web.xml if we want to send init params. If you would like to use the standard way like /servlet/TestManagerServlet , then put this "/servlet/TestManagerServlet/" for <url-pattern> tag.
servlet.xml is mainly used to change the default settings for server. For example, to change 'auto reload' for your servlet-context to be true. Another example is to change your document root of your appln to some other physical disk location. servler.xml is not used to send init-params.
regds
maha anna



[This message has been edited by maha anna (edited May 12, 2001).]
preeti dengri
Ranch Hand

Joined: Nov 30, 2000
Posts: 111
Hi maha,
now the servlet(with the codegiven by you,rest commented) is working by typing http://localhost:8080/examples/servlet/ListManagerServlet
in the browser.i am usin Tomact as my servlet container
i added the following lines to the web.xml
[code]
<servlet>
<servlet-name>
ListManagerServlet
</servlet-name>
<servlet-class>
ListManagerServlet
</servlet-class>
<init-param>
<param-name>name</param-name>
<param-value>preethi</param-value>
</init-param>
<init-param>
<param-name>addressfile</param-name>
<param-value>address.txt</param-value>
</init-param>
<init-param>
<param-name>forum</param-name>
<param-value>ServletsAndJsp</param-value>
</init-param>
</servlet>
[code]
and the ouput is
Snoop Servlet
Servlet init parameters:
forum = ServletsAndJsp
name = preethi
addressfile = address.txt
there are few queries regarding this
1)why are parameters printed in a different sequence?
2)why are other servlets (not involving init parameters)are running successfully even when i am not touching the already existing web.xml(no new <servlet> tag added neither a <servlet-mapping> tag)?
3)the above code does not work if i omit giving a <servlet-name> tag or <servlet-mapping> i.e i tried it with just the init params in <servlet>(didn't work)??
4)where does the directory "servlets" under examples dir mapped for "servlet" in the path for any such servlet to be executed in case of tomcat??
Hope u read this all,i know it has gone a bit too lengthy.
regards
preeti
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by preeti dengri:
1)why are parameters printed in a different sequence?
2)why are other servlets (not involving init parameters)are running successfully even when i am not touching the already existing web.xml(no new <servlet> tag added neither a <servlet-mapping> tag)?
3)the above code does not work if i omit giving a <servlet-name> tag or <servlet-mapping> i.e i tried it with just the init params in <servlet>(didn't work)??
4)where does the directory "servlets" under examples dir mapped for "servlet" in the path for any such servlet to be executed in case of tomcat??

1) The sequence is simply not preserved. Behind the scenes, all the init parameters and run-time attributes you find in various places in the servlet engine are likely to be HashMaps which do not preserve an order (in fact, they work by virtue of being as unordered as possible).
2) I couldn't say without examining the set-up. But for anything to execute on a servlet engine, it first has to be mapped in some way. Even JSPs, which appear to "just work" without mapping them, only work because the .jsp extension itself is mapped.
3) Without those the servlet engine wouldn't know with which servlet to associate the parameters and/or where to map the servlet.
4) I'm willing to bet your web-app is mapped to /servlet. A web-app (web application) is a collection of servlets, JSPs, supporting Java classes, HTML files and other resources, with a deployment descriptor (WEB-INF/web.xml) telling the servlet engine what goes where. Web-apps can be packaged in .war files. The web application as a whole is associated with a virtual path on the web server; in your case that seems to be /servlet. I don't think Sun specified the configuration file which maps web-apps, and I'm not familiar enough with Tomcat to tell you where to find it.
- Peter

[This message has been edited by Peter den Haan (edited May 13, 2001).]
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Preethi,
Peter has explained it well. Thanks Peter. Since I already composed this post, didn't want to waste. So here is some add-ons.

1)why are parameters printed in a different sequence?
Inside the servlet engine init params are kept as HashTables. So when we take an Enumeration of keys/values, the order is not kept.
2) Why are other servlets (not involving init parameters)are running successfully even when i am not touching the already existing web.xml(no new <servlet> tag added neither a <servlet-mapping> tag)?
It is not a must that we should use web.xml for all our servlets. web.xml is just a holder file for runtime configuration params like init-param, which is startup-servlet etc. if we don't need to configure any params, then no need to edit web.xml. Our servlets will still work.
There is a standard (default) way , (which will always work) to map servlets.
http://yourhost/servlet/servletName
Here "/servlet" is a 'magic' part which is directly mapped to .../WEB-INF/classes directory under our deployment dir. In other words, /servlet/ListManagerServlet means a ListManager.class which is under .../WEB-INF/classes/ dir.This is DEFAULT (work always) RULE used and specified in Servlet specifications. No other web.xml's URI/servlet-mapping necessary.
If we need extra decoration only, we need to edit web.xml. For example, you may have aservlet which could have been kept inside a very looooooong package like com.yourCompany.yourDept.yourProject.ListManagerServlet
If we apply the dafault rule ,we may have to invoke from browser like this.
http://localhost:8080/AppName/servlet/com.yourCompany.yourDept.yourProject.ListMa nagerServlet
which is hard to type as well as difficult to remember. In these situations, we go for editing web.xml and do <servlet-mapping> work. Here we can specify whatever fancy URL we want for this above loooooong url and off you go!
3) The above code does not work if i omit giving a <servlet-name> tag or <servlet-mapping> i.e i tried it with just the init params in <servlet>(didn't work)??
Yes. It will not work. If at all we want to associate a set of init-params with a servlet we have to specify the URL also. Because /servlet/ListManagerServlet is used as mandatory rule and servlet engine directly calls the servlet under .../WEB-INF/classes dir.
If you want to send those params to /servlet/ListManagerServlet type URL then do this trick.
<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>/servlet/TestServlet</url-pattern>
</servlet-mapping>


4)where does the directory "servlets" under examples dir mapped for "servlet" in the path for any such servlet to be executed in case of tomcat??
There is no direct connection between servlets and the magic "/servlet" portion in URL. They are no way related. From Servlet 2.2 spec onwards the directory structure for a web abbplication is defined.

if you take any sample url http://localhost:8080/MISApp/servlet/ListManagerServlet, this portion "/servlet/" is DIRECTLY mapped to ".../WEB-INF/classes/" directory under your webApplication deployment directory.
In "examples" web appln directory under Tomcat they just created a document directory named "servlets" to keep index of all example htmls. Nothing more than that. It could have beed renamed as "servletsList". In this case if you call http://localhost:8080/examples/servletsList/index.html , the index.html will be invoked.
I am sure most of us would have got this doubt when started with servlets. (Are "servlets" dir and "/servlet" URL portion anyway realted ?)
regds
maha anna

[This message has been edited by maha anna (edited May 13, 2001).]
preeti dengri
Ranch Hand

Joined: Nov 30, 2000
Posts: 111
Thanx a bunch maha and peter for taking time to explain it so clearly and deeply.
Thanks again
preeti
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ServletConfig getInitParameter() ????
 
Similar Threads
is not available
Servlet init parameters
doubt in servlet init method
Please try to execute this???
Path as initialization parameter value