I was bored yesterday and started to get sleepy around. I remember I had not written a week.ProgramNow, write a program to pick up your mind. Just think about it. Write an image tracking and copy a URL that looks like a graphic to a forum or another webpage, if is automatically added, the browser will automatically request this URL, and then my server will remember this visitor. It is equivalent to tracking. The principle of Website access volume is the same as that of "unified world. CoreCode: Define the request content as an image and output it by default. Few core code, with comments, but more than 100 lines. If Java is used, write a Servlet and define the servlet URL-partrern *. JPG, haha. This enables the function of transferring the JPG file. Every request to the JPG file will be transferred to the servlet, and then in the doget or dopost method, you can get the httpservletrequest object, and you will be free to record anything. Of course, don't forget to pass the image to others. Solution 1: The simplest solution, without using a database, stores access records using files and stores all the processing in doget or dopost. The simplest and least flexible. It takes about half an hour. Debugging and deployment is one hour -. Solution 2: Use a database to record access records. In this case, you need to install MySQL, place the database driver jar in the correct location, create a data table, establish a connection to the database in the application, write SQL statements, and release resources. These things are still written in doget or dopost. At this time, they are quite messy ....... It takes about half an hour. Data storage is good, but not scalable. I started from the following solution. Solution 3: Configure and use the data connection pool, create a table to save the data, and at the same time: Dbconnectionmanager is used to connect to the data source. The getconnection method is used. Dbutil is used to encapsulate database operations, executeupdate, and executequery. Datautil is used to process and determine the validity of data. Create a class trackdata, save the access record as a class, including three fields, and use datautil to write the data class as a Dao. Then, add the following in doget or dopost: Trackdata IP = new trackdata (request, response ); IP. savetrackdata (); OK. Finished, tested, succeeded! Because the data source has been configured for multiple times, And dbconnectionmanager, dbutil, and other classes have been written elsewhere, it will take more than an hour and a half. Improvement: Add a filter IP list (no need to save your access records) and define the Image File Cache (to deal with large traffic volumes ). Add messages. Properties Passedip = 127.0.0.1 211.87.227.95 211.87.209.99 Imageroot = C :\\ tomcat5x \ webapps \ trackyou \ Images Imagenames=clover.jpg Add the messages class to encapsulate the operations on messages. properties. The code that saves the access record becomes: Trackdata IP = new trackdata (request, response ); If (datautil. ispassedip (IP. getip ())){ Return; } IP. savetrackdata (); It was complicated to change, but it was okay. It took more than half an hour. Solution 4: Then I felt uncomfortable. The restriction was too much, so I thought of imitating the event-driven. Add interface: visitevent, which has only one method: onvisit. This event is triggered in doget or dopost. Add the visitlisten class to implement the visitevent interface. Save access records in the onvisit method. Add list visiteventlist in servlet to save the event interface. The biggest trouble is to add event listening. Because it is a Web application and the control is in Tomcat's hand, I cannot decide how to load a class, so I cannot control when to execute the static {} block. I don't want to mess up the initialized static {} blocks. Add the servlet: initapplicationservlet and specify <load-on-startup> 0 </load-on-startup>. The only function is to execute the static {} block and initialize the application. Static { Initapplication (); } This is perfect. After writing, I found that the core things are not the most important, but the surrounding things are important. The core thing is like the OpenGL library. To make something, the surrounding things are very important. |