java - How to add Context Parameters in web.xml in Eclipse? -


i using eclipse mars(version: mars release (4.5.0)).

basically developing small web application has login form , link goes registering new user.

here supposed interact database couple of times verifying login details , registering new user details.

so want add database details(like driver,url,username,password) in context parameters in web.xml avoid hardcoding ? how supposed that?thank you.

web.xml not elegant way keep kind of data login/password/driver etc.

there few approaches implement kind data.

jndi approach
configuration depends on web container/server using right now. mentioned simple web application propably using tomcat here please refer more details , example: tomcat , jndi

configuration stored in property file approach
need store these data in property file called example db.properties

#properties structure db.url=your_db_url db.driver=your_db_driver db.login=your_db_login db.password=your_db_password 

this file should placed in src/resources catalog. after making webapp package should in web-inf/classes path of web application

db.properties should load somehow in code example:

properties prop = new properties(); inputstream input = null;  try {      input = new fileinputstream("db.properties");      // load properties file     prop.load(input);      // property value , print out     string url = prop.getproperty("db.url");     string driver = prop.getproperty("db.driver");     string login = prop.getproperty("db.login");     string password = prop.getproperty("db.password");      //your code connect db } catch (ioexception ex) {     ex.printstacktrace(); } {     if (input != null) {         try {             input.close();         } catch (ioexception e) {             e.printstacktrace();         }     } } 

more approaches can defined depends on framework actualy working.

handle maybe guideline not step-by-step. way use.


Comments