• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Handling an Ctrl-C in an Servlet

 
Steven Rodeo
Ranch Hand
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Pie
Posts: 64196
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Steven Rodeo
Ranch Hand
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Pie
Posts: 64196
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Pie
Posts: 64196
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Some thing like this.

java -jar my.jar -url http://mrhost:8080/SERVLETNAME
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64196
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20203
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nope the above code won't throw an exception.
 
salvin francis
Bartender
Pie
Posts: 1230
10
Eclipse IDE Google Web Toolkit Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64196
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.



 
Steven Rodeo
Ranch Hand
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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



 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic