您的位置:首页 > 其它

servlet10-计数器

2016-06-23 13:42 302 查看
思路是定义一个全局变量,初始值为0,每调用一次get或者post该变量+1。这种方式在tomcat重启后计数器会归0,解决办法是把变量值存入数据库进行读取写入。

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;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: