码迷,mamicode.com
首页 > 移动开发 > 详细

jsp学习笔记 - 内置对象 application

时间:2017-09-13 11:56:54      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:操作   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

标签:操作   close   end   contex   对象   iter   attribute   limit   let   

原文地址:http://www.cnblogs.com/lysforwin/p/7514026.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!