Application对象是javax.servlet.ServletContext接口的实例化对象,常用的方法有:

NO

方法

描述

1

Public  String getRealPath(String path)

得到虚拟目录的绝对路径

2

Public  String getContextPath()

得到当前的虚拟路径名称

3

Public  Enumeration getAttributeNames()

得到所有属性的名称

一、取得虚拟目录对应的绝对路径(getRealPath(path))

<%@page contentType="text/html" pageEncoding="GBK"%>    application<%    //得到当前目录虚拟下对应的绝对路径    String path = application.getRealPath("/");    //这句等同于上面语句    String pathTemp = this.getServletContext().getRealPath("/");       //得到当前目录虚拟下对应的绝对路径%> 

真实路径:<%=path%>

真实路径2:<%=pathTemp%>

附:尽可能使用this.getServletContext()方法代替application对象。

 

二、简单的文件操作

	
application
输入文件名称:
输入文件内容:
<%@ page contentType="text/html" pageEncoding="GBK"%><%@ page import = "java.io.*"%><%@ page import = "java.util.*"%>	application<%	request.setCharacterEncoding("GBK");	String name = request.getParameter("filename");	String content = request.getParameter("filecontent");	//拼凑文件名 称	String fileName = this.getServletContext().getRealPath("/") + "javawritenote" + File.separator  + name;	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 scan = new Scanner(new FileInputStream(file));	scan.useDelimiter("\n");	StringBuffer buf = new StringBuffer();	while(scan.hasNext()){		buf.append(scan.next()).append("
"); } scan.close();%><%=buf%>

三、网站计数器制作

1.网站来访的人数会累计很多,难以使用基本数据保存,需要用大整数类。

2.用户每次第一次访问时才需要计数,重复刷新页面则不应该重复计数。所以需要使用isNew()函数来判断用户是否是第一访问。

3.web开发属于多线程操作,所以在进行更改、保存时需要进行同步操作。

代码如下:

<%@ page contentType="text/html" pageEncoding="GBK"%><%@ page import = "java.io.*"%><%@ page import = "java.util.*"%><%@ page import = "java.math.*"%>	application<%!	//定义全局变量	BigInteger count = null;	public BigInteger load(File file){		BigInteger countTemp = null;		try{			if(file.exists()){		//如果文件存在,则读取				Scanner scan = null;				//从文件中读取				scan = new Scanner(new FileInputStream(file));				if(scan.hasNext()){		//如果存在内容					//内容存放在bigInteger中					countTemp = new BigInteger(scan.next());				}				scan.close();			}else{				//文件不存在则创建新的,存在第一次访问				countTemp = new BigInteger("1");				save(file, count);		//调用save()方法			}		}catch(Exception e){			e.printStackTrace();		}		return countTemp;	}	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 = new File(fileName);	if(session.isNew()){		//如果是新的session表示请允许进行增加操作		synchronized(this){		//表示进行同步操作			count = load(file);			//自增操作			count = count.add(new BigInteger("1"));			save(file, count);		}	}%>

您是第<%=count==null ? 1 : count%>访客!

四、getAttributeNames()方法使用

<%@ page contentType="text/html" pageEncoding="GBK"%><%@ page import = "java.util.*"%>	application<%	//得到全部属性名称	Enumeration enu = this.getServletContext().getAttributeNames();	while(enu.hasMoreElements()){		String name = (String) enu.nextElement();%>	

<%=name%>--><%=this.getServletContext().getAttribute(name)%>

<% }%>

以上内容参考JAVAWEB开发实战经典(名师讲坛)