how to read values from web.xml using getInitParameter()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • momotaro
    Contributor
    • Sep 2006
    • 357

    how to read values from web.xml using getInitParameter()

    Hi,
    AM trying to read some values from my web.xml file using the
    getInitParamete r method but the application crushes.
    here is the web.xml and the servlet:
    Code:
    public Connection getConnection() {
    
    		String url  = null;
    		String username = null;
    		String password = null;
    		
    		System.out.println(url);
    		
    		url = getInitParameter("url");
    		username = getInitParameter("username");
    		password = getInitParameter("password");
    		try {
    			
    			Class.forName(getInitParameter("jdbc.driver"));
    			con = (Connection) DriverManager.getConnection(url, username, password);
    		} catch (Exception e) {
    			System.out.println("impossible de se connecter a la base de données !");
    		}
    		return con;
    	}
    Code:
    java.lang.NullPointerException
    	javax.servlet.GenericServlet.getInitParameter(GenericServlet.java:82)
    	com.ayach.tp3.DBConnection.getConnection(DBConnection.java:62)
    	com.ayach.tp3.Formulaire.doGet(Formulaire.java:40)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    If you defined the parameters global in your web.xml and not for a particular servlet then you should use getServletConte xt().getInitPar ameter("url") to retrieve the parameter values.

    Comment

    Working...