import net.sf.json.JSONObject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;
/**
*提供顺序时间图表的Servlet
*author:徐海瀚
*/
public class MonitorChartServlet extends HttpServlet {
/**网络接口名字数组:生成图表中序列的个数*/
private static String[] netNamearray;
/** 网络名字:对应的历史值列表*/
private static Map<String,LinkedList<Integer>> nameValueOfList=new HashMap();
/**图表上最长展示的时间点个数:超过则从时间表后面移走*/
private static final int timeLen=12;
/**保存时间线的队列*/
private static java.util.LinkedList<String> timeList=new java.util.LinkedList();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/json; charset=utf-8");
PrintWriter out = null;
try {
out = response.getWriter();
out.write (getData ());
//request.getRequestDispatcher ("index.html").forward (request,response);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
out.close();
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException {
doGet (request,response);
}
/**
* 生成动图的数据集:
* @return 可用于图的数据
* 方法全同步.
*/
public synchronized String getData(){
Chart chart=new Chart();
List<Object> list=new ArrayList<Object> ();
//将网络流量map中的key,即IP名转为数组
Map<String,Integer> netMap= Ipdump.ins().getNameTrafficMap();
Set<String> ss= netMap.keySet();
String[] forTest=new String[ss.size()];
netNamearray= ss.toArray(forTest);
//初化化放置具体流量数据的结构
for(int i=0;i<netNamearray.length;i++){
if(!nameValueOfList.containsKey(netNamearray[i])){
nameValueOfList.put(netNamearray[i],new LinkedList());
}
}
//将网卡上的计数加入每个卡名对应的列表
for (String key : netNamearray) {
int currnetValue = netMap.get (key);
//清空网络流量map中原来的数据
netMap.put (key, 0);
LinkedList<Integer> historyValueList = nameValueOfList.get (key);
//历史数据表中没有超过timeLen个时,加入列表头中
if (historyValueList.size () < timeLen) {
historyValueList.addFirst (currnetValue);
} else {
historyValueList.removeLast ();
historyValueList.addFirst (currnetValue);
}
}
//查看是否要根据本次请求更新时间线:
if(!timeList.contains (currentTime ())) {
if (timeList.size () < timeLen) {
//加入到前面
timeList.addLast(currentTime ());
} else {
//移除后面的时间
timeList.removeFirst ();
// 加入到前面
timeList.addLast (currentTime ());
}
}
//将数据填充到dataset中
for (String aNetNamearray : netNamearray) {
LinkedList<Integer> historyValueList = nameValueOfList.get (aNetNamearray);
Map<String,Object> map=new HashMap<String, Object> ();
map.put ("name",aNetNamearray);
map.put ("data",historyValueList);
list.add (map);
}
//填充数据到chart对象的series属性中
chart.setSeries (list);
//将时间线填充到chart对象的xAxis属性中
Map<String,Object> map1=new HashMap<String, Object> ();
map1.put ("categories",timeList);
chart.setxAxis (map1);
//将chart对象转换成字符串对象
JSONObject json= JSONObject.fromObject (chart);
String data =json.toString ();
return data;
}
/**
*当前时间信息
* @return 当前时间串
*/
private static String currentTime(){
Date d = new Date();
SimpleDateFormat kk=new SimpleDateFormat("HH:mm:ss");
return kk.format(d);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
计算机网络课程设计-IP包流量统计
共70个文件
jar:25个
xml:18个
java:6个
需积分: 23 17 下载量 150 浏览量
2018-04-29
20:51:01
上传
评论 1
收藏 4.01MB RAR 举报
温馨提示
网页动态可视化显示IP包数据,界面相对美观,有开关按钮,可以下载瞬时图片保存,值得下载。编译器是用idea,可以运行,启动后需要请求到访问页面。
资源推荐
资源详情
资源评论
收起资源包目录
monitorippacket.rar (70个子文件)
monitorippacket
monitorippacket.iml 5KB
src
main
webapp
WEB-INF
lib
javax.servlet.jsp.jar 77KB
javax.resource.jar 43KB
orsonpdf-1.6-eval.jar 71KB
javax.jms.jar 25KB
json-lib-2.1-jdk15.jar 293KB
javax.persistence.jar 127KB
log4j-web-2.9.1.jar 32KB
jcommon-1.0.23.jar 323KB
javax.transaction.jar 9KB
commons-beanutils-1.7.0.jar 184KB
jpcap.jar 27KB
ezmorph-1.0.3.jar 76KB
commons-collections-3.1.jar 546KB
javax.servlet.jsp.jstl.jar 27KB
log4j-core-2.9.1.jar 1.48MB
junit-4.11.jar 239KB
javax.servlet.jar 68KB
hamcrest-core-1.3.jar 44KB
commons-logging.jar 44KB
log4j-1.2-api-2.9.1.jar 60KB
log4j-api-2.9.1.jar 234KB
javax.ejb.jar 46KB
swtgraphics2d.jar 17KB
commons-lang-2.5.jar 273KB
servlet.jar 78KB
web.xml 1KB
views
index.html 2KB
statics
js
exporting.js 9KB
highcharts.js 188KB
jquery-3.2.1.min.js 85KB
related
2.jpg 23KB
1.jpg 19KB
buttonfonts
icomoon.woff 1KB
icomoon.ttf 1KB
icomoon.svg 2KB
icomoon.eot 2KB
css
buttoncss
default.css 6KB
normalize.css 2KB
styles.css 13KB
font-awesome.min.css 23KB
resources
log4j2.xml 3KB
java
ToViewServlet.java 687B
MonitorChartServlet.java 5KB
Ipdump.java 3KB
LoopPacketThread.java 597B
DumpPacket.java 1KB
Chart.java 3KB
target
generated-sources
annotations
classes
MonitorChartServlet.class 4KB
log4j2.xml 3KB
Ipdump.class 3KB
DumpPacket.class 2KB
LoopPacketThread.class 665B
ToViewServlet.class 1KB
Chart.class 3KB
.idea
artifacts
monitorippacket_war_exploded.xml 1KB
libraries
Maven__commons_collections_commons_collections_3_2_1.xml 616B
commons_beanutils_1_7_0.xml 154B
Maven__net_sf_ezmorph_ezmorph_1_0_6.xml 512B
Maven__commons_beanutils_commons_beanutils_1_8_0.xml 594B
Maven__commons_lang_commons_lang_2_5.xml 525B
Maven__com_lmax_disruptor_3_3_4.xml 502B
Maven__net_sf_json_lib_json_lib_jdk15_2_4.xml 521B
Maven__junit_junit_3_8_1.xml 462B
Maven__commons_logging_commons_logging_1_1_1.xml 572B
misc.xml 2KB
compiler.xml 642B
workspace.xml 65KB
modules.xml 270B
pom.xml 1KB
共 70 条
- 1
资源评论
slagsea
- 粉丝: 500
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功