Catching exceptions with tomcat and a servlet
I have set-up tomcat to catch all my exceptions and pass them to a servlet with the following in web.xml.
<servlet-mapping>
<servlet-name>exception</servlet-name>
<url-pattern>/exception</url-pattern>
</servlet-mapping>
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/exception</location>
</error-page>
This works fine and I have the servlet logging some information and forwarding to a jsp. However I want to log the URI which caused the exception to be thrown and when I call request.getRequestURI() I get /exception which is my servlet path that's handling logging the exception. How can I get the original URI that caused the exception?
Asked by: Ryan141 | Posted: 23-01-2022
Answer 1
You can get the original uri with
request.getAttribute("javax.servlet.forward.request_uri")
Answered by: Max239 | Posted: 24-02-2022
Similar questions
Catching exceptions in Java
There are certain predefined exceptions in Java, which, if thrown, report that something serious has happened and you'd better improve your code, than catching them in a catch block (if I have understood it correctly). But still I find many programs in which I have the following:
} catch (IOException e) {
...
} catch (FileNotFoundException e) {
....
}
and I thought that IOExcepti...
Java: catching specific Exceptions
say I have the following
try{
//something
}catch(Exception generic){
//catch all
}catch(SpecificException se){
//catch specific exception only
}
What would happen when it comes across SpecificException ? Does it catch it as a generic exception first, and then catch the specificexception ?
Or does it only catch SpecificException while ignoring generic exceptions.
I don't wan...
java - JNA Catching Exceptions
I have a quick question about dealing with exceptions being thrown by libraries under JNA...
When I throw an exception in the underlying native code, JNA gets a invalid memory access error. I'm assuming this is because C libraries cannot throw an exception up through it's stack (it's actually C++/CLR but has C exports)? So is there no real way to report the exception to Java? Or "should it work" and I'm just doing ...
java - Catching Exceptions From Multiple Methods Withing the Class
In Java, I have a class with a few methods that throw the same custom exception (the custom exception extends the 'Exception' class):
private void setAColor(float r, float g, float b, float a) throws AnException {}
private void setBColor(float r, float g, float b, float a) throws AnException {}
private void setCColor(float r, float g, float b, float a) throws AnException {}
Instead of havi...
java - Catching Javamail Exceptions
I am planning on using javamail to sent automated emails in a loop type fashion. So for example I might have 300 emails that I need to build and send out with javamail one after the other. I am using timers in java to automate the process at a specific time each day.
What exceptions should I attempt to catch and how do I handle the error when I do catch an exception without interrupting the loop? What exceptions sh...
java - JavaFX 2 - Catching all runtime exceptions
I tried
Thread.setDefaultUncaughtExceptionHandler...
in the main, and also in the start(Stage primaryStage) method. It ain't working.
I also tried
public static void main(String[] args) {
try {
launch(args);
}catch(Throwable t) {
System.out.println(t.getMessage);
}
}
Exception stack trace.
at jav...
java - Catching exceptions with Xalan xslt
I have the folowwing XSLT based on Xalan:
TransformerFactory factory = TransformerFactory.newInstance();
XalanErrorListener listener = new XalanErrorListener();
factory.setErrorListener(listener);
// Create transformer
StreamSource config = new StreamSource(xslPath);
Transformer transformer = factory.newTransformer(config);
// Create input / ouput
StreamSource source = new Stream...
java catching exceptions
try {
if (x.length == Styles.size()) {
}
else{
throws InputMismatchException ;
}
} finally {
OutputFileScanner.close();
}
I get compile error in method contains code above , is there any way to throw InputMismatchException in else block ?
sftp - zeon java library is not catching exceptions
Hi I´m implementing a sftp client using Zeon library and it works fine. The problem starts when I test it for failures here is my code:
int status = 0;
try {
SFTPClient sftpClient = new SFTPClient(host, username, password);
status = sftpClient.sendFolder(sendingFolder, destFolder, new BatchTransferProgressDefault());
System.out.println("FileTransferStatus.SUCCESS: " + FileTransferStatus.SUCCES...
java - Catching exceptions from a Jersey REST client thrown from a Spring MVC Rest service
I implemented a REST application using Spring MVC, Jersey and JAXB.
The client sends a ServiceRequest object which contains information about the request and receives a ServiceResponse object back with information about the response.
ServiceRequest
@XmlRootElement(name = "servicerequest")
public class ServiceRequest{
String serviceName = "AddUser"
public...
java - How Can I Avoid Using Exceptions for Flow Control?
I have been assigned a project to develop a set of classes that act as an interface to a storage system. A requirement is that the class support a get method with the following signature:
public CustomObject get(String key, Date ifModifiedSince)
Basically the method is supposed to return the CustomObject associated with the key if and only if the object has been ...
java - Catching several exceptions and rethrowing a general exception
I'm using reflection to add some data to a private variable inside a class from a third-party library. Along the way there are about four different Exceptions that can be thrown; all of them are related to reflection, and all of them are very unlikely to occur. I'm hardcoding the name of the class and variable involved. I'm unlikely to receive any class not found or field not found errors, unless the library gets upgrad...
java - Throwing exceptions to control flow - code smell?
Closed. This question is opinion-based. It is not c...
Why don't you have to explicitly declare that you might throw some built in exceptions in Java?
I've noticed with Integer.parseInt() that you don't have to surround it with a try catch or declare that the method might throw an exception, despite the fact that it "throws" a NumberFormatException.
Why don't I have to explicitly catch the NumberFormatException or state that my method throws it?
Throwing exceptions in Java
I have a question about throwing exceptions in Java, a kind of misunderstanding from my side, as it seems, which I would like to clarify for myself.
I have been reading that the two basic ways of handling exception code are:
1.) throwing an exception in a try-block with "throw new ...", and catching it immediately in a catch-block - the so called try-throw-catch mechanism.
2.) throwing an exception ...
Catching exceptions in Java
There are certain predefined exceptions in Java, which, if thrown, report that something serious has happened and you'd better improve your code, than catching them in a catch block (if I have understood it correctly). But still I find many programs in which I have the following:
} catch (IOException e) {
...
} catch (FileNotFoundException e) {
....
}
and I thought that IOExcepti...
java - How expensive are Exceptions
This question already has answers here:
Catching exceptions as an expression while debugging Java in Eclipse IDE
An everyday debugging situation for Java developers is that in which an Exception is thrown and then you need to dig into the debugger to find out what threw it. Usually you would try to set up some breakpoints before the exception is thrown and hope that you are able to determine the situation that leads up to that exception.
In Eclipse, a breakpoint may have an expression defined where it is only triggered when, ...
Why catch Exceptions in Java, when you can catch Throwables?
We recently had a problem with a Java server application where the application was throwing Errors which were not caught because Error is a separate subclass of Throwable and we were only catching Exceptions.
We solved the immediate problem by catching Throwables rather than Exceptions, but this got me thinking as to why you would ever want to catch Exceptions, rather than Throwables, because you would then miss th...
java - Simple code to handle JNI exceptions
I'd like to have a nice, tidy way of expressing the following java code as JNI:
try {
SomeMethod ();
}
catch (ExceptionType1 e) {
SomeAction ();
}
catch (ExceptionType2 e) {
SomeAction ();
}
catch (ExceptionType3 e) {
SomeAction ();
}
Is there a tidy JNI patter for doing this? At present, I have this:
java_class = (*env)->FindClass (env, EXCEPTION_CLASS_...
Still can't find your answer? Check out these amazing Java communities for help...
Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)