简体   繁体   English

如何以编程方式配置Tomcat的会话跟踪模式

[英]How to configure programatically the session tracking modes for Tomcat

I'm using an embedded tomcat server version 7. I want to programmatically configure the server with the session tracking mode (in fact I search a way to get rid of jsessionid ie neither in the cookies nor in the url). 我正在使用嵌入式tomcat服务器版本7.我想以编程方式使用会话跟踪模式配置服务器(实际上我搜索了一种方法来摆脱jsessionid,即既不在cookie中也不在URL中)。 But there is no a method in org.apache.catalina.Context to simply configure this property like in a web.xml descriptor file (I cannot use such file). 但是在org.apache.catalina.Context中没有一个方法可以像在web.xml描述符文件中那样简单地配置这个属性(我不能使用这样的文件)。 So what is the best way to do it? 那么最好的方法是什么?

I tried the above code 我尝试了上面的代码

Context ctxt = tomcat.addWebapp(.......);
ctxt.getServletContext().setSessionTrackingModes(
        my_modes);

But this method failed with the error 但是这种方法因错误而失败

java.lang.IllegalStateException: The session tracking modes for context / cannot be set whilst the context is running
at org.apache.catalina.core.ApplicationContext.setSessionTrackingModes(ApplicationContext.java:1235)
at org.apache.catalina.core.ApplicationContextFacade.setSessionTrackingModes(ApplicationContextFacade.java:611)

I tried to use https://tomcat.apache.org/tomcat-7.0-doc/api/org/apache/catalina/Context.html#addApplicationListener(java.lang.String) . 我尝试使用https://tomcat.apache.org/tomcat-7.0-doc/api/org/apache/catalina/Context.html#addApplicationListener(java.lang.String) The listener on contextInitialized set the tracking mode contextInitialized上的监听器设置跟踪模式

servletContextEvent.getServletContext().setSessionTrackingModes(
        EnumSet.copyOf(sessionTrackingModes));

But I don't know how to configure tomcat with an instance of this listener because with the last method Context.addApplicationListener the method takes the className of the listener and when running the server fails to instantiate my listener because the absence of an empty constructor (my listener constructor takes the list of sessionTrackingMode.. cannot be configurable) 但我不知道如何使用此侦听器的实例配置tomcat,因为使用上一个方法Context.addApplicationListener,该方法采用侦听器的className,并且在运行服务器时无法实例化我的侦听器,因为缺少空构造函数(我的监听器构造函数获取sessionTrackingMode列表..无法配置)

[read-1] ERROR Aug 18 19:10:03 - Error configuring application listener of class xx.xx.xx.SessionTrackingListener
java.lang.InstantiationException: xx.xx.xx.SessionTrackingListener
at java.lang.Class.newInstance(Class.java:427)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:125)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4715)
..............
Caused by: java.lang.NoSuchMethodException: xx.xx.xx.xx.SessionTrackingListener.<init>()
at java.lang.Class.getConstructor0(Class.java:3082)
at java.lang.Class.newInstance(Class.java:412)

So do you suggest a better way to do it 所以你建议一个更好的方法来做到这一点

Use a SessionTrackingListener. 使用SessionTrackingListener。 The problem with the one you are using is that it doesn't have a default constructor. 您正在使用的问题是它没有默认构造函数。 It should work if you add a default constructor to it. 如果您向其添加默认构造函数,它应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM