aspose file tools*
The moose likes Servlets and the fly likes Java Exceptions - Halt Servlet Execution Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Java Exceptions - Halt Servlet Execution" Watch "Java Exceptions - Halt Servlet Execution" New topic
Author

Java Exceptions - Halt Servlet Execution

Charles OBrien
Greenhorn

Joined: May 23, 2002
Posts: 10
Okay, I've written a servlet, and in this servlet I throw a custom exception, "InvalidSiteException", however when throwing this, all it does it return my nice error message to the top of the page, then continue execution of the servlet.
How would I go about halting the execution of the servlet after the execption is thrown, cought, and error is displayed.
What it does:
"The siteid (s=2) does not exist on our servers.
The page HTML continues to load and display after the exception being thrown and cought."
What I want:
"The siteid (s=2) does not exist on our servers."
----
Basically, for the servlet execution, output, etc... to stop after the error message. I want it to be fatal versus like a generalized message.
Thanks in advance.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12678
    
    5
How about the sendError method in HttpServletResponse - you can send one of the standard error numbers plus a message. Then do an immediate return from your doGet or whatever method.
Bill
Charles OBrien
Greenhorn

Joined: May 23, 2002
Posts: 10
That is working just fine.
If it is not possible to do it the other way, where I can output my own HTML etc for a pretty error, this will do.
Much appreciated.
Charles OBrien
Greenhorn

Joined: May 23, 2002
Posts: 10
An example would be
http://community.infopop.net/2/OpenTopic?a=cfrm&s=729094322&f=fakeforum
[Edit]
Another good example:
http://keytopic.com/KeyTopic/
[/Edit]
I want it to print the nice looking error, but not continue loading the rest of the page.
----
Here is some example PHP code...

Hrm, this seems to work...
Set the method return type to String, and then in the catch return null; after the error is sent.
[ May 15, 2003: Message edited by: Charles OBrien ]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12678
    
    5
All thats necessary to stop the remainder of the page being written is to close the output stream and return from your doGet or whatever.
You should be able to:
1. response.resetBuffer() to discard previous output. You may have to increase the buffer size to ensure that none has been sent already.
2. write your custom error msg
3. close the output stream
4. return
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Exceptions - Halt Servlet Execution
 
Similar Threads
Messed up response to calls to same servlet with different parameters
is CloneNotSupportedException Checked Exception.
passing exception in MVC model from servlet to JSP
response.sendRedirect() not redirecting
Problem with forwarding page towards error page