HTTP Status 500 customise through webapp error page - web.xml

I have customized error code 403 (client error) through webapp i.e. adding entry in web.xml as below which works fine.
<error-page>
<error-code>403</error-code>
<location>/access-denied.xhtml</location>
</error-page>
I try to customise error code 500 (server error) with the above approach as below.
<error-page>
<error-code>500</error-code>
<location>/error-500.xhtml</location>
</error-page>
but this doesn’t work (getting jboss's original error message for error code 500 instead of error-500.xhtml) and I have no clue why, so I am left with only option to customize it through apache httpd and vhost config.
Any idea why this is not working?

The reason for not working is because the webapp has implemented ExceptionMapper that catches the exception instead of letting the custom error page to be resolved from web.xml
The resolution would be to remove the ExceptionMapper impl class.

Related

org.omnifaces.EXCEPTION_TYPES_TO_UNWRAP doesn't work for non-ajax requests

I use FullAjaxExceptionHandlerFactory to handle exceptions during both ajax and non-ajax requests.
The OmniFaces showcase page says
The FullAjaxExceptionHandler will transparently handle exceptions during ajax requests exactly the same way as exceptions during synchronous (non-ajax) requests.
I noticed that if an exception happened during a synchronous request, findExceptionRootCause (the method that actually does unwrapping) doesn't get called. Therefore, the rules defined in the web.xml aren't being applied since they rely on FullAjaxExceptionHandler unwrapping.
Does it mean I need to extend the FullAjaxExceptionHandler or is there something I am missing?
Stack trace :
(non-ajax request)
javax.servlet.ServletException:
Caused by: javax.faces.view.facelets.TagAttributeException
Caused by: javax.el.ELException
Caused by: xxx.MyException
web.xml:
<context-param>
<param-name>org.omnifaces.EXCEPTION_TYPES_TO_UNWRAP</param-name>
<param-value>javax.servlet.ServletException,javax.faces.view.facelets.TagAttributeException,javax.el.ELException</param-value>
</context-param>
...
<error-page>
<exception-type>xxx.MyException</exception-type>
<location>/xxx/page-not-found.xhtml</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/xxx/internal-server-error.xhtml</location>
</error-page>
*I remember ELException may be omitted since it will be included anyway. For the sake of certainty, I included every type preceded my exception in the stack trace.
Result:
/xxx/internal-server-error.xhtml is shown
Expected:
/xxx/page-not-found.xhtml is shown
Update:
For the exception mentioned above, if I define <error-page> like
<error-page>
<exception-type>javax.faces.view.facelets.TagAttributeException</exception-type>
<location>/blueglue/templates/error/page-not-found.xhtml</location>
</error-page>
or
<error-page>
<exception-type>javax.servlet.ServletException</exception-type>
<location>/blueglue/templates/error/page-not-found.xhtml</location>
</error-page>
I will get the expected outcome - the 404 page.
For javax.el.ELException and xxx.MyException, the exception results in the 500 page which isn't what I am expecting.
I use FullAjaxExceptionHandler to handle exceptions during both ajax and non-ajax requests.
The FullAjaxExceptionHandler does not handle exceptions during non-ajax requests. Its sole purpose is to handle exceptions during ajax requests the same way as non-ajax requests (namely, showing an error page defined in web.xml).
Further in the showcase page which you linked you can find this section:
Normal requests
Note that the FullAjaxExceptionHandler does not deal with normal (non-ajax) requests at all. To properly handle JSF and EL exceptions on normal requests as well, you need an additional FacesExceptionFilter. This will extract the root cause from a wrapped FacesException and ELException before delegating the ServletException further to the container (the container will namely use the first root cause of ServletException to match an error page by exception in web.xml).
So, all you need to do is to install the FacesExceptionFilter in order to get the same unwrapping behavior as FullAjaxExceptionHandler.

External redirects in exception logic is not working on Tomcat 8.5.39

I'm setting up exception handling logic for a multipart project with common error page (that is hosted in other part of the project). When I tried to redirect to external URL on exception, tomcat 8.5.39 is showing default error instead. Funny thing is, this seems to work just fine in tomcat 8.5.38
I've tried many different exception handling techniques, but they all seem not to work for external redirects.
So currently, i have something like this in my web.xml file:
...
<error-page>
<error-code>404</error-code>
<location>/error/error404</location>
</error-page>
...
and for my Spring controller,
#Controller
#RequestMapping(value = "/error")
public class ErrorHandler{
...
#GetMapping(value = "error404")
public String error404(){
return "redirect:http://{myproject}/{404errorPage}";
}
...
}
I'm expecting this code to redirect the user to http://{myproject}/{404errorPage} when 404 error occurs, which works just fine in tomcat 8.5.38. But on 8.5.39, they seem to have changed error handling logic, and it will display default error page(browser default 404 page).
Any input or idea would be tremendously helpful.
This is a known regression in 8.5.39 which is fixed in the just released 8.5.40.

spring - how to deny the direct access to servlet mapping(POST method used) from the url

For example, I have a servlet mapping /servlet, and actually it is requested by a form using POST Method. If I type in the mapping directly without using the form in the browser, the server will give me a 405 page.
HTTP Status 405 - Request method 'GET' not supported
What I want to do is to catch this exception using spring security. I was thinking to restrict this servelet request by using POST method only so that the direct url access will be denied. However, I searched on the internet but I didn't find an answer. Please guide me how to do this, thanks.
If you want to override default error message you should create your own error page and map it into web.xml like this
<error-page>
<error-code>405</error-code>
<location>/internal-server-error.htm</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/405.html</location>
</error-page>
and your 405.html will contain your personalized error message for the user.
Read more: http://mrbool.com/how-to-create-error-page-in-j2ee/26770

websphere 7 shows full error to customer on web app deployment/starting error

i have deployed my web application in WebSphere 7. this application is build using spring and jsp servlets. some times when it get deployed due to errors it shows the following attached image like errors.
i have handled my web application errors as follows, by redirecting the errors to spring controller.
<error-page>
<error-code>500</error-code>
<!--Internal server error -->
<location>/error.p?message=500</location>
</error-page>
<error-page>
<error-code>403</error-code>
<!--Forbidden -->
<location>/main.p</location>
</error-page>
but it only works when the web application is successfully deployed. when deployment error happens WebSphere shows full error message like above.
is there any way to hide this error page and add custom error page in websphere when such deployment errors happens?
If you have an Apache server (or other) in front of your WebSphere, you may use it to redirect error 500 on a custom static web page.
Since you're using Spring, if this error can occur often, you can try to use lazy bean initialization on the remoteOMSConnectorWS (See LazyInitTargetSource). This would delay bean instantiation until its first use, most likely after webapp complete startup. In this case, your error configuration from web.xml could be used.

Tomcat 404 Page With Spring Form

In my web.xml page I have the 404 error mapped to a jsp page as:
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/jsp/errorPages/error404.jsp</location>
</error-page>
The redirect on 404 works fine. I get to error404.jsp. The problem is that I have a spring form on the page (a search form that is on EVERY page) which causes errors because the target object is not in the model. I get the following exception:
Caused by: java.lang.IllegalStateException: Neither BindingResult nor
plain target object for bean name 'searchCriteria' available as request attribute
I'd like to have this search form on every page, including the error pages. Is there any way to do this? I realize that 404 as I have it configured above doesn't go to a servlet... so how do I get the request attribute into the model?
One solution is to code the search form as plain HTML in your JSP/template, so that you remove any dependency on the form binding object. For a simple search form with a text input and submit button, this should not be a problem.
The way to do this is to map the error pages to a servlet. Within the servlet you can add things to your model just like you can in any other servlet. To map error pages to a servlet, add something like the following to your web.xml file:
<error-page>
<error-code>404</error-code>
<location>/error/generalError</location>
</error-page>
where "error" is the servlet name and "generalError" is the request mapping.

Resources