Get your CodeRanch badge!*
The moose likes Servlets and the fly likes Servlets + Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlets + Threads" Watch "Servlets + Threads" New topic
Author

Servlets + Threads

Aris Petridis
Greenhorn

Joined: Jul 12, 2001
Posts: 5
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
Cherry Mathew
Ranch Hand

Joined: Dec 26, 2000
Posts: 159
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
Aris Petridis
Greenhorn

Joined: Jul 12, 2001
Posts: 5
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
hahahaforever
Greenhorn

Joined: Jul 12, 2001
Posts: 3
why not just put the counter in the session?
Marcos Maia
Ranch Hand

Joined: Jan 06, 2001
Posts: 977
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.
hahahaforever
Greenhorn

Joined: Jul 12, 2001
Posts: 3
session is associate with request, so use session.setAttribute("counter",new Integer(count)) , everybody should get different value, right?
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
"hahahaforever":
Please read our username policy ( http://www.javaranch.com/ubb/Forum10/HTML/000180.html )and re-register at your earliest convenince.
Thanks.
---
Sheriff Tony
[This message has been edited by Tony Alicea (edited July 12, 2001).]


Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
wenwei cheng
Greenhorn

Joined: Jul 12, 2001
Posts: 5
sorry, Sheriff
Aris Petridis
Greenhorn

Joined: Jul 12, 2001
Posts: 5
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlets + Threads
 
Similar Threads
understanding threads - please help
Stateful EJB internal mechanism
[NX]STuPiD questions HELP !!!!
3yrs experience want to know about questions asked on interviews on servlet and jsp
Retrieving data in session