By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,650 Members | 1,493 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,650 IT Pros & Developers. It's quick & easy.

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

momotaro
100+
P: 357
Hi,
AM trying to read some values from my web.xml file using the
getInitParameter method but the application crushes.
here is the web.xml and the servlet:
Expand|Select|Wrap|Line Numbers
  1. public Connection getConnection() {
  2.  
  3.         String url  = null;
  4.         String username = null;
  5.         String password = null;
  6.  
  7.         System.out.println(url);
  8.  
  9.         url = getInitParameter("url");
  10.         username = getInitParameter("username");
  11.         password = getInitParameter("password");
  12.         try {
  13.  
  14.             Class.forName(getInitParameter("jdbc.driver"));
  15.             con = (Connection) DriverManager.getConnection(url, username, password);
  16.         } catch (Exception e) {
  17.             System.out.println("impossible de se connecter a la base de données !");
  18.         }
  19.         return con;
  20.     }
Expand|Select|Wrap|Line Numbers
  1. java.lang.NullPointerException
  2.     javax.servlet.GenericServlet.getInitParameter(GenericServlet.java:82)
  3.     com.ayach.tp3.DBConnection.getConnection(DBConnection.java:62)
  4.     com.ayach.tp3.Formulaire.doGet(Formulaire.java:40)
  5.     javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
  6.     javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
Mar 4 '12 #1
Share this Question
Share on Google+
1 Reply


10K+
P: 13,264
If you defined the parameters global in your web.xml and not for a particular servlet then you should use getServletContext().getInitParameter("url") to retrieve the parameter values.
Mar 5 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.