aspose file tools*
The moose likes Servlets and the fly likes How can we read initparameter(String t) of a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How can we read initparameter(String t) of a servlet " Watch "How can we read initparameter(String t) of a servlet " New topic
Author

How can we read initparameter(String t) of a servlet

Gaurav Chikara
Ranch Hand

Joined: Jun 09, 2000
Posts: 410
If my code is like this
1. public class UploadServlet extends HttpServlet {
2. private String dirName;
3.
4. public void init(ServletConfig config) throws 5.ServletException {
6. super.init(config);
7. // read the uploadDir from the servlet parameters
8. dirName = config.getInitParameter("uploadDir");
9. if (dirName == null) {
10. throw new ServletException("Please supply uploadDir 11.parameter");
12. }
13. }
14.}
Now If i want to retrive the dirName through HTML form having input field with name =uploadDir and even after supplying values in that field code from line 9 to 11 is executed
Is there ny other way of retrieving the paramaeter .Is my approach wrong .Has anyone done this before ? Or do i have to get it in doPost or doGet only?
If yes will you tell me ?
It is throwing exception


SCJP,SCWCD,SCBCD<br />If Opportunity doesn't knock then build the door
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Gaurav,
Init parameter of a servlet means 'those values supplied by servlet engine when the servlet's code is first loaded into memory. It is one time value!
Generally we supply those parameters which we think should be configured whenever a servet is first time loaded, through .../webapp/web-inf/web.xml file. We edit this file and include <init-param>....</init-param> sub-tags.
If this is what you want to achieve means, a realted discussion thread is here http://www.javaranch.com/ubb/Forum7/HTML/001790.html
Otherwise, if you want to get the 'uploadDir' from users, then retrive it like this.

regds
maha anna
[This message has been edited by maha anna (edited March 22, 2001).]
Gaurav Chikara
Ranch Hand

Joined: Jun 09, 2000
Posts: 410
Hi Maha anna
As per your discussion thread I found out that only way of retrrieving init parameter is through XML (correct me if I am wrong) and moreover It was focussed mainly assuming tomcat into consideration .In My case I am using javawebserver2.0 trial version .once it runs here then I have to map it with Weblogic
Server (for implementation and testing)
Now will u clarify my doubts if possible
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
This is not the only way we can get init parameters of a servlet. In order to deploy our web applications with as less changes as possible across many servlet containers, I use another method. Since many people familier with web.xml method I first posted that.
I use a ***.properties text file and write all the configuration parameters in a standard format and store as
...\yourwebappln\***.properties file
and grab the init parameters by writing a utility method and use it wherever you want. Here is a sample MISApp.properties file and the utility class which I use for your reference.
We need not worry about which servlet container we are going to use at all! It will work anywhere.
regds
maha anna


[This message has been edited by maha anna (edited March 22, 2001).]
lokesh reddy
Ranch Hand

Joined: Sep 15, 2000
Posts: 66
Hi Gaurav,
There is nothing wrong with your program, the only mistake u have done is u r trying to get the parameter from the html page.
As maha anna said the initparameters can be initialized only once in a life cycle of servlet, and the initparameters must be supplied by a servlet engine.
If u r using tomcat then u have to use web.xml file and supply the init parameters in that file.
As u said u r using javawebserver 2.0 trial version, then u have to follow the following steps.
1) Start ur java webserver.
2) Open ur browser.
3) type http://localhost:9090 (This will open admin panel, if u r using this for the first time, then type admin as username and password).
4) Double click on web service. (This will open a dialog box)
5) Select Servlets button from the top.
6) Click on Add and enter ur servlet name and servlet class in the text fields and click ok.
7) Then ur servlet will be listed under configure (Which is under add).
8) Double Click on the servlet name (Under Configure)(This will open a dialog on right hand side).
9) Click on properties tab and click on add button.
10) Enter the parameter name and parameter value in the appropriate boxes.
Thats it u r done with supplying a init parameter to ur servlet.
I hope u find this info useful.
Bye.
Loke.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can we read initparameter(String t) of a servlet