您可以從您的應用程序(jsp-servlet)內部僅使用/WEB-INF/test.xml
訪問WEB-INF
文件夾中的資源。
<!%@ taglib uri="/WEB-INF/tiles-jsp.tld" prefix="tiles" %>
如果你想讓它對Web用戶可用,那麼它不能被直接訪問。爲了讓用戶直接訪問它,必須有某種接口將文件從WEB-INF文件夾(例如,讀取文件/WEB-INF/test.xml並將其輸出到jsp/servlet中的jsp )
UPDATE
讀取使用一個Servlet使用本文件:
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
StringBuffer strContent = new StringBuffer("");
int ch;
PrintWriter out = response.getWriter();
try {
String path = getServletContext().getRealPath("/WEB-INF/newfile.xml");
File file = new File(path);
FileInputStream fin = null;
try {
fin = new FileInputStream(file);
while ((ch = fin.read()) != -1) {
strContent.append((char) ch);
}
fin.close();
} catch (FileNotFoundException e) {
System.out.println("File " + file.getAbsolutePath()
+ " could not found");
} catch (IOException ioe) {
System.out.println("Exception while reading the file" + ioe);
}
System.out.println("File contents :");
System.out.println(strContent);
} finally {
out.close();
}
}
這將讀取WEB-INF
名爲newfile.xml一個文件,將輸出內容到控制檯。輸出將是這樣的:
文件內容:
<一>
< B> XXXX </B>
</A>
問: 「getServletContext()」方法究竟有什麼問題? – paulsm4
我想從一個靜態塊加載它。 – Don
duplicate [如何從Web檔案的WEB-INF目錄加載資源](http://stackoverflow.com/questions/1108434/howto-load-a-resource-from-web-inf-directory-of-a- web-archive) – user1406062