application的常用方法:
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<html>
<head>
</script>
</head>
<body>
<%=application.getServerInfo()%>//返回servlet的版本信息
<%=application.getRealPath("a.jsp")%>//返回虚拟路径的真实路径
<%=application.getResource("/a.jsp")%>//返回一个URL对象,该对象反映位于给定URL地址的servlet环境中的资源
</body>
</html>
(一)运用application的三个典型的应用:
1.网络日志
2.聊天室
3.网页计数器
(二)网站日志
用application.log("");将信息写入web应用程序系统日志中。默认情况下,servlet系统日志存放在tomcat安装文件夹下的logs文件夹中,文
件名称通常是“域名.日期.log”
(三)聊天室
chatroom.html
*********
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>//分割窗口,注意这种情况下不能用<body></body>标签
<frameset rows="*,150">
<frame src="message.jsp">
<frame src="talk.jsp">
</frameset>
</HTML>
message.jsp
setTimeout("hanshu",200)函数是延时200毫秒再执行操作hanshu
********
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<html>
<head>
<meta HTTP-EQUIV="REFRESH" CONTENT="3;url=message.jsp">//通过http头配置信息,使网页周期性刷新
<script language="javaScript">
function scrollWindow()
{
this.scroll(0,65000);
setTimeout('scrollWindow()',200);
}
scrollWindow();
</script>
</head>
<body>
<%=application.getAttribute("words")%>
</body>
</html>
talk.jsp
********
<%@ page language="java" contentType="text/html;charset=gbk"%>
<%
request.setCharacterEncoding("GBK");
String mywords=request.getParameter("message");
application.log(mywords);//为系统写日志文件
//先判断myword是否为空
if(mywords!=null)
{
int len_mywords=mywords.length();
int flag=1;
application.log(""+len_mywords);
for(int i=0;i<len_mywords;i++)
{
if(mywords.charAt(i)=='<')
flag=0;
}
if(flag==1)
{
mywords=">>: "+mywords;
Object obj=application.getAttribute("words");
if(obj==null)
{
application.setAttribute("words",mywords);
}
else
{
application.setAttribute("words",obj.toString()+mywords+"<br>");
}
}
}
%>
<html>
<head>
<body>
<form action="talk.jsp" method="post">
<input name="message" type="text" size=50>
<input type="submit" value="发送">
</form>
</body>
</head>
</html>
(四)网页计数器
只针对一个jsp程序的作用。application.getAttribute()返回的是一个Object类型的对象,要用到toString()方法转换成字符串。
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<html>
<head>
</script>
</head>
<body>
<%
if(application.getAttribute("count")==null)
{
application.setAttribute("count","1");
}
else
{
String str=application.getAttribute("count").toString();
int ncount=Integer.valueOf(str).intValue()+1;
application.setAttribute("count",""+ncount);
}
%>
你是第<%=application.getAttribute("count")%>的访客。。。。。。。
</body>
</html>