filter实现----网站点击计数器

来源:互联网 发布:数据库系统工程师 pdf 编辑:程序博客网 时间:2024/05/01 20:48

1.filterCountJdbc.java

import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;public class filterCountJdbc {    /*     * test     *///   public static void main(String[] arg) {//   int coun=100;//   filterCountJdbc fj=new filterCountJdbc();//   fj.qu();//   }    // jdbc连接    public static Connection getConnection() {        String driverName = "com.mysql.jdbc.Driver";        String url = "jdbc:mysql://localhost:3306/filterCount";        String user = "root";        String password = "123456";        Connection con = null;        // 连接数据库        try {            Class.forName(driverName);// load driver            con = DriverManager.getConnection(url, user, password);            System.out.println("connection mysql success");        } catch (Exception e) {            e.printStackTrace();        }        return con;    }     //向数据库中存 计数值    public void cun(int count) {        Connection conn = filterCountJdbc.getConnection();        PreparedStatement pstm = null;        String sql = null;        int rows;        int countt = count;        // 操作数据库        try {            sql = "update Count set cou=" + count;            // sql = "update Count set cou=? where id=?";            System.out.println("use sql language:::" + sql);            pstm = conn.prepareStatement(sql);            // pstm.setString(1,count);            // pstm.setInt(1,count);            // pstm.setInt(2,id);            rows = pstm.executeUpdate(sql);            System.out.println(rows);            } catch (Exception ex) {            ex.printStackTrace();        }    }    //从数据库中取出  计数值    public int qu() {        Connection conn = filterCountJdbc.getConnection();        PreparedStatement pstm = null;        String sql = null;        ResultSet rs;        int count = 0;        // 操作数据库        try {            sql = "select cou from Count";            System.out.println("use sql language:::" + sql);            pstm = conn.prepareStatement(sql);            // pstm.setString(1,count);            // pstm.setInt(1,count);            // pstm.setInt(2,id);            rs = pstm.executeQuery(sql);            while(rs.next()){                 //String name =re.getString("username");                //countt = rs.getInt("cou") ;                 count = rs.getInt(1) ;  // 此方法比较高效                 System.out.println("jdbc_qu_count"+count);                }             } catch (Exception ex) {            ex.printStackTrace();        }        return count;    }}

2.filterCount.java

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;import javax.servlet.annotation.WebFilter;/** * Servlet Filter implementation class filterCount */@WebFilter("/filterCount")public class filterCount implements Filter {     int count;    /**     * Default constructor.      */    public filterCount() {        // 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        filterCountJdbc fj=new filterCountJdbc();        if(count>0){        fj.cun(count);        }else{            count=fj.qu();        }         // 输出计数器          System.out.println("网站访问统计:"+ count );          count++;          // 把请求传回到过滤器链          chain.doFilter(request,response);    }    /**     * @see Filter#init(FilterConfig)     */    public void init(FilterConfig fConfig) throws ServletException {        // TODO Auto-generated method stub        count=0;    }}

3.数据库设计
数据库名:filterCount
表名:Count
字段:cou 非空int


4.web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  <display-name>Servlet_test1</display-name>  <welcome-file-list>    <welcome-file>index.html</welcome-file>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list>  <filter>   <filter-name>filterCount</filter-name>   <filter-class>filterCount</filter-class></filter><filter-mapping>   <filter-name>filterCount</filter-name>   <url-pattern>/*</url-pattern></filter-mapping></web-app>

这里写图片描述

0 0