Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to count the number of visitors by JSP

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to achieve traffic counting in JSP. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Sometimes it is necessary to count the number of clicks for each article, and if you update the library every time you browse, the performance will be under great pressure on the server in the case of large traffic. A better way is to cache the updated data first, and then use the batch processing of the database to update the database every once in a while.

Then the following JSP tutorial provides the following source code:

CountBean.java / * * CountData.java * * Created on, June 30, 2009, 4:44 * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. * / package com.tot.count; / * / public class CountBean {private String countType; int countId; / * * Creates a new instance of CountData * / public CountBean () {} public void setCountType (String countTypes) {this.countType=countTypes;} public void setCountId (int countIds) {this.countId=countIds;} public String getCountType () {return countType;} public int getCountId () {return countId }} CountCache.java / * * CountCache.java * * Created on, June 30, 2009, 5:01 * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. * / package com.tot.count; import java.util.*; / * @ author http://www.tot.name * / public class CountCache {public static LinkedList list=new LinkedList (); / * * Creates a new instance of CountCache * / public CountCache () {} public static void add (CountBean cb) {if (cbsp null) {list.add (cb) } CountControl.java / * * CountThread.java * * Created on, June 30, 2009, 4:57 * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. * / package com.tot.count; import tot.db.DBUtils; import java.sql.*; / * @ author http://www.tot.name * / public class CountControl {time of last update private static long executeSep=60000;// defines the update interval (in milliseconds / * * Creates a new instance of CountThread * / public CountControl () {} public synchronized void executeUpdate () {Connection conn=null) PreparedStatement ps=null; try {conn = DBUtils.getConnection (); conn.setAutoCommit (false); ps=conn.prepareStatement ("update t_news set hitshits=hits+1 where id=?"); for (int ionomy 0 +) {CountBean cb= (CountBean) CountCache.list.getFirst (); CountCache.list.removeFirst (); ps.setInt (1, cb.getCountId ()); ps.executeUpdate (); ⑴ / / ps.addBatch () ⑵} / / int [] counts = ps.executeBatch (); ⑶ conn.commit ();} catch (Exception e) {e.printStackTrace ();} finally {try {ps.clearParameters (); ps.close (); ps=null;}} catch (SQLException e) {} DBUtils.closeConnection (conn);} public long getLast () {return lastExecuteTime } public void run () {long now = System.currentTimeMillis (); if ((now-lastExecuteTime) > executeSep) {/ / System.out.print ("lastExecuteTime:" + lastExecuteTime); / / System.out.print ("now:" + now+ "\ n"); / / System.out.print ("sep=" + (now-lastExecuteTime) + "\ n"); lastExecuteTime=now; executeUpdate () } else {/ / System.out.print ("wait for" + (now-lastExecuteTime) + "seconds:" + "\ n");} / / Note: if your database driver supports batch processing, you can remove the comments before the code marked by ⑵ and ⑶, and write the comment class before the code ⑴. The following is called in JSP. <% CountBean cb=new CountBean (); cb.setCountId (Integer.parseInt (request.getParameter ("cid"); CountCache.add (cb); out.print (CountCache.list.size () + "< br >"); CountControl c=new CountControl (); c.run (); out.print (CountCache.list.size () + "< br >") % > this is the end of the article on "how to achieve traffic counting in JSP". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report