jsp变量和方法的声明
来源:互联网 发布:网络英雄洛克人粤语 编辑:程序博客网 时间:2024/05/21 22:24
一、变量和方法的声明
1.在<%!与%>之间放置声明变量是页面的成员变量。声明的变量在整个JSP页面都有效。例如
<%!
int i=100;
Data date;
%>
因为JSP引擎转换JSP页面成Java文件时,将这些变量作为类的成员变量,这些变量的内存空间直到服务器关闭才被释放。
所以所有请求该页面的线程共享JSP页面的成员变量,任何用户对该成员变量的操作结果都影响其它客户。
简单页面计数器
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<body>
<%!
int i=0;
%>
<%
i++;
%>
<p>
您是第<%=i%>个访问本站的用户。
</body>
</html>
2. 声明方法
在<%!和%>之间声明的方法在整个JSP页面都有效,但是该方法内定义的变量只在该方法内有效。当多个用户请求一个JSP页面时,可以使用方法操作成员变量。
<%@ page contentType="text/htnl;charset=gb2312"%>
<html>
<body>
<%!
int number=0;
synchronized void contPeople()
{
number++;
}
%>
<%
countPeople();
%>
<p>
您是第<%=number%>个访问客户。
</body>
</html>
将访问人数保存到文本文件中
<%@ page contentType="text/htnl;charset=gb2312"%>
<%@ page import="java.io.*" %>
<html>
<body>
<%!
int number=0;
File file=new File("count.txt");
synchronized void contPeople()
{
if(!file.exists())
{
number++;
try{
file.createNewfile();
FileOutputStream out =new FileOutputStream("count.txt");
DataOutputStream dataOut=new DataOutputStream(out);
dataOut.writeInt(number);
out.close();
dataOut.close();
}
catch(IOException ex){}
}
else
{
try{
FileInputStream in=new FileInputStream("count.txt");
DataInputStream dataIn=new DataInputStream(in);
number=dataIn.readInt();
number++;
in.close();
dataIn.close();
FileOutputStream out =new FileOutputStream("count.txt");
DataOutputStream dataOut=new DataOutputStream(out);
dataOut.writeInt(number);
out.close();
dataOut.close();
}
catch(IOException ex){}
}
}
%>
<%
countPeople();
%>
<p>
您是第<%=number%>个访问客户。
</body>
</html>
- jsp变量和方法的声明
- JSP和servlet中变量声明的方法
- jsp中声明变量和方法
- GO声明变量和常量的方法
- jsp中变量的声明
- JSP中变量的声明
- jsp两种声明变量<%! %>和<% %>的区别
- 头文件中定义变量和声明变量的方法
- 常变量声明的方法
- go 语言中变量和常量的声明方法
- JSP两种声明变量的区别
- JSP两种声明变量的区别
- jsp中变量声明中!的区别
- JSP的变量声明以及输出表达式
- JSP两种声明变量的区别
- JSP两种声明变量的区别
- JSP两种声明变量的区别
- JSP两种声明变量的区别
- Computer Systems Performance Evaluation and Prediction
- Debian/Ubuntu下配置Subversion Server
- The Definitive Guide to SWT and JFACE
- Practical Service Level Management: Delivering High-Quality Web-Based Services
- Architecting Enterprise Solutions: Patterns for High-Capability Internet-based Systems
- jsp变量和方法的声明
- XPath 2.0 Programmer's Reference
- VC6.0 msdn 文章中关于更改m_pszAppName的错误
- Professional Development with Web APIs : Google, eBay, Amazon.com, MapPoint, FedEx
- 剧本翻译之SHUFFLE【前奏】
- Android 资源和国际化
- Google搜索秘技
- 纪念第一个求助帖
- XPath Kick Start: Navigating XML with XPath 1.0 and 2.0 (Kick Start)