how to get custom exception message in ajaxError of jquery - java

I know we can get exception using xhr.responseText, but in my APP, the responseTextdisplay as exception description amended by Tomcat. please refer to below image, is there anyone can tell me how can I control the content of responseText or how to handle custom message(may be in a better way).
I need a title and content in the response as I can display them in dialog of Jquery UI. BTW, I throw a general exception just like throw new Exception("some error happens");

You can define custom error message in your web.xml like this -
<error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/Error.jsp</location>
</error-page>
You can have any text in your error.jsp page, that text will be sent to client on respective error
more info

Related

How can I rewrite page content after an exception has been thrown while rendering a view?

As a simple example, take a page that renders to JSP, and has an web.xml error-page attribute as a catch all exception:
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/WEB-INF/error/exceptions/throwable-layout.jsp</location>
</error-page>
Now, in the view, suppose we attempt a method that doesn't exist:
${requestScope.getBadVar()}
What happens is the output of the view terminates and the error page is then inserted immediately at the point of where the exception was thrown.
I understand this is tricky because the servlet has already started writing out the output stream and now it needs to be started from scratch, is there any way that this is possible?

404 error page page not shown in some conditions

When my path is
hostname/some_file.jsp
ErrorPage404.jsp is displayed
But when my path is any one of following
hostname/some_file.html
hostname/some_file_path
hostname/unknown_path/some_file_path
hostname/unknown_path/some_file.html
ErrorPage404.jsp is not displayed
I have mapped ErrorPage404.jsp in web.xml in following manner
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/jsp/Entry/ErrorPage404.jsp</location>
</error-page>
Thanks in advance.

Java EE - more generic error code mapping

I'm currently mapping error codes in web.xml and so far it looks like this:
<error-page>
<error-code>404</error-code>
<location>/errorHandler?code=404</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/errorHandler?code=500</location>
</error-page>
Do I have to map every single error code manually or is there more automatic way? I'd like to have something like this:
<error-page>
<error-code>*</error-code>
<location>/errorHandler?code=*</location>
</error-page>
That's possible, according to the documentation of oracle. It depends on whether you are using servlet 2.5 (not supported) or 3.0 (supported).
I would recommend you to use plain html for the error codes.
The reason behind this, is these pages will be shown when something is going very wrong. Full list of status codes
Generic solution example: moreinfo with onehippo
From security perspective it is not recommended to give information what went wrong in the application: https://blog.whitehatsec.com/error-handling-in-java-web-xml/

Handling runtime exceptions in UI

Suppose my web application throws a runtime exception due to some reason.
When that happens, I want to capture it and show a proper message in the UI rather than a 501/505 error.
What is the best way to do that ?
something lik ethis in your web.xml
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/app/error/commonErrorPage</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/app/error/404ErrorPage</location>
</error-page>

How to get original page url/uri after request dispatcher forward

I have a Error404Servlet which is configured as error-page for 404 in web.xml:
<servlet>
<servlet-name>Error404</servlet-name>
<servlet-class>com.foo.bar.Error404Servlet</servlet-class>
</servlet>
<error-page>
<error-code>404</error-code>
<location>/error404</location>
</error-page>
In this servlet i have to log the original url that caused 404, but request.getRequestURI() always returns "/error404"
How can i get the original url? The unly ugly method i know is to create filter that puts the original url to request attribute.
From the request you can retrieve attributes set by the container in the event of an error:
request.getAttribute("javax.servlet.error.request_uri");
other attributes that can provide usefull information;
javax.servlet.error.status_code
javax.servlet.error.exception_type
javax.servlet.error.message
javax.servlet.error.exception
See also this servlet 2.3 features article.

Categories