视频讲的是不是有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);
}
}
正在回答
同学你好,测试同学的代码会出现课程老师演示的rt请求被拦截,这里同学的监听器类可按照课程进行修改及其完善。如下:
如果我的回答解决了你的疑惑,请采纳,祝学习愉快~
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星