servletcontext類中有這么四個(gè)方法:
- getrealpath(string path)
- getresource(string path)
- getresourceasstream(string path)
- getresourcepaths(string path)
這四個(gè)方法都使用web工程下某個(gè)web資源路徑的字符串表現(xiàn)形式作為參數(shù),而每個(gè)方法返回不同的類型,我們通過這四個(gè)方法之一可以獲取某個(gè)資源,并對(duì)其進(jìn)行讀取和修改操作。
假設(shè)我們的【myservlet】web工程中有一個(gè)數(shù)據(jù)庫的配置文件:database.properties,在這個(gè)數(shù)據(jù)庫中已經(jīng)有了一些參數(shù),而我們?cè)趙eb工程中希望讀取這個(gè)配置文件中的有關(guān)信息:
先來看看servletcontext中的getresourceasstream()
方法,這個(gè)方法返回inputstream對(duì)象。由于我們的配置文件為properties文件,所以可以用properties對(duì)象來裝載這個(gè)輸入流,代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { servletcontext context = this .getservletcontext(); inputstream in = context.getresourceasstream( "/database.properties" ); properties prop = new properties(); prop.load(in); string url = prop.getproperty( "url" ); string username = prop.getproperty( "username" ); string password = prop.getproperty( "password" ); system.out.println(url); system.out.println(username); system.out.println(password); } |
最后在瀏覽器中訪問這個(gè)servlet,那么在myeclipse的控制臺(tái)上能看到的數(shù)據(jù)正好是database.properties中我們配置的信息:
接下來看看servletcontext中的getrealpath()
方法,這個(gè)方法返回string對(duì)象。由于我們的配置文件為properties文件,所以可以用properties對(duì)象來裝載這個(gè)輸入流,代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
servletcontext context = this .getservletcontext(); string filepath = context.getrealpath( "/database.properties" ); fileinputstream fis = new fileinputstream(filepath); properties prop = new properties(); prop.load(fis); string url = prop.getproperty( "url" ); string username = prop.getproperty( "username" ); string password = prop.getproperty( "password" ); system.out.println(url); system.out.println(username); system.out.println(password); |
最后在瀏覽器中訪問這個(gè)servlet,那么在myeclipse的控制臺(tái)上能看到的數(shù)據(jù)正好是database.properties中我們配置的信息:
使用getrealpath()方法的好處在于這個(gè)方法還可以獲取文件名,而getresourceasstream()方法就只能獲取文件流了。例如獲取文件名:
1
2
3
4
5
6
7
8
9
|
servletcontext context = this .getservletcontext(); string filepath = context.getrealpath( "/web-inf/web.xml" ); system.out.println(filepath); if (filepath == null ) { system.out.println( "所找文件不存在!" ); } string filename = filepath.substring(filepath.lastindexof( "\\" )); system.out.println( "文件為:" +filename); |
接著來看看servletcontext中的getresource()
方法,這個(gè)方法返回url對(duì)象。而url對(duì)象具有打開到此 url 的連接并返回一個(gè)用于從該連接讀入的 inputstream的openstream()方法。由于我們的配置文件為properties文件,所以可以用properties對(duì)象來裝載這個(gè)輸入流,代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
servletcontext context = this .getservletcontext(); url fileurl = context.getresource( "/database.properties" ); inputstream in = fileurl.openstream(); properties prop = new properties(); prop.load(in); string url = prop.getproperty( "url" ); string username = prop.getproperty( "username" ); string password = prop.getproperty( "password" ); system.out.println(url); system.out.println(username); system.out.println(password); |
最后在瀏覽器中訪問這個(gè)servlet,那么在myeclipse的控制臺(tái)上能看到的數(shù)據(jù)正好是database.properties中我們配置的信息:
以上說完了幾種通過servletcontext對(duì)象來讀取web應(yīng)用下的某個(gè)資源文件,只要通過讀取的方法,并將資源相對(duì)于web工程的路徑作為參數(shù)傳入其中便可。我們上述的例子都是直接在web工程中,或者web工程的某個(gè)目錄下,而如果我們把某個(gè)web資源放置在myeclipse中的【src】目錄中,那么該如何讀取呢:
我們說過,這個(gè)web應(yīng)用在發(fā)布時(shí),會(huì)將【src】目錄下的.java文件編譯成為.class字節(jié)碼文件,由服務(wù)器自動(dòng)將這些字節(jié)碼文件放置在該web應(yīng)用中的【web-inf】下的【classes】目錄里,如果沒有【classes】目錄,服務(wù)器會(huì)自動(dòng)幫我們創(chuàng)建,因此,只要是放置在【src】目錄中的資源,最后也會(huì)被服務(wù)器自動(dòng)放置在【classes】目錄中,這樣我們可以繼續(xù)通過servletcontext對(duì)象來獲取:
1
2
3
4
5
6
7
8
9
10
11
12
|
servletcontext context = this .getservletcontext(); inputstream in = context.getresourceasstream( "/web-inf/classes/database.properties" ); properties prop = new properties(); prop.load(in); string url = prop.getproperty( "url" ); string username = prop.getproperty( "username" ); string password = prop.getproperty( "password" ); system.out.println(url); system.out.println(username); system.out.println(password); |
關(guān)于web工程下某個(gè)web資源在不同位置下的問題:
問題一:我們?yōu)槭裁床荒苡脗鹘y(tǒng)方式,如fileinputstream或者file對(duì)象來直接獲取web工程中的資源呢?其實(shí)也是可以的,但是有個(gè)路徑的問題,servlet中方法所需要的路徑都是相對(duì)于web應(yīng)用的路徑,而傳統(tǒng)的fileinputstream等等中方法所需的路徑參數(shù)都是相對(duì)于虛擬機(jī)的路徑。而又因?yàn)槲疫@個(gè)web應(yīng)用是從myeclipse中的tomcat里啟動(dòng)的,所以這時(shí)候的虛擬機(jī)目錄其實(shí)是tomcat中的【bin】目錄。所以如果想用傳統(tǒng)方式讀取文件必須每次都將文件放置在tomcat的【bin】目錄下, 這是多么麻煩的事,因此我們開發(fā)web工程就應(yīng)該使用web工程中的方法來讀取文件!但是,這卻又引出了問題二。。。
問題二:當(dāng)我們web工程中有別的非servlet的類時(shí),比如javabean,當(dāng)javabean需要連接數(shù)據(jù)庫時(shí),這就是非servlet對(duì)象讀取web工程中的資源文件了,不能用servletcontext來讀取,問題一種也說過不能用傳統(tǒng)方式如fileinputstream來讀取,那么該如何讀取呢?
答案是:類加載器!由于在【src】目錄下的java程序經(jīng)過編譯成字節(jié)碼class文件,如果要用到這些類,java虛擬機(jī)需要先將這些字節(jié)碼文件加載到內(nèi)存中才可以使用,而這個(gè)過程就是由類加載器來完成。因此這就有一個(gè)知識(shí)點(diǎn),如果我們將某個(gè)web資源放置在【src】目錄下,因?yàn)檫@是個(gè)web工程,服務(wù)器會(huì)自動(dòng)將各個(gè)字節(jié)碼文件重新放置在【classes】目錄下, 而這個(gè)web資源也會(huì)重新被服務(wù)器放置在【classes】目錄下,那么類加載器能加載【classes】目錄下所有的字節(jié)碼文件,同時(shí),同處在這個(gè)目錄下的web資源也會(huì)被類加載器加載進(jìn)內(nèi)存,這時(shí)我們就可以使用類加載器讀取該web資源了。
例:在【myservlet】的dao包中創(chuàng)建一個(gè)student的javabean對(duì)象,并在src【目錄下】創(chuàng)建一個(gè)student的配置文件student.properties,而這個(gè)配置文件內(nèi)容如下圖所示:
在student類中,我們需要通過類加載器來獲取輸入流來讀取這個(gè)文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public class student { public void getstudent() throws ioexception { classloader loader = student. class .getclassloader(); inputstream in = loader.getresourceasstream( "student.properties" ); properties prop = new properties(); prop.load(in); string studentname = prop.getproperty( "name" ); string studentage = prop.getproperty( "age" ); system.out.println(studentname+ ":" +studentage); } } |
另外創(chuàng)建一個(gè)servlet作為可以供瀏覽器訪問的對(duì)象,在該servlet中創(chuàng)建student的示例來獲取配置文件中的內(nèi)容,這樣就達(dá)到了從非servlet對(duì)象讀取web資源內(nèi)容并向servlet對(duì)象傳遞數(shù)據(jù):
1
2
3
4
5
6
7
8
|
public class servletdemo extends httpservlet { public void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { student student = new student(); student.getstudent(); } } |
從瀏覽器中訪問該servlet,可以看到通過類加載器讀取的配置文件中的內(nèi)容:
注意,這種方法只能是web資源放置在【src】目錄中才可以使用,如果要讀取的web資源是放置在web工程的目錄下,使用類加載器也還是無法讀取,因?yàn)轭惣虞d器只能讀取類目錄下的文件,這時(shí)候非servlet類就無法讀取資源文件,只能使用servletcontext來讀取了。
方立勛老師說:“類加載器只能加載【classes】目錄下的所有文件一次,這樣在服務(wù)器運(yùn)行web工程的過程中,如果我們修改【classes】目錄下的student.properties配置文件,則由于類加載器不再加載,因此使用類加載器的方式不能讀取修改后的內(nèi)容”
但是我修改后,還是可以使用類加載器的方式讀取classes】目錄下修改后的student.properties配置文件,難道是因?yàn)閖dk7的原因嗎?
不過不管是什么原因,方立勛老師針對(duì)他的問題所采取的解決方案還是值得學(xué)習(xí)的,他采用先用類加載器獲取該配置文件的路徑,然后再采用傳統(tǒng)方式獲取這個(gè)文件的輸入流。所以在student中的getstudent()方法代碼改為:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public class student { public void getstudent() throws ioexception { classloader loader = student. class .getclassloader(); url fileurl = loader.getresource( "student.properties" ); string filepath = fileurl.getpath(); fileinputstream fis = new fileinputstream(filepath); properties prop = new properties(); prop.load(fis); string studentname = prop.getproperty( "name" ); string studentage = prop.getproperty( "age" ); system.out.println(studentname+ ":" +studentage); } } |
這種方式還有一種好處就是,如果要讀取的文件過大,而之前通過類加載器將大文件加載進(jìn)內(nèi)存就容易導(dǎo)致內(nèi)存溢出,所以還是采用這種方式比較好。
最后再說明一點(diǎn),如果是在非servlet類中采用類加載器獲取【classes】目錄中的資源,方法參數(shù)的路徑只需要是相對(duì)于【src】目錄即可。
補(bǔ)充:使用類加載器加載【classes】目錄中的資源,得到的路徑取決是哪個(gè)虛擬機(jī)(或服務(wù)器)調(diào)用,例如上面的代碼getstudent()方法,如果是在非servlet的類的方法中被調(diào)用,那么就是使用jvm虛擬機(jī),那么得到的資源路徑并不是tomcat的應(yīng)用【webapps】目錄的路徑。因此如果是要為servlet中提供資源,那么非servlet類中獲取資源的方法,請(qǐng)一定要使用servlet來調(diào)用,這樣才能保證得到的資源路徑是在tomcat服務(wù)器下的自己的web應(yīng)用所在目錄中的正確位置。
例如下面的例子,我的myeclipse工作空間在【e】盤,而tomcat服務(wù)器所在路徑為【f】盤:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public class resourceutils { public static void main(string[] args) throws ioexception { getresource(); } @test public static void getresource() throws ioexception { classloader loader = resourceutils. class .getclassloader(); url url = loader.getresource( "student.properties" ); string path = url.getpath(); system.out.println(path); } } |
而資源為student.properties配置文件,放置的位置為【src】目錄下:
這個(gè)是在我的一個(gè)web應(yīng)用中定義的一個(gè)非servlet的普通java類,這個(gè)類無論是用junit測試還是使用main函數(shù),亦或是使用別的非servlet類來調(diào)用getresource方法獲取在web應(yīng)用下【src】目錄中的student.properties資源,顯示的路徑為myeclipse的工作空間,而不是tomcat服務(wù)器:
而如果是使用servlet來調(diào)用的話,才是真正顯示在tomcat中web應(yīng)用所在的地方:
1
2
3
4
5
6
7
8
|
public class servletdemo extends httpservlet { public void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { resourceutils.getresource(); } } |
因此在使用web工程中,如果使用非servlet類來獲取資源,請(qǐng)一定注意這個(gè)資源路徑問題!!!