无为清净楼资源网 Design By www.qnjia.com
复制代码 代码如下:
//过滤器类
public class EcondingFilter implements Filter {
private String charset = null;
private ServletContext context = null;
private String path = "";
/**
* 在销毁前将数据存入本地文件中
*/
public void destroy() {
//获取servleContext中的属性的那个值
String nums = (String) context.getAttribute("nums");
//创建写入流
FileWriter fw = null;
BufferedWriter bw = null;
try {
fw = new FileWriter(path);
bw = new BufferedWriter(fw);
bw.write(nums);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bw != null) {
bw.close();
}
if (fw != null) {
fw.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("filter销毁");
}
复制代码 代码如下:
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
System.out.println("doFilter前");
String path = ((HttpServletRequest)request).getServletPath();//获取每次访问的action的相对路径
<img alt="" src="/UploadFiles/2021-04-02/Center">if(path.endsWith("/login.action")){
context.setAttribute("nums",Integer.parseInt(context.getAttribute("nums").toString())+1+"");
}
request.setCharacterEncoding(charset);
response.setCharacterEncoding(charset);
chain.doFilter(request, response);
System.out.println("doFilter后");
}
复制代码 代码如下:
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
System.out.println("filter初始化");
//获取编码格式
charset = filterConfig.getInitParameter("encoding");
//获取servletContext
context = filterConfig.getServletContext();
System.out.println(charset);
path = context.getRealPath("");
File file = new File("D:\\text.txt");
if (!file.exists()) {//判断文件是否存在
// 如果文件不存在,就创建一个文件,保存在D盘中
file = new File("d:\\text.txt");
FileWriter fw = null;
BufferedWriter bw = null;
try {
fw = new FileWriter(file);
bw = new BufferedWriter(fw);
bw.write(0 + "");// 写入初始化数据0
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bw != null) {
bw.close();
}
if (fw != null) {
fw.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//当每次tomcat启动服务时,进行读取创建的那个文件
path = "d:\\text.txt";
// 从本地读取访问的人数的文件
FileReader fr = null;
BufferedReader bf = null;
String nums = "";
try {
fr = new FileReader(path);
bf = new BufferedReader(fr);
nums = bf.readLine();
System.out.println(nums);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bf != null) {
bf.close();
}
if (fr != null) {
fr.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//将获得到的数据保存在servletContext中
context.setAttribute("nums", nums);
}
}
用过滤器方便的一点,不需要我们每次手动去调用,当web服务启动时候,自动会引用。首先说下,我写到init方法的依据是,每次web服务启动会调用一次init方法,当关闭服务的时候会调用一次destory方法,将计数的那个数据文件,这个方法写到init方法和destory方法,这样可以减少每次的不断的读取服务器和读取写入文件的次数,当我们每登陆一次,就让servletContext中的那个attr加1,从而实现当关闭服务的时候,把文件保存在磁盘中。下次从磁盘中读取。
//过滤器类
public class EcondingFilter implements Filter {
private String charset = null;
private ServletContext context = null;
private String path = "";
/**
* 在销毁前将数据存入本地文件中
*/
public void destroy() {
//获取servleContext中的属性的那个值
String nums = (String) context.getAttribute("nums");
//创建写入流
FileWriter fw = null;
BufferedWriter bw = null;
try {
fw = new FileWriter(path);
bw = new BufferedWriter(fw);
bw.write(nums);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bw != null) {
bw.close();
}
if (fw != null) {
fw.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("filter销毁");
}
复制代码 代码如下:
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
System.out.println("doFilter前");
String path = ((HttpServletRequest)request).getServletPath();//获取每次访问的action的相对路径
<img alt="" src="/UploadFiles/2021-04-02/Center">if(path.endsWith("/login.action")){
context.setAttribute("nums",Integer.parseInt(context.getAttribute("nums").toString())+1+"");
}
request.setCharacterEncoding(charset);
response.setCharacterEncoding(charset);
chain.doFilter(request, response);
System.out.println("doFilter后");
}
复制代码 代码如下:
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
System.out.println("filter初始化");
//获取编码格式
charset = filterConfig.getInitParameter("encoding");
//获取servletContext
context = filterConfig.getServletContext();
System.out.println(charset);
path = context.getRealPath("");
File file = new File("D:\\text.txt");
if (!file.exists()) {//判断文件是否存在
// 如果文件不存在,就创建一个文件,保存在D盘中
file = new File("d:\\text.txt");
FileWriter fw = null;
BufferedWriter bw = null;
try {
fw = new FileWriter(file);
bw = new BufferedWriter(fw);
bw.write(0 + "");// 写入初始化数据0
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bw != null) {
bw.close();
}
if (fw != null) {
fw.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//当每次tomcat启动服务时,进行读取创建的那个文件
path = "d:\\text.txt";
// 从本地读取访问的人数的文件
FileReader fr = null;
BufferedReader bf = null;
String nums = "";
try {
fr = new FileReader(path);
bf = new BufferedReader(fr);
nums = bf.readLine();
System.out.println(nums);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bf != null) {
bf.close();
}
if (fr != null) {
fr.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//将获得到的数据保存在servletContext中
context.setAttribute("nums", nums);
}
}
用过滤器方便的一点,不需要我们每次手动去调用,当web服务启动时候,自动会引用。首先说下,我写到init方法的依据是,每次web服务启动会调用一次init方法,当关闭服务的时候会调用一次destory方法,将计数的那个数据文件,这个方法写到init方法和destory方法,这样可以减少每次的不断的读取服务器和读取写入文件的次数,当我们每登陆一次,就让servletContext中的那个attr加1,从而实现当关闭服务的时候,把文件保存在磁盘中。下次从磁盘中读取。
标签:
jsp,网页计数器
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
更新日志
2024年09月20日
2024年09月20日
- 《粤语超白金 百听不厌的经典金曲 2CD》[WAV/分轨][1.3GB]
- 群星.1994-全世界精丫华星】【WAV+CUE】
- 杜德伟.1997-知.解2CD【滚石】【WAV+CUE】
- 群星.2022-一不小心喵上你原声大碟【TME影音】【FLAC分轨】
- 《战锤40K星际战士2》卡在正在加入服务器解决方法
- 《勇者斗恶龙怪物仙境3》玩法内容介绍
- 《幸福工厂》无限报错解决方法
- 交错战线原始交易所推荐角色一览
- 战锤40K星际战士2战术职业介绍|战术职业技能效果一览
- 战锤40K星际战士2突击职业介绍|突击职业技能效果一览
- [妙音金曲]群星《悲情咖啡屋》(黑胶)2CD[DTS-WAV]
- 阿兰·达瓦卓玛《A-Lan阿兰唯美歌姬》2CD[DTS-WAV]
- 【小提琴】陈立新《思乡曲》2004[FLAC+CUE]
- 《战地》新作明年初大规模测试!EA已内部测试一年
- 《GTAOL》PC版时隔多年更新反作弊!小助手宣布跑路