老师帮忙检查下,运行的时候好像访问的数量并没有增加

来源:2-2 请求流量分析统计-开发统计查询Servlet

罗杰明

2020-02-21 10:52:07

package com.imooc.total;


import java.io.IOException;

import java.util.List;


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;


/**

 * 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 {

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());

}


/**

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

}


}



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;


public class RequestTotalListener implements ServletContextListener,ServletRequestListener{


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

}


@Override

public void requestDestroyed(ServletRequestEvent sre) {

// TODO Auto-generated method stub

}


@Override

public void requestInitialized(ServletRequestEvent sre) {

// TODO Auto-generated method stub

List<String> timeList=(List)sre.getServletContext().getAttribute("timeList");

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

}

}


}


写回答

1回答

好帮手慕小班

2020-02-21

同学你好,检查同学代码,在RequestTotalListener中时间的获取有一个小问题,修改建议如下:

http://img.mukewang.com/climg/5e4f81da0958815806610082.jpg

如上修改后,同学再来尝试一下,时间的获取有误导致监听器获取内容有误。另外,同学需要添加url的判断,例如:

http://img.mukewang.com/climg/5e4f83610910775107670156.jpg

修改后代码如下:

@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
		//TimeList: 10:02  10:03  10:04   10:05
		//ValueList:  5       7      10     2
		List<String> timeList = (List)sre.getServletContext().getAttribute("timeList");
		
		
		List<Integer> valueList = (List)sre.getServletContext().getAttribute("valueList");
		
		
		Date date = new Date();
		//Date date=new Date();
		
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
		//SimpleDateFormat sdf=new SimpleDateFormat("HH:MM");
		
		String time = sdf.format(date);
		//10:05
		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);
		}
		
	}

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

0

0 学习 · 9666 问题

查看课程