Handling runtime exceptions in UI - java

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>

Related

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/

how to get custom exception message in ajaxError of jquery

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

Hide JSPs from FacesContext

I have a JSF application with a Servlet Filter configured for a urlPattern of /faces/*.
I want to hide a JSP from faces context so that it won't go through the Servlet Filter.
So I kept it under WebContent folder of my project as WebContent/Error.jsp and declared like the following in the web.xml:
<error-page>
<exception-type>java.lang.NullPointerException</exception-type>
<location>/Error.jsp</location>
</error-page>
But my Error.jsp never gets picked up. Instead I see 404 Page not found error.
To be more clear, I want my Error.jsp page URL to be:
http://localhost:8080/myappname/Error.jsp
But it is only reachable by:
http://localhost:8080/myappname/faces/Error.jsp
The same is the case when I declare any view-id in the faces-config.xml. Where do I keep the error JSP if I want to hide it from faces context?
Based on the information given so far, it looks as it should just work fine. You don't have a <dispatcher>ERROR</dispatcher> on the filter, so the filter should not be invoked at all whenever the NPE is thrown.
Apparently the NPE got wrapped up in another exception because it's been thrown at an illogical place such as bean's constructor instead of a normal bean action method. In such case, JSF would rethrow it as a ManagedBeanCreationException. The container would get it instead of the NPE and thus won't be able to locate the error page. In the container's default HTTP 500 error page, you should read the topmost exception of the stacktrace in order to determine the right exception to define an error page for.
Please keep in mind that runtime exceptions like NPEs are developer errors (bugs!), not production errors and that they should be fixed ASAP. I'd personally just use a global HTTP 500 error page for this kind of bugs:
<error-page>
<status-code>500<status-code>
<location>/errors/generic.jsp</location>
</error-page>
For more specific, real production exceptions, you can always declare a more specific error page:
<error-page>
<exception-type>com.example.YourDatabaseAccessException</exception-type>
<location>/errors/database.jsp</location>
</error-page>
<error-page>
<exception-type>javax.faces.application.ViewExpiredException</exception-type>
<location>/errors/sessionexpired.jsp</location>
</error-page>
If you define a filter and declear that in your web.xml, all the request will go through that filter, unless you define the filter mapping.
I think you can define a filter mapping in your web.xml as following:
<filter>
<filter-name>URLFilter</filter-name>
<filter-class>the filter class in your source code</filter-class>
</filter>
<filter-mapping>
<filter-name>URLFilter</filter-name>
<url-pattern>/some pages</url-pattern>//skip error.jsp here
</filter-mapping>
this is not tested, but just an inspiration.
edit: you can find out more from Oracle site
To me it doesn't sound like the Faces filter has anything to do with this issue. Can you reach http://localhost:8080/myappname/Error.jsp with the filter completely disabled? If not, then perhaps there is some issue with the Error.jsp file itself?
It's hard to say without seeing the code itself.

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