Application对象是javax.servlet.ServletContext接口的实例化对象,表示的是Servlet的上下文,ServletContext代表了整个容器的操作。
No | 方法 | 类型 | 描述 |
1 | String getRealPath(String path) | 普通 | 得到虚拟目录对应的绝对路径 |
2 | PublicEnumeration getAttributeNames() | 普通 | 得到所有属性的名称 |
3 | Public String getContextPath() | 普通 | 取得当前虚拟路径名称 |
一、取得绝对路径
如果要想取得一个虚拟目录对应的绝对路径要使用getRealPath()方法
<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
<head><title>欢迎来到望星空</title></head>
<body>
<%
String path = application.getRealPath("/");
%>
<h3>真实路径:<%=path%></h3>
</body>
</html>
application的操作本身是ServletContext接口的实例,但是在JSP中有一个方法的功能可以完全与之对应:getServletContext()方法。String path = getServletContext().getRealPath("/");
正常情况下所有的方法应该都是由一个对象调用,实际上如果非要在这个地方加上一个对戏那个的话,可以使用this,this表示的是当前容器。String path = this.getServletContext().getRealPath("/");(注意:这个方法要重点使用)
样例:
input_content.html
<html>
<head><title>欢迎来到望星空</title></head>
<body>
<form action="input_content.jsp" method="post">
输入文件名称:<input type="text" name="filename"><br>
输入文件内容:<textarea name="filecontent" cols="30" rows="3"></textarea><br>
<input type="submit" value="保存">
<input type="reset" value="重置">
</form>
</body>
</html>
input_content.jsp
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.Scanner"%>
<html>
<head><title>欢迎来到望星空</title></head>
<body>
<%
request.setCharacterEncoding("GBK"); //解决乱码问题
String name = (String)request.getParameter("filename");
String content = (String)request.getParameter("filecontent");
//要想操作文件必须有绝对路径,这时就需要getRealPath()
String fileName = this.getServletContext().getRealPath("/") +
"note" + File.separator + name; //保存在note文件夹之中
File file = new File(fileName); //实例化File类对象
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("<br>");
}
scan.close();
%>
<%=buf%>
</body>
</html>
二、案例讲解:网站计数器
注意:
1.网站的来访人数可能会有很多,有可能超过20位整数,所以必须使用大整数类-----BigInteger完成。
2.用户每次在第一次访问的时候才需要进行计数的操作,在执行计算之前必须使用isNew()判断
3.在进行更改、保存的时候需要进行同步操作。
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.Scanner"%>
<%@ page import="java.math.BigInteger"%>
<html>
<head><title>欢迎来到望星空</title></head>
<body>
<%!
BigInteger count = null;
%>
<%!
public BigInteger load(File file){
BigInteger count = null; //接收数据
try{
if(file.exists()){
Scanner scan = new Scanner(new FileInputStream(file));
if(scan.hasNext()){
count = new BigInteger(scan.next());
}
scan.close();
}else { //应该保存一个新的,从0开始
count = new BigInteger("0");
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 = new File(fileName);
if(session.isNew()){
synchronized(this){
count = load(file); //读取
count = count.add(new BigInteger("1")); //在原本的基础上增加1
save(file, count);
}
}
%>
<h3>您是第<%=count==null?0:count%>位访客</h3>
</body>
</html>
三、查看属性
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.Enumeration"%>
<html>
<head><title>欢迎来到望星空</title></head>
<body>
<%
Enumeration enu = this.getServletContext().getAttributeNames();
while(enu.hasMoreElements()){
String name = (String)enu.nextElement();
%>
<h4><%=name%>---><%=this.getServletContext().getAttribute(name)%></h4>
<%
}
%>
</body>
</html>
通过Tomcat配置的第三方的.jar文件都是通过application属性设置到服务器上去的,所以在每次配置了一个新的开发包的时候,服务器必须重新启动。总结:
1.application表示的是整个上下文的资源环境
2.在实际中可以通过this.getServletContext()方法来代替application的使用
3.通过getRealPath()方法可以取得一个虚拟目录对应的真是路径