javaeclipsejava-8javaw

Get initialization parameter from the web.xml file. in Eclipse and Tomcat


I am following this tutorial http://www.javatpoint.com/config-implicit-object

This tutorial uses an file web.xml without a servelet and fill in the variables of the file "welcome.jsp" with the data of the file "web.xml" I put all files in the same folder of Eclipse called "WebContent", but the program don't work. I am a newbie in Eclipse and JSP

WebContent/index.html

<form action="welcome">  
<input type="text" name="uname">  
<input type="submit" value="go"><br/>  
</form>  

WebContent/web.xml

<web-app>  

<servlet>  
<servlet-name>sonoojaiswal</servlet-name>  
<jsp-file>/welcome.jsp</jsp-file>  

<init-param>  
<param-name>dname</param-name>  
<param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>  
</init-param>  

</servlet>  

<servlet-mapping>  
<servlet-name>sonoojaiswal</servlet-name>  
<url-pattern>/welcome</url-pattern>  
</servlet-mapping>  

</web-app>  

WebContent/welcome.jsp

<%   
out.print("Welcome "+request.getParameter("uname"));  

String driver=config.getInitParameter("dname");  
out.print("driver name is="+driver);  
%> 

Solution

  • You have to put web.xml into the WEB-INF directory. That is where the servlet container expects it, otherwise it has no effect. Putting it into WEB-INF also has the "side-effect" of not making the file accessible to users for download through their browser (which would be bad, as it can contain sensitive configuration information).

    So the path should be WebContent/WEB-INF/web.xml.