application的应用

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>


     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值