1,application对象:
String getRealPath(String path) : 得到虚拟目录的绝对路径
public Enumeration getAttributeNames() : 得到所有属性的名称
public String getContextPath() : 得到当前的虚拟目录的名称
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*" %>
<html>
<head><title>www.baidu.com,这是一个学习的好网站</title></head>
<body>
<%
//http://localhost/mldn
String path= application.getRealPath("/");
%>
<h3>真实路径:<%=path%></h3>
</body>
</html>
2,getServletContext() 与
application可以达到同样的效果,(重点
记忆)
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*" %>
<html>
<head><title>www.baidu.com,这是一个学习的好网站</title></head>
<body>
<%
//http://localhost/mldn
String path= application.getRealPath("/");
String path2 = getServletContext().getRealPath("/");
%>
<h3>真实路径:<%=path%></h3>
<h3>真实路径2:<%=path2%></h3>
</body>
</html>
String path = application.getRealPath("/"); ==
String path2 = this.getServletContext().getRealPath("/");
案例解析:网站计数器
三个方面:1,来访人数会很多,建议大整数类--BigInteger完成。
2,用户每次在第一次访问时候才需要进行计数的操作,使用isNew()判断
3,在进行更改,保存的时候需要 进行同步操作。
BigInteger 是 java.math.* 包下面的类型
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*"%>
<%@ page import="java.math.*"%>
<html>
<head><title>www.baidu.com,这是一个学习的好网站</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);
}
}
%>
<h2>您是第<%=count==null?0:count%>位访客!</h2>
</body>
</html>
4,取得全局属性:
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*"%>
<%@ page import="java.math.*"%>
<html>
<head><title>www.baidu.com,这是一个学习的好网站</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>
5,config对象一般是WEB安全性,有关系
6, out对象一般对应缓冲区大小:
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*"%>
<%@ page import="java.math.*"%>
<html>
<head><title>www.baidu.com,这是一个学习的好网站</title></head>
<body>
<%
int buffer = out.getBufferSize();
int available = out.getRemaining();
int use = buffer - available;
%>
<h3>缓冲区大小:<%=buffer%></h3>
<h3>可以用缓冲区大小:<%=available%></h3>
<h3>使用中的缓冲区大小:<%=use%></h3>
</body>
</html>
7,pageContext对象:主要的功能是在JSP文件中的支持,而且一定要记住的是pageContext功能强大,可以操作各种内置对象,javax.servlet.jsp.PageContext类的实例。
主要用在JSP高级编程中,标签编程使用。