wood burning stoves 2.0*
The moose likes Servlets and the fly likes How to stop a servlet from executing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to stop a servlet from executing" Watch "How to stop a servlet from executing" New topic
Author

How to stop a servlet from executing

igwe kalu kalu ogba
Ranch Hand

Joined: Feb 03, 2005
Posts: 133
Hi, I want to stop a servlet from running using the doPost method.

hence, my html would contain -
<FORM name="form3 " method="POST" action="MessageService">

and my doPost method would contain a method to completely stop the servlet code from executing. How do I go about this? I've tried system.exit(0) but it doesn't seem to work. thanks.
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
If you don't override the doPost() method, it won't do anything. It will throw a ServletException stating that POST method not allowed or something like that.


James Carman, President<br />Carman Consulting, Inc.
igwe kalu kalu ogba
Ranch Hand

Joined: Feb 03, 2005
Posts: 133
that is what i meant. But i'm asking, what method do i use to completely stop a servlet from executing. System.exit(0) doesn't seem to work. is there a way to stop a servlet from running? thanks
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

System.exit(0) is almost never acceptable in a webapp (although it will put an end to your servlet ).

Are you looking for a way to toggle a particular servlet on and off at runtime?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
There certainly is - just have your doPost method throw an UnavailableException (in the javax.servlet package) You can either make it permanently stopped or unavailable for NN seconds. Subsequent requests to the servlet will get an unavailable message.

If you are running Tomcat you can use the Management interface to stop/restart a specific "web application" - which may be any number of servlets and JSP.

Bill
igwe kalu kalu ogba
Ranch Hand

Joined: Feb 03, 2005
Posts: 133
how about the servlet destroy() method. will that equally work??
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

No, servletDestroy is called by the container.
It doesn't actually destroy anything.
Yuriy Zilbergleyt
Ranch Hand

Joined: Dec 13, 2004
Posts: 429
how about a simple return in the doPost() whereever you want execution to stop?

-Yuriy
todd runstein
Ranch Hand

Joined: Feb 15, 2005
Posts: 64
What exactly are you trying to do - or rather, why are you trying to do this? Are you trying to interrupt a long running task that may not be needed any longer? Do you want to lock users out from a specific servlet while leaving the rest of the application running? Knowing what you're trying to accomplish will make it easier for people to recommend useful solutions.
igwe kalu kalu ogba
Ranch Hand

Joined: Feb 03, 2005
Posts: 133
Well, I'm trying to stop the servlet completely. It is actually a servlet that sends messages, the idea is that the user can simply stop the messages from being sent meaning that the entire servlet stops.

Yuri, should I simply add return to the bottom or is it return 0 or something similar. Thanks.
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
So, during one request, you send some "messages" and the client's browser is sitting there waiting for it to return? And, you want another browser to send in another request to stop the messages?
Yuriy Zilbergleyt
Ranch Hand

Joined: Dec 13, 2004
Posts: 429
doPost is a void method, so it has no return type. In the middle of the method you can just do something like:

doPost(...) {
...

if (needToStop()) {
return;

}
....

}

However stopping method from OUTSIDE this thread, which seems like what you are trying to do, is a whole different ball game. I guess you can have the needToStop() function check some variable set by a different thread, but it seems to me that you don't really understand the whole request/response cycle, and how it fits in with the servlet life cycle and multi-threading. I suggest you read up on that.

-Yuriy
Neeraj Dheer
Ranch Hand

Joined: Mar 30, 2005
Posts: 225
From what i understand of the question, I agree with Yuriy's reply. That is the best way to do it.

However, it should be noted that if we do it using return, the servlet will still be called and doPost() executed till the 'return' statement and a response sent back to the client(which may be empty if return is called before doing anything else).

is that acceptable?
igwe kalu kalu ogba
Ranch Hand

Joined: Feb 03, 2005
Posts: 133
Thanks Yuri, return seems to work. but , is it possible to make the page such that when i click on the stop button, a new page is not loaded. I seem to be getting a blank white page. thanks a lot guys.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15958
    
  19

I'm not positive, but this sounds like another attempt to write a servlet as though it was a client-server app.

Servlets have some very definite limitations. First and foremost, they can't initiate actions, you have to give them a request and they provide a response. Want another response? you have to make another request. Give a request? you'll get a response. And so on.

The second major limitation is that browsers expect the response to come back fairly quickly. In olden times, a minute or 3 with no response and the browser would simply time out and quit listening. These days I've not seen that happen, but I still code like it will. At a minimum, you've locked the browser window while it's waiting on that response.

So how to handle situations where the response is dependent on slow resources? The preferred way is to spin off an "engine" thread to do the work and send back a "waiting..." page. Usually you'll use the timer meta tag to cause the "waiting..." page to automatically request an updated status on the engine. When the engine is done, it posts a session variable that tells the request process to quit displaying "waiting..." pages and display the engine's output.

That's it in a nutshell. For all the details, I think Bill can probably sell you a book or two.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to stop a servlet from executing
 
Similar Threads
Multi Thread
how to kill main thread and relative subthread?
MVC, PRG and the double-post problem
explicitly sending http 200ok message from servlet
Socket Exception in servlet ??