File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes question on getInitParameter(String) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "question on getInitParameter(String)" Watch "question on getInitParameter(String)" New topic
Author

question on getInitParameter(String)

rick collette
Ranch Hand

Joined: Mar 22, 2002
Posts: 208
Hi,
Its says in API:
Returns a String containing the value of the named initialization parameter, or null if the parameter does not exist.
We can get parameters from gery string, I am still not sure where to get this named init parameter. Can anyone shed some light on it by giving an example? Thanks.
Clark Bruce
Greenhorn

Joined: Feb 27, 2002
Posts: 17
Rick,
The initialization parameters are declared inside of the <servlet> tags of the deployment descriptor. An example:
<servlet>
<servlet-name>TheServlet</servlet-name>
<servlet-class> /MyServlet </servlet-class>
<init-param>
<param-name> firstParam </param-name>
<param-value> TheParam </param-value>
</init-param>
</servlet>
So now if you call getInitParam("firstParam"),
it should return "TheParam" as a string(no quotes). Be sure to restart the server after you change the deployment descriptor. Hope this helps.
Regards,
Clark
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
These initial parameters are the J2EE compliant way of initializing the state of a servlet.
There is also an analogous initial parameter for the context. In the ServletContext interface, you can also find a getInitParameter() that behaves the same way, except the name and value of the initial paramter is set in the web.xml using <context-param> tags.
A context init-param is accessible by all servlets/JSPs of the web application, assuming they have a reference to the ServletContext. A servlet init-param, however, is only accessible within its corresponding servlet, unless the developer explicitly takes pains to pass them around as attributes.
-anthony
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
Also keep in mind that the getInitParameter() method is accessible through the ServletConfig interface, these are parameters that are defined as sub-elements of the <servlet> element in the deployment descriptor

craig
rick collette
Ranch Hand

Joined: Mar 22, 2002
Posts: 208
Thanks, guys.
I still have some questions on this.
(1) Where to put web.xml file? Should I put it in
install_dir/webapps/WEB-INF or install_dir/webapps/ROOT/WEB-INF? I tried both, and it did not work.
(2) After I changed web.xml files, I got the following error message when I start up tomcat:
ERROR reading java.io.ByteArrayInputStream@e95a56
At Line 162 /web-app/servlet-mapping/
What is the cause of that?
Thanks
Clark Bruce
Greenhorn

Joined: Feb 27, 2002
Posts: 17
Rick,
The /ROOT/WEB-INF should work. Try just using an empty Deployment Descriptor first.
<web-app>
</web-app>
Make sure your compiled servlet class is placed in /ROOT/WEB-INF/classes directory. Once this is running then you can start to make changes in the web.xml file to see how it works.
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
I believe you said you are using tomcat, so I would first shutdown tomcat and then package the application in a web archive file(WAR), which should include the deployment description, jsp, servlets etc. Then place the myapp.war in the following folder:
c:\%catalina_home%\webapps
Then re-start tomcat, the server will unpackage the WAR file into that directory. Remember your web.xml deployment descriptor for myapp.war will be located in the WEB-INF\web.xml. You should be able to access your application stating
http://"localhost"/myapp/my.jsp
Hope this helps.
Craig
rick collette
Ranch Hand

Joined: Mar 22, 2002
Posts: 208
Originally posted by Clark Bruce:
Rick,
The /ROOT/WEB-INF should work. Try just using an empty Deployment Descriptor first.
<web-app>
</web-app>
Make sure your compiled servlet class is placed in /ROOT/WEB-INF/classes directory. Once this is running then you can start to make changes in the web.xml file to see how it works.

Thank you very much Clark and cj. I got it. Strangely I need to restart not only server and but also a new browser. Somehow, browser will store the previous content.
[ April 05, 2002: Message edited by: rick collette ]
Clark Bruce
Greenhorn

Joined: Feb 27, 2002
Posts: 17
Rick,
Sometimes if you go to Tools, Internet Options, and then click on delete files it will work without having to restart the browser.
Regards,
Clark
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Strangely I need to restart not only server and but also a new browser. Somehow, browser will store the previous content.

Your browser is caching the pages. The last Tomcat I handled was 3.2 , but if I remember it correctly, when you create your own webapp document root, try to set the reloadable attribute to "true" in your server.xml. All your servlet classes will be automatically reloaded by the container whenever it detects a change in the source code at tomcat-root/webapp/webapp-root/WEB-INF/classes, so there's no need to restart.
-anthony
[ April 05, 2002: Message edited by: Anthony Villanueva ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question on getInitParameter(String)