请问老师这是怎么回事?
package com.imooc.total; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.ServletRequestEvent; import javax.servlet.ServletRequestListener; import javax.servlet.http.HttpServletRequest; public class RequestTotalListener implements ServletContextListener,ServletRequestListener{ @Override public void requestDestroyed(ServletRequestEvent sre) { // TODO Auto-generated method stub } @Override public void requestInitialized(ServletRequestEvent sre) { HttpServletRequest request=(HttpServletRequest)sre.getServletRequest(); String url=request.getRequestURL().toString(); if(url.endsWith("/rt")==true) { return; } // TODO Auto-generated method stub List<String> timeList=(List)sre.getServletContext().getAttribute("timList"); List<Integer> valueList=(List)sre.getServletContext().getAttribute("valueList"); Date date=new Date(); SimpleDateFormat sdf=new SimpleDateFormat("HH:mm"); String time=sdf.format(date); if(timeList.indexOf(time) == -1) { timeList.add(time); valueList.add(1); sre.getServletContext().setAttribute("timeList",timeList); sre.getServletContext().setAttribute("valueList", valueList); }else { int index=timeList.indexOf(time); int value=valueList.get(index); valueList.set(index, value+1); sre.getServletContext().setAttribute("valueList", valueList); } } @Override public void contextDestroyed(ServletContextEvent sce) { // TODO Auto-generated method stub } @Override public void contextInitialized(ServletContextEvent sce) { // TODO Auto-generated method stub List timeList=new ArrayList(); List valueList=new ArrayList(); sce.getServletContext().setAttribute("timeList", timeList); sce.getServletContext().setAttribute("valueList", valueList); } }
package com.imooc.total; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.alibaba.fastjson.JSON; /** * Servlet implementation class RequestTotalServlet */ @WebServlet("/rt") public class RequestTotalServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public RequestTotalServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub ServletContext context=request.getServletContext(); List<String> timeList=(List)context.getAttribute("timeList"); List<Integer> valueList=(List)context.getAttribute("valueList"); response.setContentType("text/html;charset=utf-8"); /*response.getWriter().println(timeList.toString()); response.getWriter().println("<br/>"); response.getWriter().println(valueList.toString());*/ Map result=new HashMap(); result.put("timeList",timeList); result.put("valueList",valueList); String json=JSON.toJSONString(result); response.getWriter().println(json); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <script type="text/javascript" src="js/echarts.min.js"></script> <script type="text/javascript" src="js/jquery-3.5.1.min.js"></script> </head> <body> <div id="main" style="width:600px;heigth:400px;"></div> <script type="text/javascript"> function showChart(){ $.ajax({ url:"./rt", type:"get", datatype:"json"; success:function(json){ console.log(json.timeList); console.log(json.valueList); // 基于准备好的dom,初始化echarts实例 var myChart = echarts.init(document.getElementById('main')); // 指定图表的配置项和数据 var option = { title: { text: '请求流量分析统计' }, tooltip: {}, legend: { data:['访问量'] }, xAxis: { data: json.timeList }, yAxis: {}, series: [{ name: '访问量', type: 'bar', data:json.valueList }] }; // 使用刚指定的配置项和数据显示图表。 myChart.setOption(option); } }) } window.setInterval("showChart()",1000); </script> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>test page</title> </head> <body> <h1>I'm test page 1</h1> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>test page</title> </head> <body> <h1>I'm test page 2</h1> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>test page</title> </head> <body> <h1>I'm test page 3</h1> </body> </html>
源自:监听器
2-4 实现动态图表
20
收起
正在回答 回答被采纳积分+1
4回答
2. 从网页搭建入门JavaWeb
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星