標籤:操作 close end contex 對象 iter attribute limit let
---恢複內容開始---
1.application一般用this.getServletContext()替代
2.appllication有一個非常有用的函數 getRealPath(),擷取絕對路徑,以便實現jsp的檔案操作
String fileName = this.getServletContext().getRealPath("/") + "note" + File.separator + name;
3.儲存檔案用 PrintStream類對象
File file = new File(fileName);
if (!file.getParentFile().exists()){
file.getParentFile().mkdir();
}
PrintStream ps = null;
ps = new PrintStream(new FileOutputStream(file));
ps.println(fileContent);
ps.close();
3.讀取檔案用Scanner類對象
Scanner scan = new Scanner(new FileInputStream(file));
scan.useDelimiter(";");
StringBuffer buf = new StringBuffer();
while(scan.hasNext()){
buf.append(scan.next()).append("<br>");
}
scan.close();
<%=buf%>
4.擷取伺服器環境屬性
<%
Enumeration enu = this.getServletContext().getAttributeNames() ;// 取得全部的屬性
while(enu.hasMoreElements()){
String name = (String) enu.nextElement() ;
%>
<h4><%=name%> --> <%=this.getServletContext().getAttribute(name)%></h4>
<%
}
%>
jsp學習筆記 - 內建對象 application