Hi all, my question is the following: I have a servlet which communicates with a remote device to issue queries. I'd like to have a thread for each of the users that log into the web site (started when they first use the servlet) which will check how many requests are coming from this user's IP address, so that I can do some hammering prevention. The way I've thought about it, the thread would run for the length of the session, probably keeping a counter and checking the time intervals between the requests. How would you go about doing that? How do you create a thread just once on the first request, and then use the same thread until the session is over?? Any help would be greatly appreciated... Thanks guys
The service() method of the servlet is by default multithreaded( to make it single thread u got to implement singleThread model). U dont have to create a new thread right ?. u can store the hits by that user either in the session or if u want to monitor them even after the session is over u store them in a hashtable and monitor it. Hope this is clear Cherry
Joined: Jul 12, 2001
Thanks for the reply... what I have come to during this half hour has confused me a liitle bit... To figure out how things work, I had a simple counter, which I incremented every time the servlet was called... and so it did.. the problem is that having increased the counter several times, I tried to request the servlet from another IP address, and the counter was not reset for that machine.... Isn't it supposed to?? I thought so... Anyway, what I am doing now is I have a vector of User objects, and I filter the requesting IP and register them. Each user has a request counter... The problem is that somehow it seems now that the Vector is reinstantiated each time the servlet loads... I am confused! :-)) I will try to sort that out myself, however if you think you can shed some more light into it I would be very grateful... Thanks
please note that there is(usually) only one instance of your servlet in the server and one new thread for each request so if your counter is initiallized like an instance variable or in the init method it�s not gonna work and all the clients will see the same value I think that must be the case.
Joined: Jul 12, 2001
session is associate with request, so use session.setAttribute("counter",new Integer(count)) , everybody should get different value, right?
OK guys thanks for your help... problem solved, my question now is, I have a thread being started from the servlet which does operations on a static vector of the servlet... It is only being started once (if !isAlive()) but I was wondering... IF LOTS OF USERS LOG ON TO THE SITE SIMULTANEOUSLY, WILL A NEW THREAD BE INSTANTIATED FOR EVERY ONE OF THEM...?? NOTE: I cannot make it static, as I pass it "this" (servlet) as a parameter... (which is not static...) I just want to have one thread, which will run for as long as the servlet is alive JUST ONE! Any suggestions or advice?? Thanks in advance... Aris