You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Michael <sg...@gmx.net> on 2007/11/01 15:21:37 UTC

Usage of ContainerServlet

Hi,

I am kinda lost here.

My goal is to retrieve all session from the Context and purge idle sessions.

devel machine: TC 5.5.25, XP SP2, JRE 6.0.3
server machine: TC 5.5.23, HP-UX 11, JRE 5.0.7

I've taken a look in to the source manager app and noticed, it has to 
implement the ContainerServlet to get the Wrapper which supplies me with 
all necessary objects.

Well, I did the same. Wrapper remains null. I did a google and mailing 
list search and I've read that my Context has to be true either in my 
context.xml or in the server.xml
Still no avail. To my understanding the server folder in tomcat has more 
rights due to the privileged flag.

Hence I have no idea how to manage that. I use lambda probe to monitor 
my tomcat and it is a simple war which does the same as the manager 
although it isn't in the server folder but hat the privileged flag set.

Can someone give me pointer how to cope with that, what I have missed here?

This is my basic code:

public class SessionServlet extends HttpServlet implements 
ContainerServlet {

	private Wrapper wrapper;
	
	public Wrapper getWrapper() {
		return wrapper;
	}

	public void setWrapper(Wrapper wrapper) {
		this.wrapper = wrapper;
		System.out.println("Wrapper set");

	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		resp.setCharacterEncoding("UTF-8");
		
		PrintWriter pw = resp.getWriter();
		
		pw.print("Liste:\n");
		//pw.print(wrapper.getInfo());
		pw.print(wrapper);
		pw.close();
		
	}


wrapper returns null

It seems like I have to manage the sessions in a HashMap with a 
SessionListener.

thx,

Mike
-- 
<NO> OOXML - Say NO To Microsoft Office broken standard
http://www.noooxml.org

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Usage of ContainerServlet

Posted by Bill Barker <wb...@wilshire.com>.
"Michael" <sg...@gmx.net> wrote in message news:472A16CE.2020505@gmx.net...
> Addition:
>
> Still I'd like to know which mistake I have done?!
>

I'm guessing that your context isn't privileged and/or you have copied 
ContainerServlet to someplace where it isn't in the same classloader as 
server/lib.

However, if you really want to do this like the Manager webapp, then it is 
probably easier to implement this with JMX (of course, this is still 
Tomcat-specific).  It is probably easier to just hook up JConsole and look 
at the MBeans with type=Manager to see how to do this.

> Mike
> -- 
> <NO> OOXML - Say NO To Microsoft Office broken standard
> http://www.noooxml.org
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
> 




---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Usage of ContainerServlet

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Mike,

Michael wrote:
> Addition:
> 
> Still I'd like to know which mistake I have done?!

Sorry, I can't help you with that. I stay away from Tomcat internals
pretty much at all costs.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFHKiDz9CaO5/Lv0PARApNnAJ9HsG2vCjwqBp8+L2aiPaNXIjYLYwCfR8i0
dv63G0GMY4O5fcxAS9uIMwA=
=hOvh
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Usage of ContainerServlet

Posted by Michael <sg...@gmx.net>.
Addition:

Still I'd like to know which mistake I have done?!

Mike
-- 
<NO> OOXML - Say NO To Microsoft Office broken standard
http://www.noooxml.org

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Usage of ContainerServlet

Posted by Michael <sg...@gmx.net>.
Christopher Schultz wrote:
>[...]
> So, you have several options. My personal recommendation (and the only
> option I'll cover) is a do-it-yourself approach. Working with Tomcat
> internals is tedious and you are likely to lock yourself into a
> particular version of Tomcat because the APIs are not necessarily fixed.
>[Solution...]

Hi Christopher,

thank you for your very elaborated explanation. Your manual approach 
matches to 90 %+ to my approach besides the direct manipulation.
I didn't like the direct manipulation anyway. My boss and a coworker 
pushed me to investigate how this can me done in the manager-app manner.

my basic idea was some hashmap woth appcreated/sessioncreated listener 
anyway:

1. create a hashmap and put into app context
2. session created, check in map and do filtering

basically the same you proposed.

Thanks again,

Mike
-- 
<NO> OOXML - Say NO To Microsoft Office broken standard
http://www.noooxml.org

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Usage of ContainerServlet

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Mike,

Wizard of OS wrote:
> During a day we have several hundred users which could produce 15-20
> session per user.

Ouch.

> Session timeout is set to 11 hours which is part of
> work contract limitations.

OUCH!

> This can't be changed. Every session can
> have a few hundred kilobytes but that number isn't certain because we
> didn't make a complete test yet. The issue basically is that we have
> to track what users do to track down errors during some processes and
> avoid maintance clutter by zombie sessions.

Hmm.

> I hope this made it clearer.

Yes, it makes it much clearer.

So, you have several options. My personal recommendation (and the only
option I'll cover) is a do-it-yourself approach. Working with Tomcat
internals is tedious and you are likely to lock yourself into a
particular version of Tomcat because the APIs are not necessarily fixed.

If you do it yourself (in my way), you can create a solution that will
work across all versions of all servlet containers and application servers.

Here's the trick: write yourself an HttpSessionListener.

HttpSessionListener (in case you didn't know) receives session "create"
and "destroy" events. All you have to do is implement the
HttpSessionListener interface in a class and then register it as a
<listener> in your web.xml file and it will receive the events.

When a session is created, store the session in a master list that you
maintain (I would recommend a synchronized Map or List). When one is
destroyed, remove it.

Each session will need to know its own IP address, so you'll need to be
recording that information at some point. You could do this with a
Filter, or, if you already have that capability built-into your
application you don't have to worry about it.

Finally, then it's time to run your cleanup, you just traverse all of
the recorded sessions looking at "old" ones
(session.getLastAccessedTime) for a particular IP address
(session.getAttribute("ip.address.that.we.put.in.there.before")). Feel
free to call "invalidate" on any of them.

You can even combine the HttpSessionListener into the object that does
the session harvesting. If I were doing this, I'd write a class like this:

public class MySessionManager
   implements HttpSessionListener
{
    public void removeOldSessions(String sessionId);

    // HttpSessionListener event handlers:

    public void sessionCreated(HttpSessionEvent ev);
    public void sessionDestroyed(HttpSessionEvent ev);
}

You won't be able to access the "removeOldSessions" method on the
correct object unless you can get to it from somewhere else. I would
recommend coding the sessionCreated() method to check the ServletContext
(application scope) for a copy of itself (say, under the
"MySessionManager" key), and install itself if it's not there.

Then, anywhere in your application that has access to the ServletContext
has access to the running MySessionManager object and can call the
removeOldSessions method.

Note that if there is no MySessionManager object in the ServletContext,
there will be no need to remove sessions (because none have been
created). Therefore, it's safe to do this lazy-style insertion of
MySessionManager into the application scope.

Hope that helps,
- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFHKg0u9CaO5/Lv0PARAipMAJ0UddEZRyUt7UYRDQbAOghk+AQccACguJ8d
vXCyUEY8/BJaTWeIbwKYiCE=
=imyb
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Usage of ContainerServlet

Posted by Wizard of OS <sg...@gmx.net>.
Hi Christopher,
> 
> Mike,
> 
> > A timeout is set but I want to purge all session for the same IP
> > which are older than the most current session.
> 
> Are you observing a lot of sessions being created by a single remote
> client? How many? 2 or 3, or 100 or so? What is your session timeout?
> How much stuff do you have laying around in the session?
> 
> I'm just curious what's happening to you. Are you sure you need
> something like this?

During a day we have several hundred users which could produce 15-20 session per user. Session timeout is set to 11 hours which is part of work contract limitations. This can't be changed. Every session can have a few hundred kilobytes but that number isn't certain because we didn't make a complete test yet.
The issue basically is that we have to track what users do to track down errors during some processes and avoid maintance clutter by zombie sessions.

I hope this made it clearer.

Mike
-- 
Psssst! Schon vom neuen GMX MultiMessenger gehört?
Der kann`s mit allen: http://www.gmx.net/de/go/multimessenger

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Usage of ContainerServlet

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Mike,

sgfan@gmx.net wrote:
>> Michael wrote:
>>> My goal is to retrieve all session from the Context and purge
>>> idle sessions.
>> 
>> May I ask why Tomcat's built-in session-purging capability is not 
>> sufficient?
> 
> Which one do you refer to exactly? Are you talking about session
> timeout?

Yes, that's what I'm talking about.

> A timeout is set but I want to purge all session for the same IP
> which are older than the most current session.

Are you observing a lot of sessions being created by a single remote
client? How many? 2 or 3, or 100 or so? What is your session timeout?
How much stuff do you have laying around in the session?

I'm just curious what's happening to you. Are you sure you need
something like this?

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFHKfHj9CaO5/Lv0PARAqUHAKC+Flrnf41YsW1FS5EXCAneb1SQfwCgp0Hv
cilSo3gZMZ+a6gFSjO2mloE=
=pWap
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Usage of ContainerServlet

Posted by sg...@gmx.net.
> Michael wrote:
> > I am kinda lost here.
> > 
> > My goal is to retrieve all session from the Context and purge idle
> > sessions.
> 
> May I ask why Tomcat's built-in session-purging capability is not
> sufficient?

Which one do you refer to exactly?
Are you talking about session timeout?

A timeout is set but I want to purge all session for the same IP which are older than the most current session.

bye
-- 
Ist Ihr Browser Vista-kompatibel? Jetzt die neuesten 
Browser-Versionen downloaden: http://www.gmx.net/de/go/browser

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Usage of ContainerServlet

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Michael,

Michael wrote:
> I am kinda lost here.
> 
> My goal is to retrieve all session from the Context and purge idle
> sessions.

May I ask why Tomcat's built-in session-purging capability is not
sufficient?

- -chris

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFHKeTd9CaO5/Lv0PARAvVxAJ9QbfiXYg9y0ArPIHmiVwcYbampWACfW86v
fZH/rxGu6qwoaYETUN65Wd0=
=qMN0
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org