????我们在全局<%! %>中定义jspInit方法帮助页面每次加载的时候都会去counter.db里面拿counter的数据
????需要配合FileInputStream和DataInputStream读取和写入每次改变的counter值(看起来很复杂,但其实每次像写公式那样直接这样写就可以了)
????其实就像在数据库里拿东西,结束的时候再修改。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35<%@ 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); } } %> <html><body> <% count++; %> Welcome! You are <%= count %> th visitor(s). </body></html> <%! 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); } } %>
最后
以上就是明理冬瓜最近收集整理的关于JSP持久层的使用的全部内容,更多相关JSP持久层内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复