File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes calling  a servlet from a seperate thread class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "calling  a servlet from a seperate thread class?" Watch "calling  a servlet from a seperate thread class?" New topic

calling a servlet from a seperate thread class?

manu chandra

Joined: Oct 08, 2012
Posts: 15
Hi i am doing a web application . In that i need to set reminders and when the time comes it should popup a message. In the login servlet i check whether there are reminders. if there are then i call a java class in a separate thread and i schedule the time. after the time i need to call a servlet and from there i need to redirect to jsp page. My problem is I after the designed time I use THis to call Servlet

URL url=new URL("url path");
and the system.out.println() in the servlet is working but if i do a response.sendredirect it wont work.

i need to some how send a pop up the user..

Can any one help me with this?
Jayesh A Lalwani
Saloon Keeper

Joined: Jan 17, 2008
Posts: 2746

You cannot do this with a thread running on the server. The thread has no way of communicating with the browser on the client side. You will have to do this using JavaScript running o. The clients browser. Either use a timer or keep regularly polling the server to check if a popup needs to be displayed.
manu chandra

Joined: Oct 08, 2012
Posts: 15
Acually the senario is that the user logs in sets reminder. if the reminder is same date it should start timer and when timer stops means schdules i need to pop up.

and when the user log in i need to check for the remainder. so this must be done in back graound right .. in which jsp page can i use timer function?
manu chandra

Joined: Oct 08, 2012
Posts: 15
And i forgot to tell.. The users will have to do there work . so this timer must not disturb other activities. like adding clients information and others
Emanuel Kadziela
Ranch Hand

Joined: Mar 24, 2005
Posts: 187
As the other comment alluded, you will need to do something not just on the server, but in the browser as well. The way the client/server interaction works is that only the browser (client) can make requests from the server, and the server can respond to them, but the server cannot initiate an interaction with the browser, it can only respond to requests initiated by the client.

Reverse Ajax works around this issue by either keeping very long running requests (virtually "never" timing out) with the server, or by polling (repeatedly asking the server if it has anything to say to the client). That's the Javascript (ajax) piece. There are libraries which implement this kind of thing, such as DWR or Lightstreamer (both, I believe, available in basic versions for free). This Javascript function running in the browser will interact with a servlet running on the server and keep asking it if the user needs to be warned that a timer has expired. It can then do whatever necessary to warn the user (pop-up an alert, redirect, etc.).

Another way to do this would be to just keep all of this in javascript running in the browser. If it is as simple as a user logging in and setting up reminders, this can all be done in the browser, and there is no need to talk to the server. However, if the requirements are more complex (if one user can set reminders for another user, for example), then the server interaction might have to be implemented.
I agree. Here's the link:
subject: calling a servlet from a seperate thread class?
It's not a secret anymore!