请问老师这是怎么回事?

请问老师这是怎么回事?

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>

http://img1.sycdn.imooc.com//climg/5f03cef20936cd6102910445.jpg

http://img1.sycdn.imooc.com//climg/5f03cf1009b7d41011000644.jpg

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

4回答
好帮手慕阿慧 2020-07-07 15:37:11

同学你好,折线应该设置type的值为line,如下:

http://img1.sycdn.imooc.com//climg/5f0425fe09d35c7604490353.jpg

如果问题没有解决,请同学贴一下错误截图。

祝学习愉快~

好帮手慕阿慧 2020-07-07 14:04:56

同学你好,高度heigth拼写错了,应该是height。datatype中t应该大写,json后面应该是逗号。

参考代码如下:

http://img1.sycdn.imooc.com//climg/5f0410820962605a05640185.jpg

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

  • 提问者 qq_就是这样_1 #1
    老师,又有问题,就是X轴和y轴有了,但图表折线怎么没法显示
    2020-07-07 14:41:59
好帮手慕阿慧 2020-07-07 11:01:21

同学你好,timList写错了,应该是timeList。

参考代码如下:

http://img1.sycdn.imooc.com//climg/5f03e5720995fa3906780104.jpg

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

  • 提问者 qq_就是这样_1 #1
    老师,我改下之后,倒是没有报错了,但页面没有显示动态图,而是空白的
    2020-07-07 11:40:42
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师