Forums Register Login

ServletConfig getInitParameter() ????

+Pie Number of slices to send: Send
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???
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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).]
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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).]
+Pie Number of slices to send: Send
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).]
+Pie Number of slices to send: Send
Thanx a bunch maha and peter for taking time to explain it so clearly and deeply.
Thanks again
preeti
joke time: What is brown and sticky? ... ... ... A stick! Use it to beat this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 6296 times.
Similar Threads
Servlet init parameters
doubt in servlet init method
Please try to execute this???
is not available
Path as initialization parameter value
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 11:30:33.