1,什么是application?
application对象是javax.servlet.ServletContext接口的实例化对象,单从词义上翻译是:servlet上下文;ServletContext代表了整个容器的操作,常用的方法有:
No | 方法 | 类型 | 描述 |
1 | String getRealPath(String path) | 普通 | 得到虚拟目录对应的绝对路径 |
2 | public Enumeration getAttributeNames() | 普通 | 得到所有属性的名称 |
3 | public String getContextPath() | 普通 | 得到当前的虚拟路径名称 |
2,取得虚拟目录对应的绝对路径?
<%String realpath=application.getRealPath("/"); %> <%= path %>
会输出项目的根目录的路径,在使用中应该尽量使用this.getServletContext()来代替applicatioin;
实例:在一个文本输入一些数据,然后让其在项目的根目录下生存文件:
input.jsp:
<form action="inputContent.jsp" method="post"> 输入文件名称:<input type="text" name="fileName"><br /> 输入文件内容:<textarea rows="3" cols="14" name="filecontent"></textarea><br /> <input type="submit" value="提交"> <input type="reset" value="重写"> </form>
执行并显示的页面content.jsp:
<body> <% request.setCharacterEncoding("utf-8"); String name = request.getParameter("fileName"); String content = request.getParameter("filecontent"); //拼凑文件名称 String fileName = this.getServletContext().getRealPath("/")+"note"+File.separator+name; //实例化File类对象 File file = new File(fileName); //判断父文件夹是否存在 if(!file.getParentFile().exists()){ //创建文件夹 file.getParentFile().mkdir(); } //定义打印流对象 PrintStream ps = null; //准备向文件中的保存 ps = new PrintStream(new FileOutputStream(file)); //输出内容 ps.println(content); //关闭输出流 ps.close(); %> <% //使用scanner读取文件 Scanner scan = new Scanner(new FileInputStream(file)); //设置读取分隔符 scan.useDelimiter("\n"); //将所有内容都读取进来 StringBuffer buf = new StringBuffer(); //取出所有数据 while(scan.hasNext()){ //读取内容,保存在StringBuffer类中 buf.append(scan.next()).append("<br>"); } //关闭输出流 scan.close(); %> <%=buf %> </body>
大家可以在tomcat的webapps下的工程下生成一个文件;
实例:网站计数器:
必须注意以下3点:
<body> <%!BigInteger count = null;%> <%!//直接在方法中处理异常,实际应用中应该交给调用处处理 //读取计数文件 public BigInteger load(File file) { //接收读取的数据 BigInteger count = null; try { //如果文件存在,则读取 if (file.exists()) { //定义scanner对象 Scanner scan = null; //从文件中读取 scan = new Scanner(new FileInputStream(file)); //存在内容 if (scan.hasNext()) { //将内容放到BigInteger类中 count = new BigInteger(scan.next()); } //关闭输入流 scan.close(); } else { //第一次访问 count = new BigInteger("0"); //调用save()方法,保存新文件 save(file, count); } } catch (Exception e) { e.printStackTrace(); } //返回读取后的数据 return count; } //保存技术文件 public void save(File file, BigInteger count) { try { //定义输出流对象 PrintStream ps = null; //打印输出流 ps = new PrintStream(new FileOutputStream(file)); //保存属数据 ps.println(count); //关闭 ps.close(); } catch (Exception e) { e.printStackTrace(); } }%> <% //文件路径 String fileName = this.getServletContext().getRealPath("/")+"count.txt"; //定义File类对象 File file = new File(fileName); //如果是新的session表示允许进行操作 if(session.isNew()){ //必须进行线程同步 synchronized(this){ count = load(file); //自动操作 count = count.add(new BigInteger("1")); save(file,count); } } %> <h2>您是第<%=count == null?0:count %>位访客</h2> </body>
查看application范围的属性:
页面必须引入:
<%@page import="java.util.*" %>
<% Enumeration enu = this.getServletContext().getAttributeNames(); while(enu.hasMoreElements()){ String name = (String)enu.nextElement(); %> <h2><%=name %>---><%=this.getServletContext().getAttribute(name) %></h2> <%} %>