File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Handling an Ctrl-C in an Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Handling an Ctrl-C in an Servlet" Watch "Handling an Ctrl-C in an Servlet" New topic
Author

Handling an Ctrl-C in an Servlet

Steven Rodeo
Ranch Hand

Joined: Mar 06, 2008
Posts: 72

The following servlet handles a POST request. The following servlet accepts an input file, and writes this to a DISK. The problem is when the user / Client / UI which is talking to this servlet
issues a CTRL-C ( SIGINT ), the servlet seems to ignore it, it won't throw an exception. Don't know how to handle such a scenario. How does one handles this situation


Thanks a Million!

SM
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61764
    
  67

Steven Rodeo wrote:The problem is when the user / Client / UI which is talking to this servlet

This makes little sense. Nothing "talks" to the servlet. An HTTP request is issued to the server and it returns a response. Control-C has no meaning in this environment.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Steven Rodeo
Ranch Hand

Joined: Mar 06, 2008
Posts: 72

The CTRL-C is some how having an impact; the while loops exits, for some reason . But the while loop doesn't throw any sort of exception
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61764
    
  67

What while loop are you talking about, and how is the servlet being accessed? Where is the Ctrl-C being entered? Your post is very short on details.

You might also want to edit your original post and fix the code tags.
Steven Rodeo
Ranch Hand

Joined: Mar 06, 2008
Posts: 72


What while loop are you talking about, and how is the servlet being accessed? Where is the Ctrl-C being entered? Your post is very short on details.


The while loop, I was referring to is in the Servlet Code.

The servlet is deployed on a TomCat.

A stand alone application (Jar file) is trying to access this servlet via an URL some thing like this.( this is run on my local machine )



For instance when the uploaded FileName is large, it might take a very long timeto upload. Consequently the user issues a Ctrl-C
causing the while loop (in the servlet code ) to exit. I don't want this to happen, I rather fail when the Ctrl-C is issued, and throw an
Exception back to the client.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61764
    
  67

Steven Rodeo wrote:A stand alone application (Jar file) is trying to access this servlet via an URL some thing like this.

How is this application accessing the servlet?
Steven Rodeo
Ranch Hand

Joined: Mar 06, 2008
Posts: 72

Some thing like this.

java -jar my.jar -url http://mrhost:8080/SERVLETNAME
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61764
    
  67

That's not what I asked. How is the application accessing the servlet? Not how is someone running the application.

If the app is accessing the servlet using an HTTP connection, the same applies. The servlet is on the other end of the connection and cannot respond to things like a Ctrl-C that it know nothing about. All it knows is that for some reason the client stopped sending data in the middle of the upload, or that the connection for the response is closed, or whatever happens to the connection when the client is aborted. There's is no way to make it react to a Ctrl-C on the other end of the connection.
Steven Rodeo
Ranch Hand

Joined: Mar 06, 2008
Posts: 72
All it knows is that for some reason the client stopped sending data in the middle of the upload, or that the connection for the response is closed, or whatever happens to the connection when the client is aborted


Thank you!!!...... I think you solved my problem, is there a way to figure out from the InputStream, what I am expecting some thing like how big a stream its going to be ?. Coz that way I can throw an Exception once the connection fails

Thanks a bunch !

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

You mean in that servlet code you posted? It's already going to throw an exception if it can't read the whole input stream because the client disconnected. There's no need for you to try to duplicate that.
Steven Rodeo
Ranch Hand

Joined: Mar 06, 2008
Posts: 72

Nope the above code won't throw an exception.
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

1. This is not a Servlet issue but an HTML issue
2. Servlet runs at the Webserver, the Client sees and interacts with a webpage at the client end (which is html)


Please either post the complete servlet or just the generated html file

My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61764
    
  67

salvin francis wrote:1. This is not a Servlet issue but an HTML issue

How on earth are yiou figuring that? Have you read the posts? This has nothing whatsoever to do with HTML.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Steven Rodeo wrote:
Nope the above code won't throw an exception.


This post isn't very helpful.
Is your code actually running inside a servlet?
Is it inside the doPost method?

If so, why are you declaring a ServletRequest on line one?
You should be using the one passed to the doPost method by the container.

Is all of this code wrapped in another try/catch block that deals with your exception or throws an IOException or ServletException?

It would be helpful if you posted all the code (if not for the entire servlet, than at least for the service method that you're overriding).

Tomcat will throw an exception if the client disconnects before the completion of the service method.




Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Steven Rodeo
Ranch Hand

Joined: Mar 06, 2008
Posts: 72

Here is the code snippet called from doPost ( BTW : I am deploying this Servlet on GlassFish )



Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

The code, as you've presented it here, wouldn't even compile.
How do you know that it won't throw an exception?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Handling an Ctrl-C in an Servlet