java_web/webapp/persistent_counter.jsp

34 lines
938 B
Plaintext
Raw Normal View History

2024-11-26 15:35:29 +08:00
<%@ page language="java" import="java.io.*" %>
<%!
int count = 0;
String dbPath;
public void jspInit() {
try {
dbPath = getServletContext().getRealPath("/WEB-INF/counter.db");
FileInputStream fis = new FileInputStream(dbPath);
DataInputStream dis = new DataInputStream(fis);
count = dis.readInt();
dis.close();
} catch (Exception e) {
log("Error loading persistent counter", e);
}
}
public void jspDestroy() {
try {
FileOutputStream fos = new FileOutputStream(dbPath);
DataOutputStream dos = new DataOutputStream(fos);
dos.writeInt(count);
dos.close();
} catch (Exception e) {
log("Error storing persistent counter", e);
}
}
%>
<%-- 下面是向浏览器输出的主要内容它将成为产生的_jspService()方法的一部分 --%>
<html><body>
<% count++; %>
Welcome! You are <%= count %> th visitor(s).
</body></html>