视频讲的是不是有bug,如果一直访问/rt,原始的页面数据内容是不会变的,我改了之后代码如下对不对?

视频讲的是不是有bug,如果一直访问/rt,原始的页面数据内容是不会变的,我改了之后代码如下对不对?

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

<script type="text/javascript" src="js/jQuery.js"></script>

<script type="text/javascript" src="js/echarts.min.js"></script>

</head>

<body>

<div id="main" style="width: 600px;height:400px;"></div>

 

<script type="text/javascript">


       function showChart(){

$.ajax({

"url":"./rt",

"type":"get",

"data":"",

"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: 'line',

                data: json.valuelist

            }]

        };


        // 使用刚指定的配置项和数据显示图表。

        myChart.setOption(option);

   

}

})

}

   

window.setInterval("showChart()",1500);


</script>


</body>

</html>



package com.date.tatol;


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 RequestTatol implements ServletContextListener,ServletRequestListener{


@Override

public void requestDestroyed(ServletRequestEvent req) {

// TODO Auto-generated method stub

}


@Override

public void requestInitialized(ServletRequestEvent req) {

// TODO Auto-generated method stub

HttpServletRequest request=(HttpServletRequest)req.getServletRequest();

String url=request.getRequestURL().toString();

List<String> timelist=(List)req.getServletContext().getAttribute("timelist");

List<Integer> valuelist=(List) req.getServletContext().getAttribute("valuelist");

Date d1=new Date();

SimpleDateFormat sdf=new SimpleDateFormat("HH:mm");

String time=sdf.format(d1);

if(url.endsWith("/rt")) {

if(timelist.indexOf(time)==-1) {

timelist.add(time);

valuelist.add(0);

req.getServletContext().setAttribute("timelist", timelist);

req.getServletContext().setAttribute("valuelist", valuelist);

}else {

return;

}

}else {


if(timelist.indexOf(time)==-1) {

timelist.add(time);

valuelist.add(1);

req.getServletContext().setAttribute("timelist", timelist);

req.getServletContext().setAttribute("valuelist", valuelist);

}else {

int index=timelist.indexOf(time);

int value=valuelist.get(index);

valuelist.set(index, value+1);

req.getServletContext().setAttribute("valuelist", valuelist);

}

}

}


@Override

public void contextDestroyed(ServletContextEvent ser) {

// TODO Auto-generated method stub

}


@Override

public void contextInitialized(ServletContextEvent ser) {

// TODO Auto-generated method stub

List timelist=new ArrayList();

List valuelist=new ArrayList();

ser.getServletContext().setAttribute("timelist",timelist);

ser.getServletContext().setAttribute("valuelist",valuelist);

}


}



 package com.date.tatol;


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 DatatatolServlet

 */

@WebServlet("/rt")

public class DatatatolServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public DatatatolServlet() {

        super();

        // TODO Auto-generated constructor stub

    }


/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

*/

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

List<String> timelist=(List)request.getServletContext().getAttribute("timelist");

List<String> valuelist=(List)request.getServletContext().getAttribute("valuelist");

Map map=new HashMap();

map.put("timelist", timelist);

map.put("valuelist", valuelist);

System.out.println(map);


        String json=JSON.toJSONString(map);

        System.out.println(json);

response.getWriter().println(json);

response.setContentType("text/html;charset=UTF-8");


}


/**

* @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);

}


}


正在回答

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

3回答

同学你好,测试同学的代码会出现课程老师演示的rt请求被拦截,这里同学的监听器类可按照课程进行修改及其完善。如下:

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

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

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

  • 大圣归来² 提问者 #1
    如果网站没有被访问,整个流量显示图表就暂停了,时间轴上的数一直停留在原始时刻了呢。而不会出现随着时间改变,访问量 为0。想要出现每时每刻都显示应该怎么改?
    2020-08-12 13:03:58
好帮手慕小脸 2020-08-12 19:01:37

同学你好,这里同学可以测试运行代码:

访问html页面,从某个时间(16:04)开始至某个时间(16:08)之间不访问,超出16:20后的这个时间范围后,再去访问时,之前的时间段里,访问量都为0,且可以显示未访问的那段时间。

祝学习愉快~

好帮手慕小脸 2020-08-12 16:07:56

同学你好,首先需要在网站上访问http://localhost:8080/request-total/total.html后,才会展示图片,其次访问量是会变化的而不是固定为0。

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

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

如果不是上述老师理解的意思,建议同学描述清楚或截图贴出

祝学习愉快~

  • 提问者 大圣归来² #1
    如果一直没有访问量,图中的时间轴的时间是不变的吧,比如说16:06以后就不访问了,那么图表就一直是16:06以前的数据了吧。也就是说这个表不在是动态了的,可是想要16:06以后,虽然没有访问量,但也能动态的显示当时的时间,当然对应的访问量 是0
    2020-08-12 17:01:14
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

在线咨询

领取优惠

免费试听

领取大纲

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