老是我这个一直报错500
来源:2-1 请求流量分析统计-开发监听器
weixin_慕设计3058955
2020-01-05 15:52:55
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<String> timeList=new ArrayList<>();
List<Integer> 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);
sre.getServletContext().setAttribute("valueList", "valueList");
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>requesttotal</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>com.imooc.total.RequestTotalListener</listener-class>
</listener>
</web-app>
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 RequestTotalSerlvet
*/
@WebServlet("/req")
public class RequestTotalSerlvet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public RequestTotalSerlvet() {
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(valueList.toString());
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>I'm a test page 1!</h1>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>I'm a test page 2!</h1>
</body>
</html>


1回答
好帮手慕酷酷
2020-01-05
同学你好,根据报错信息是类型转换的异常。不能将String类型转换为List。根据报错定位到RequestTotalListener类的39行代码。
如:

发现在设置值的时候设置的是String字符串而不是List集合。应该设置timeList和valueList集合,删除双引号。
具体如下:


修改后如下:


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