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"%>
<html>
<head>
<title>application</title>
</head>
<body>
<%
//得到当前目录虚拟下对应的绝对路径
String path = application.getRealPath("/");
//这句等同于上面语句
String pathTemp = this.getServletContext().getRealPath("/"); //得到当前目录虚拟下对应的绝对路径
%>
<h2>真实路径:<%=path%></h2><br>
<h2>真实路径2:<%=pathTemp%></h2>
</body>
</html>
附:尽可能使用this.getServletContext()方法代替application对象。
二、简单的文件操作
<!doctype html>
<html>
<head>
<meta charset="GBK">
<title>application</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>
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import = "java.io.*"%>
<%@ page import = "java.util.*"%>
<html>
<head>
<title>application</title>
</head>
<body>
<%
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("<br>");
}
scan.close();
%>
<%=buf%>
</body>
</html>
三、网站计数器制作
1.网站来访的人数会累计很多,难以使用基本数据保存,需要用大整数类。
2.用户每次第一次访问时才需要计数,重复刷新页面则不应该重复计数。所以需要使用isNew()函数来判断用户是否是第一访问。
3.web开发属于多线程操作,所以在进行更改、保存时需要进行同步操作。
代码如下:
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import = "java.io.*"%>
<%@ page import = "java.util.*"%>
<%@ page import = "java.math.*"%>
<html>
<head>
<title>application</title>
</head>
<body>
<%!
//定义全局变量
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);
}
}
%>
<h2>您是第<%=count==null ? 1 : count%>访客!</h2>
</body>
</html>
四、getAttributeNames()方法使用
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import = "java.util.*"%>
<html>
<head>
<title>application</title>
</head>
<body>
<%
//得到全部属性名称
Enumeration enu = this.getServletContext().getAttributeNames();
while(enu.hasMoreElements()){
String name = (String) enu.nextElement();
%>
<h3><%=name%>--><%=this.getServletContext().getAttribute(name)%></h3>
<%
}
%>
</body>
</html>
以上内容参考JAVAWEB开发实战经典(名师讲坛)
转载于:https://blog.51cto.com/udbful/1683839