• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to stop a servlet from executing

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
igwe kalu kalu ogba
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how about the servlet destroy() method. will that equally work??
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, servletDestroy is called by the container.
It doesn't actually destroy anything.
 
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how about a simple return in the doPost() whereever you want execution to stop?

-Yuriy
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 429
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic