请问老师为什么我的监听器在代码中断了?

请问老师为什么我的监听器在代码中断了?

package com.imooc.web.Listener;

import com.imooc.utils.LoginCache;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRequest;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.*;

@WebListener()
public class LoginSessionListener implements HttpSessionAttributeListener {
       private static  final String ExistUser="existUser";

   public void attributeAdded(HttpSessionBindingEvent sbe) {
       System.out.println("有用?");
       String attrName = sbe.getName();
       System.out.println(attrName);
      if (ExistUser.equals(attrName)){
          System.out.println("1112345");
          String attrVal = (String) sbe.getValue();
          System.out.println(attrVal);
          //是哪个Session在存储
          System.out.println("11123");
          HttpSession session = sbe.getSession();
          System.out.println(session);
          String sessionId = session.getId();
          System.out.println(sessionId+"222");
          String sessionId2 = LoginCache.getInstance().getSessionIdByUsername(attrVal);
          System.out.println(sessionId2+"333");
          System.out.println("11111");
          if(null == sessionId2){

          }else{
              HttpSession session1 = LoginCache.getInstance().getSessionBySessionId(sessionId2);
              //清除会话对象
              session1.invalidate();
          }
          LoginCache.getInstance().setSessionIdByUserName(attrVal,sessionId);
          LoginCache.getInstance().setSessionBySessionId(sessionId,session);
      }

   }

   public void attributeRemoved(HttpSessionBindingEvent sbe) {
     /* This method is called when an attribute
        is removed from a session.
     */
   }

   public void attributeReplaced(HttpSessionBindingEvent sbe) {
     /* This method is invoked when an attibute
        is replaced in a session.
     */
   }
}





package com.imooc.utils;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

public class LoginCache {
   private static LoginCache instance = new LoginCache();
   private Map<String,String> loginUserSession = new HashMap<String,String>();
   private Map<String, HttpSession> loginSession = new HashMap<>();

   public LoginCache() {
   }

   public static LoginCache getInstance() {
       return instance;
   }
   //通过用户名获得会话栏Id
   public String getSessionIdByUsername(String username){
       return loginUserSession.get(username);
   }
   //通过会话Id获得Session对象
   public HttpSession getSessionBySessionId(String sessionId){
       return loginSession.get(sessionId);
   }
   //存储username和sessionId功能
   public void setSessionIdByUserName(String username,String sessionId){
       loginUserSession.put(username,sessionId);
   }
   //存储sessionId和会话框的功能
   public void setSessionBySessionId(String sessionId,HttpSession session){
       loginSession.put(sessionId,session);
   }
}




D:\apache-tomcat-8.5.38\bin\catalina.bat run
[2019-04-15 11:15:56,196] Artifact shop:war exploded: Waiting for server connection to start artifact deployment...
Using CATALINA_BASE:   "C:\Users\吴南\.IntelliJIdea2018.3\system\tomcat\Unnamed_shop_2"
Using CATALINA_HOME:   "D:\apache-tomcat-8.5.38"
Using CATALINA_TMPDIR: "D:\apache-tomcat-8.5.38\temp"
Using JRE_HOME:        "C:\Program Files\Java\jdk1.8.0_201"
Using CLASSPATH:       "D:\apache-tomcat-8.5.38\bin\bootstrap.jar;D:\apache-tomcat-8.5.38\bin\tomcat-juli.jar"
15-Apr-2019 11:15:59.166 淇℃伅 [main] org.apache.catalina.startup.VersionLoggerListener.log Server version:        Apache Tomcat/8.5.38
15-Apr-2019 11:15:59.168 淇℃伅 [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:          Feb 5 2019 11:42:42 UTC
15-Apr-2019 11:15:59.168 淇℃伅 [main] org.apache.catalina.startup.VersionLoggerListener.log Server number:         8.5.38.0
15-Apr-2019 11:15:59.168 淇℃伅 [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:               Windows 10
15-Apr-2019 11:15:59.168 淇℃伅 [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version:            10.0
15-Apr-2019 11:15:59.169 淇℃伅 [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture:          amd64
15-Apr-2019 11:15:59.169 淇℃伅 [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home:             C:\Program Files\Java\jdk1.8.0_201\jre
15-Apr-2019 11:15:59.169 淇℃伅 [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version:           1.8.0_201-b09
15-Apr-2019 11:15:59.171 淇℃伅 [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor:            Oracle Corporation
15-Apr-2019 11:15:59.171 淇℃伅 [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE:         C:\Users\鍚村崡\.IntelliJIdea2018.3\system\tomcat\Unnamed_shop_2
15-Apr-2019 11:15:59.171 淇℃伅 [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:         D:\apache-tomcat-8.5.38
15-Apr-2019 11:15:59.171 淇℃伅 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=C:\Users\鍚村崡\.IntelliJIdea2018.3\system\tomcat\Unnamed_shop_2\conf\logging.properties
15-Apr-2019 11:15:59.172 淇℃伅 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
15-Apr-2019 11:15:59.172 淇℃伅 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote=
15-Apr-2019 11:15:59.172 淇℃伅 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.port=1099
15-Apr-2019 11:15:59.172 淇℃伅 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.ssl=false
15-Apr-2019 11:15:59.173 淇℃伅 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.password.file=C:\Users\鍚村崡\.IntelliJIdea2018.3\system\tomcat\Unnamed_shop_2\jmxremote.password
15-Apr-2019 11:15:59.173 淇℃伅 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.access.file=C:\Users\鍚村崡\.IntelliJIdea2018.3\system\tomcat\Unnamed_shop_2\jmxremote.access
15-Apr-2019 11:15:59.173 淇℃伅 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.rmi.server.hostname=127.0.0.1
15-Apr-2019 11:15:59.174 淇℃伅 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
15-Apr-2019 11:15:59.174 淇℃伅 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
15-Apr-2019 11:15:59.174 淇℃伅 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=
15-Apr-2019 11:15:59.174 淇℃伅 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=C:\Users\鍚村崡\.IntelliJIdea2018.3\system\tomcat\Unnamed_shop_2
15-Apr-2019 11:15:59.174 淇℃伅 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=D:\apache-tomcat-8.5.38
15-Apr-2019 11:15:59.175 淇℃伅 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=D:\apache-tomcat-8.5.38\temp
15-Apr-2019 11:15:59.175 淇℃伅 [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded APR based Apache Tomcat Native library [1.2.21] using APR version [1.6.5].
15-Apr-2019 11:15:59.175 淇℃伅 [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
15-Apr-2019 11:15:59.175 淇℃伅 [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
15-Apr-2019 11:15:59.185 淇℃伅 [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized [OpenSSL 1.1.1a  20 Nov 2018]
15-Apr-2019 11:15:59.769 淇℃伅 [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
15-Apr-2019 11:15:59.798 淇℃伅 [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
15-Apr-2019 11:15:59.830 淇℃伅 [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-nio-8009"]
15-Apr-2019 11:15:59.838 淇℃伅 [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
15-Apr-2019 11:15:59.839 淇℃伅 [main] org.apache.catalina.startup.Catalina.load Initialization processed in 1460 ms
15-Apr-2019 11:15:59.918 淇℃伅 [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
15-Apr-2019 11:15:59.918 淇℃伅 [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.5.38
15-Apr-2019 11:15:59.931 淇℃伅 [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
15-Apr-2019 11:15:59.948 淇℃伅 [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]
15-Apr-2019 11:15:59.956 淇℃伅 [main] org.apache.catalina.startup.Catalina.start Server startup in 117 ms
Connected to server
[2019-04-15 11:15:59,996] Artifact shop:war exploded: Artifact is being deployed, please wait...
15-Apr-2019 11:16:00.574 璀﹀憡 [RMI TCP Connection(3)-127.0.0.1] org.apache.tomcat.util.descriptor.web.WebXml.setVersion Unknown version string [4.0]. Default version will be used.
15-Apr-2019 11:16:01.534 淇℃伅 [RMI TCP Connection(3)-127.0.0.1] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
[2019-04-15 11:16:01,711] Artifact shop:war exploded: Artifact is deployed successfully
[2019-04-15 11:16:01,711] Artifact shop:war exploded: Deploy took 1,715 milliseconds
UTF-8
15-Apr-2019 11:16:09.933 淇℃伅 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [D:\apache-tomcat-8.5.38\webapps\manager]
15-Apr-2019 11:16:10.085 淇℃伅 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [D:\apache-tomcat-8.5.38\webapps\manager] has finished in [131] ms
UTF-8
UTF-8
UTF-8
UTF-8
UTF-8
UTF-8
UTF-8
UTF-8
login
aaa  111
15-Apr-2019 11:16:19.952 淇℃伅 [http-nio-8080-exec-10] com.mchange.v2.log.MLog.<clinit> MLog clients using java 1.4+ standard logging.
15-Apr-2019 11:16:20.215 淇℃伅 [http-nio-8080-exec-10] com.mchange.v2.c3p0.C3P0Registry.banner Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]
15-Apr-2019 11:16:20.660 淇℃伅 [http-nio-8080-exec-10] com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getPoolManager Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 1hgery9a28llt6vwy6az4|41aab9ba, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hgery9a28llt6vwy6az4|41aab9ba, idleConnectionTestPeriod -> 0, initialPoolSize -> 5, jdbcUrl -> jdbc:mysql:///jdbctest?characterEncoding=utf-8, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 20, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ]
有用?
existUser
1112345
UTF-8
findAll
CategoryServlet的方法执行了
CategoryService的方法执行了
CategoryDao的findAll方法执行了...
UTF-8

正在回答

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

3回答

打开MySQL的安装目录  apache-tomcat-8.5.40\conf\logging.properties  的文件

ava.util.logging.ConsoleHandler.level = FINE

java.util.logging.ConsoleHandler.formatter = org.apache.juli.OneLineFormatter

java.util.logging.ConsoleHandler.encoding = UTF-8

我用sublime打开 是47行

最后一个换成单词换成GBK 可以解决日志乱码  0.0

好帮手慕阿莹 2019-04-17 16:20:04

同学调用的是idea吗?同学可以在监听器上想要查看的地方打上断点:

在对应的行数单击就可以

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

然后点击这个

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

按钮执行。

可以通过F8进行下一步,

按F8 在 Debug 模式下,进入下一步,如果当前行断点是一个方法,则不进入当前方法体内,跳到下一条执行语句。

按F7在 Debug 模式下,进入下一步,如果当前行断点是一个方法,则进入当前方法体内,如果该方法体还有方法,则会进入该内嵌的方法中 .

跳出该方法,可以按Shift+F8,在 Debug 模式下,跳回原来地方。

这时我们按F8,会继续执行下一条语句。

建议同学按照这个方法跟踪一下,Debug模式下,

http://img1.sycdn.imooc.com//climg/5cb6e18a0001d1a809390200.jpg还会显示这个变量的具体值。

请同学按照这个方式试一下。因为同学的代码不全,老师不好测试。

祝学习愉快。

  • 提问者 慕盖茨8087740 #1
    谢谢,老师我先把作业做完,就去调试试一下
    2019-04-17 18:14:59
  • 提问者 慕盖茨8087740 #2
    谢谢老师,我先把作业做完,就去调试试一下
    2019-04-17 18:15:12
  • 提问者 慕盖茨8087740 #3
    谢谢老师,我先把作业做完,就去调试试一下
    2019-04-17 18:15:13
好帮手慕阿莹 2019-04-15 15:24:00

同学的断了是什么意思呢?是说

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

没有执行后边的输出语句吗?还是什么意思呢?同学可以再描述一下吗?

祝学习愉快。


  • 提问者 慕盖茨8087740 #1
    是啊老师,后面语句没有执行,导致换个浏览器可以同时执行
    2019-04-15 15:27:32
  • 好帮手慕阿莹 回复 提问者 慕盖茨8087740 #2
    确实很奇怪,同学有没有打断点跟一下呢?看看是否执行了 String attrVal = (String) sbe.getValue();这一句呢?看起来System.out.println(attrVal);也没有执行到。请同学打断点跟一下,并且,把idea重启后再试一下。如果还是不行,请同学贴一下你的其他的代码,例如jsp
    2019-04-15 17:32:12
  • 提问者 慕盖茨8087740 回复 好帮手慕阿莹 #3
    好的,我抽空试试,谢谢
    2019-04-15 18:44:12
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

在线咨询

领取优惠

免费试听

领取大纲

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