servlet-servletContext网站计数器
1、在项目中新建文件夹新建文件nums.txt
2、在web.xml文件配置
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>NumServlet</servlet-name>
<servlet-class>servlet.NumServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
public class NumServlet extends HttpServlet {
//复写init初始化方法,将数据读取到ServletContext对象中
@Override
public void init() throws ServletException {
//获取文件路径
String path=this.getServletContext().getRealPath("/nums/nums.txt");
//声明流对象
FileReader fr=null;
BufferedReader br=null;
try{
fr=new FileReader(path);
br=new BufferedReader(fr);
String nums=br.readLine();
System.out.println(nums);
this.getServletContext().setAttribute("nums",nums);
}catch(Exception e){
e.printStackTrace();
}finally{
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//复写销毁方法,存储计数器到文件中
@Override
public void destroy(){
//获取网页计数器
int nums=(int)this.getServletContext().getAttribute("nums");
//获取文件路径
String path=this.getServletContext().getRealPath("/nums/nums.txt");
//声明流对象
BufferedWriter bw=null;
FileWriter fw=null;
try{
fw=new FileWriter(path);
bw=new BufferedWriter(fw);
bw.write(nums+"");
bw.flush();
}catch(Exception e){
e.printStackTrace();
}finally{
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
servlet-servletContext网站计数器的更多相关文章
- ServletContext结合Servlet接口中的init()方法和destroy()方法的运用----网站计数器
我们一般知道Servlet接口中的init()方法在tomcat启动时调用,destroy()方法在tomcat关闭时调用.那么这两个方法到底在实际开发中有什么作用呢?这就是这个随笔主要讲的内容. 思 ...
- ServletContext与网站计数器
什么是ServletContext? ServletContext是服务器的一个公用的空间,是不同的浏览器共享的一个数据. 由图可以看出ServletContext和Cookie与session之间的 ...
- ServletContext实现网站计数器
在网站开发中,有很多功能需要使用ServletContext,比如: 1.网站计数器 2.网站在线用户的显示 3.简单的聊天系统 总之,如果是涉及到不用用户共享数据,而这些数据量不大,同时又不希望写入 ...
- 【原】tomcat 7 启动报错:java.lang.NoSuchMethodError: javax.servlet.ServletContext.getSessionCookieConfig()Ljavax/servlet/SessionCookieConfig的解决
现象: tomcat 7 启动报错:java.lang.NoSuchMethodError: javax.servlet.ServletContext.getSessionCookieConfig() ...
- tomcat7 启动项目报错 java.lang.NoSuchMethodError: javax.servlet.ServletContext.getSessionCookieConfig()
JDK版本:jdk1.8.0_77 Tomcat 版本:apache-tomcat-7.0.47 异常重现步骤: 1.完成项目部署 2.启动Tomcat 异常头部信息:java.lang.NoSuch ...
- application 网站计数器
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- java.lang.NoClassDefFoundError: javax/servlet/ServletContext
方法1:把SpringBoot中main方法所在的class不再继承org.springframework.boot.context.web.SpringBootServletInitializer即 ...
- Histats安装Counter网站计数器 - Blog透视镜
Histats提供十分多样性的Counter网站计数器,可以依照你个人的喜好与需求,选择适合的Counter网站计数器,也可以针对同一网站,安装多个Counter网站计数器,作法其实比注册账号时更简单 ...
- Histats申请Counter网站计数器 - Blog透视镜
为了计算网页被浏览的次数,访客人数等统计数据,作为未来分析之用,可以向Histats申请免费的Counter网站计数器,它的功能相当齐全,同时也会保留一段时间的资料,当作统计比较的资料,更可以进一步付 ...
- tomcat 7 启动报错:java.lang.NoSuchMethodError: javax.servlet.ServletContext.getSessionCookieConfig()Ljavax/servlet/SessionCookieConfig的解决
现象: tomcat 7 启动报错:java.lang.NoSuchMethodError: javax.servlet.ServletContext.getSessionCookieConfig() ...
随机推荐
- Java 权限框架 Shiro 实战二:与spring集成、filter机制
转自:https://www.cnblogs.com/digdeep/archive/2015/07/04/4620471.html Shiro和Spring的集成,涉及到很多相关的配置,涉及到shi ...
- SQL Server 2005 无法连接到WMI提供程序 无法执行 SQL Server 系统配置检查器
无法连接到WMI提供程序.你没有权限或者该服务器无法访问/cannot connect to WMI provider. You do not have permission or the--由于计算 ...
- 2 python第三章文件操作
1.三元运算 三元运算又称三目运算,是对简单的条件语句的简写,如: 简单条件语句: if 条件成立: val = 1 else: val = 2 改成三元运算: val = 1 if 条件成立 els ...
- ubuntu查询可用安装包
当使用apt-get install packages时,如果不知道安装包的具体名称,可以使用关键字进行搜索,使用:apt-cache search keywords
- Ansible 管理MySQL主从复制
mysql_replication - 管理MySQL复制 1.3版新功能 概要 要求(在执行模块的主机上) 选项 例子 笔记 状态 支持 概要 管理MySQL服务器复制,从属,主状态获取和更改主控主 ...
- redis的五种常见数据类型的常用指令
一.String字符串,key-value 应用场景:string是redis的最基本数据类型,key-value格式,一个key对应一个值的情况下 1.设置key = value:set key ...
- The Closest M Points
The Closest M Points http://acm.hdu.edu.cn/showproblem.php?pid=4347 参考博客:https://blog.csdn.net/acdre ...
- 转---tcp三次握手四次挥手syn fin......
http://blog.chinaunix.net/uid-22312037-id-3575121.html转自 一.TCP报文格式 TCP/IP协议的详细信息参看<TCP/IP协 ...
- 全排列12 · Permutations
无重复 [抄题]: Given a collection of numbers, return all possible permutations. For example,[1,2,3] have ...
- .net调用web邮箱发送邮件(转载)
public static void SendEmail() { System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient( ...