It is common to use application objects to achieve the number of visitors, but some books explain this technique often without being complete. Once the server restarts, as the application will be destroyed, re-established, the number of people will start again from scratch. Solution, personally, should use the database at the same time, so that regardless of the server to restart or encounter the downtime of what, the number of statistics will be accumulated up, not lost. Here is an example to solve the problem of using the Application object to achieve a zero-based access statistics function due to server restart or downtime At the same time, if the Application object is used to extract the fatal database information saved to Web. XML, it not only solves the security problem, but also is a global variable that writes a long string of data without each connection to the database.
I. BASIC OBJECTIVES
Count the number of visitors to a page, and the number of visitors will not be lost after the server restarts.
II. Basic Preparation
In the MySQL test database to build a visitcount table, there is no primary key, just a column, that is, a property, Visitcount, not self-increment, the default value is 0, of course, do not set the default value is also possible. Type is shaping.
In the site of the project directory of the Lib folder, put into the database driver, configuration data source, that is, put in a mysql-connector-java-5.1.32.jar, this thing version number without tube, search on the internet. Site directory structure such as:
Add the following snippet to Web. XML, or just write the Web. xml:
<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "2.5" xmlns= "Http://java.sun.com/xml/ns/javaee" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http ://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "><context-param><param-name>url</param-name ><param-value>jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8& Useoldaliasmetadatabehavior=true</param-value></context-param><context-param><param-name >user</param-name><param-value>root</param-value></context-param><context-param ><param-name>pwd</param-name><param-value>root</param-value></context-param> </web-app>
In this way, later, Url,user,root is the global variable that we connect to the database, as for how to connect the database JSP, you can refer to my previous "MySQL" Java database additions and deletions of MySQL, Java System Class (click to open the link)
All pages of the entire site project, using Application.getinitparameter (""); the method can go to these global variables as soon as they are taken. It is worth noting that the original & must be written as an escape character & otherwise Web. XML will take & as a compilation symbol, the entire network project cannot be launched in the Tomcat server.
Third, the production process
In the index.jsp write the following code, you can complete, notice the introduction of the corresponding Java package at the beginning, while the site code to change to Utf-8.
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" utf-8 "%><%@ page import=" java.sql.* "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
"JSP" uses Application object to achieve visitor count function