java_web/webapp/persistent_counter.jsp
2024-11-26 15:35:29 +08:00

34 lines
938 B
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<%@ 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>