servlet10-计数器
2016-06-23 13:42
302 查看
思路是定义一个全局变量,初始值为0,每调用一次get或者post该变量+1。这种方式在tomcat重启后计数器会归0,解决办法是把变量值存入数据库进行读取写入。
累计一个页面的点击量照上面做很简单,如果是累计一个站点(对个页面)的点击量呢?
可以用过滤器实现,在过滤器的init方法里赋初值0,dofilter方法里+1,关键是还要在web.xml配置url-pattern,在这个标签里配上所有需要计数的页面。、
ackage p14hitcount; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class PageHitcount */ public class PageHitcount extends HttpServlet { private static final long serialVersionUID = 1L; private int hitcount; public void init() { hitcount=0; } /** * @see HttpServlet#HttpServlet() */ public PageHitcount() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("text/html;charset=utf-8"); hitcount++; PrintWriter out = response.getWriter(); String title = "总点击量"; String docType = "<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n"; out.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" + "<body bgcolor=\"#f0f0f0\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<h2 align=\"center\">" + hitcount + "</h2>\n" + "</body></html>"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
累计一个页面的点击量照上面做很简单,如果是累计一个站点(对个页面)的点击量呢?
可以用过滤器实现,在过滤器的init方法里赋初值0,dofilter方法里+1,关键是还要在web.xml配置url-pattern,在这个标签里配上所有需要计数的页面。、
package p14hitcount; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; /** * Servlet Filter implementation class SiteHitCount */ public class SiteHitCount implements Filter { private int hitCount; /** * Default constructor. */ public SiteHitCount() { // TODO Auto-generated constructor stub } /** * @see Filter#destroy() */ public void destroy() { // TODO Auto-generated method stub } /** * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub // place your code here // pass the request along the filter chain hitCount++; System.out.println("hitCount:"+hitCount); chain.doFilter(request, response); } /** * @see Filter#init(FilterConfig) */ public void init(FilterConfig fConfig) throws ServletException { // TODO Auto-generated method stub hitCount=0; } }
相关文章推荐
- Intellij idea 更改 Ctrl+space 快捷键为Alt+/
- 面对对象中错误信息进行配置
- 基于CentOS 6.8的OpenStack手动安装-环境配置
- JVM必备指南
- I/O、Applet以及其他主题
- ZBrush怎样快速雕刻出头发(二)
- SAX解析XML文档
- jquery mobile开发手机webapp页面
- ismod 加载过程
- SQL Server删除distribution数据库二
- ServiceStack.Redis 使用教程
- c++指针函数的使用——回调函数
- eclipse与android studio的常用快捷键对照
- android-AccessibilityNodeInfo
- 第16 章 多线程
- android ListView的Item中有CheckBox,导致OnItemClick不响应的解决办法
- BIO请求-应答[通讯模型]
- 问题 J: 二叉树遍历
- oc中的Set函数、Get函数、点语法和类方法等
- UVa 11100