java - File path to resource in our war/WEB-INF folder? -


i've got file in war/web-inf folder of app engine project. read in faqs can read file there in servlet context. don't know how form path resource though:

/war/web-inf/test/foo.txt 

how construct path resource use file(), looks above?

thanks

there's couple ways of doing this. long war file expanded (a set of files instead of 1 .war file), can use api:

servletcontext context = getcontext(); string fullpath = context.getrealpath("/web-inf/test/foo.txt"); 

http://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/servletcontext.html#getrealpath(java.lang.string)

that full system path resource looking for. however, won't work if servlet container never expands war file (like tomcat). work using servletcontext's getresource methods.

servletcontext context = getcontext(); url resourceurl = context.getresource("/web-inf/test/foo.txt"); 

or alternatively if want input stream:

inputstream resourcecontent = context.getresourceasstream("/web-inf/test/foo.txt"); 

http://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/servletcontext.html#getresource(java.lang.string)

the latter approach work no matter servlet container use , application installed. former approach work if war file unzipped before deployment.

edit: getcontext() method have implement. jsp pages make available context field. in servlet servletconfig passed servlet's init() method. if store @ time, can servletcontext time want after that.


Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -