老师帮忙解决一下

老师帮忙解决一下

package com.imooc.listener;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.SimpleFormatter;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;

public class requestListener implements ServletContextListener,ServletRequestListener{

    @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 nowtime = sdf.format(date);
//        如果时间列表中有当前时间,则访问次数加一,没有则时间列表加入当前时间,并初始化为1
        if(timeList.indexOf(nowtime)==-1) {
            timeList.add(nowtime);
            valueList.add(1);
            sre.getServletContext().setAttribute("timeList", timeList);
            sre.getServletContext().setAttribute("valueList", valueList);
        }else {
            int index = timeList.indexOf(nowtime);
            int value = valueList.get(index);
            valueList.set(index, value+1);
            sre.getServletContext().setAttribute("timeList", timeList);
            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);
        }

}




<?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>Listener</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.listener.requestListener</listener-class>
  </listener>
</web-app>



package com.imooc.listener;

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 tolalServlet
 */
@WebServlet("/s1")
public class tolalServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public tolalServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext ct = request.getServletContext();
        List<String> timeList = (List<String>) ct.getAttribute("timeList");
        List<Integer> valueList = (List<Integer>) ct.getAttribute("valueList");
        response.getWriter().println(timeList);
        response.getWriter().println("</br>");
        response.getWriter().println(valueList);
    }

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

}



HTTP Status 500 – Internal Server Error


Type Exception Report

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

java.lang.NullPointerException
com.imooc.listener.requestListener.requestInitialized(requestListener.java:34)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:616)
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:831)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1634)
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:748)

Note The full stack trace of the root cause is available in the server logs.


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

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

1回答
Java小彩鸡 提问者 2021-01-27 09:53:17

老师,解决了,一个单词大小写的问题

  • 同学自己就解决了问题,棒棒哒!继续加油!

    祝学习愉快!

    2021-01-27 10:55:20
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

在线咨询

领取优惠

免费试听

领取大纲

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