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.
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
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
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
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
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.
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
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 ]
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'm sure glad that he's gone. Now I can read this tiny ad in peace!
SKIP - a book about connecting industrious people with elderly land owners