web資源如何利用ServletContext類進(jìn)行獲取?很多新手對此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

ServletContext類中有這么四個(gè)方法:
getRealPath(String path)
getResource(String path)
getResourceAsStream(String path)
getResourcePaths(String path)
這四個(gè)方法都使用web工程下某個(gè)web資源路徑的字符串表現(xiàn)形式作為參數(shù),而每個(gè)方法返回不同的類型,我們通過這四個(gè)方法之一可以獲取某個(gè)資源,并對其進(jìn)行讀取和修改操作。
假設(shè)我們的【myservlet】web工程中有一個(gè)數(shù)據(jù)庫的配置文件:database.properties,在這個(gè)數(shù)據(jù)庫中已經(jīng)有了一些參數(shù),而我們在web工程中希望讀取這個(gè)配置文件中的有關(guān)信息:

先來看看ServletContext中的getResourceAsStream()方法,這個(gè)方法返回InputStream對象。由于我們的配置文件為properties文件,所以可以用Properties對象來裝載這個(gè)輸入流,代碼如下:
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的控制臺上能看到的數(shù)據(jù)正好是database.properties中我們配置的信息:

接下來看看ServletContext中的getRealPath()方法,這個(gè)方法返回String對象。由于我們的配置文件為properties文件,所以可以用Properties對象來裝載這個(gè)輸入流,代碼如下:
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的控制臺上能看到的數(shù)據(jù)正好是database.properties中我們配置的信息:

使用getRealPath()方法的好處在于這個(gè)方法還可以獲取文件名,而getResourceAsStream()方法就只能獲取文件流了。例如獲取文件名:
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對象。而URL對象具有打開到此 URL 的連接并返回一個(gè)用于從該連接讀入的 InputStream的openStream()方法。由于我們的配置文件為properties文件,所以可以用Properties對象來裝載這個(gè)輸入流,代碼如下:
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的控制臺上能看到的數(shù)據(jù)正好是database.properties中我們配置的信息:

以上說完了幾種通過ServletContext對象來讀取web應(yīng)用下的某個(gè)資源文件,只要通過讀取的方法,并將資源相對于web工程的路徑作為參數(shù)傳入其中便可。我們上述的例子都是直接在web工程中,或者web工程的某個(gè)目錄下,而如果我們把某個(gè)web資源放置在MyEclipse中的【src】目錄中,那么該如何讀取呢:

我們說過,這個(gè)web應(yīng)用在發(fā)布時(shí),會將【src】目錄下的.java文件編譯成為.class字節(jié)碼文件,由服務(wù)器自動將這些字節(jié)碼文件放置在該web應(yīng)用中的【W(wǎng)EB-INF】下的【classes】目錄里,如果沒有【classes】目錄,服務(wù)器會自動幫我們創(chuàng)建,因此,只要是放置在【src】目錄中的資源,最后也會被服務(wù)器自動放置在【classes】目錄中,這樣我們可以繼續(xù)通過ServletContext對象來獲取:
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對象來直接獲取web工程中的資源呢?其實(shí)也是可以的,但是有個(gè)路徑的問題,Servlet中方法所需要的路徑都是相對于web應(yīng)用的路徑,而傳統(tǒng)的FileInputStream等等中方法所需的路徑參數(shù)都是相對于虛擬機(jī)的路徑。而又因?yàn)槲疫@個(gè)web應(yīng)用是從MyEclipse中的Tomcat里啟動的,所以這時(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對象讀取web工程中的資源文件了,不能用ServletContext來讀取,問題一種也說過不能用傳統(tǒng)方式如FileInputStream來讀取,那么該如何讀取呢?
答案是:類加載器!由于在【src】目錄下的Java程序經(jīng)過編譯成字節(jié)碼class文件,如果要用到這些類,Java虛擬機(jī)需要先將這些字節(jié)碼文件加載到內(nèi)存中才可以使用,而這個(gè)過程就是由類加載器來完成。因此這就有一個(gè)知識點(diǎn),如果我們將某個(gè)web資源放置在【src】目錄下,因?yàn)檫@是個(gè)web工程,服務(wù)器會自動將各個(gè)字節(jié)碼文件重新放置在【classes】目錄下, 而這個(gè)web資源也會重新被服務(wù)器放置在【classes】目錄下,那么類加載器能加載【classes】目錄下所有的字節(jié)碼文件,同時(shí),同處在這個(gè)目錄下的web資源也會被類加載器加載進(jìn)內(nèi)存,這時(shí)我們就可以使用類加載器讀取該web資源了。
例:在【myservlet】的dao包中創(chuàng)建一個(gè)Student的JavaBean對象,并在src【目錄下】創(chuàng)建一個(gè)student的配置文件student.properties,而這個(gè)配置文件內(nèi)容如下圖所示:

在Student類中,我們需要通過類加載器來獲取輸入流來讀取這個(gè)文件:
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作為可以供瀏覽器訪問的對象,在該Servlet中創(chuàng)建Student的示例來獲取配置文件中的內(nèi)容,這樣就達(dá)到了從非Servlet對象讀取web資源內(nèi)容并向Servlet對象傳遞數(shù)據(jù):
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的原因嗎?
不過不管是什么原因,方立勛老師針對他的問題所采取的解決方案還是值得學(xué)習(xí)的,他采用先用類加載器獲取該配置文件的路徑,然后再采用傳統(tǒng)方式獲取這個(gè)文件的輸入流。所以在Student中的getStudent()方法代碼改為:
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ù)的路徑只需要是相對于【src】目錄即可。
補(bǔ)充:使用類加載器加載【classes】目錄中的資源,得到的路徑取決是哪個(gè)虛擬機(jī)(或服務(wù)器)調(diào)用,例如上面的代碼getStudent()方法,如果是在非Servlet的類的方法中被調(diào)用,那么就是使用JVM虛擬機(jī),那么得到的資源路徑并不是Tomcat的應(yīng)用【webapps】目錄的路徑。因此如果是要為Servlet中提供資源,那么非Servlet類中獲取資源的方法,請一定要使用Servlet來調(diào)用,這樣才能保證得到的資源路徑是在Tomcat服務(wù)器下的自己的web應(yīng)用所在目錄中的正確位置。
例如下面的例子,我的MyEclipse工作空間在【E】盤,而Tomcat服務(wù)器所在路徑為【F】盤:
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)用所在的地方:
public class ServletDemo extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ResourceUtils.getResource();
}
}看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。
網(wǎng)站題目:web資源如何利用ServletContext類進(jìn)行獲取-創(chuàng)新互聯(lián)
標(biāo)題路徑:http://chinadenli.net/article26/shecg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、外貿(mào)建站、小程序開發(fā)、定制開發(fā)、手機(jī)網(wǎng)站建設(shè)、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容