java - Read an xml file from a servlet -


i can't load correctly file xml servlet: that's code:

       try{             documentbuilderfactory dbf = documentbuilderfactory.newinstance();            documentbuilder db = dbf.newdocumentbuilder();            document doc = db.parse("db.xml");        } catch (exception ex) {        ex.printstacktrace();            out.print("file not found!");    } 

the db.xml inside classes folder class , java file...

if have xml file in root folder of war file, can read using real path context application folder.

string contextpath = request.getsession().getservletcontext().getrealpath("/"); 

in way, can use context class loader in multi-module environment:

classloader classloader = thread.currentthread().getcontextclassloader() document doc = db.parse(classloader.getresourceasstream(contextpath+ "/db.xml")); 

in environments, additional slash not necessary.


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -