1

I have to use some listener class to listen all pages. According to the url, I must split it and get some informations.

For example :

x.domain.com/$member

I want to get the member name through url, get the following bean and retrieve some database informations.

So, I created a PhaseListener to do the job, but it is not called every times I needed it, for example in case of 404 error.

How can I proceed ?

Thanks a lot :)

PS : I am using Glassfish 4.1.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Alexsoyes
  • 444
  • 4
  • 12
  • As far as I know, No listener, or any other JavaEE application component will ever be called in the event of a 404. The app server doesn't let an umatched request progress any further once a URL cannot be matched. You are left with standard web app error pages to handle your 404 – kolossus Feb 01 '15 at 03:56
  • Thanks for response ! So I have to called my listener for the custom 404 error page and render some compositions. How could I implement that ? My listener being called on the page load ? – Alexsoyes Feb 01 '15 at 09:58

1 Answers1

2

Use a servlet filter which is mapped on both normal and error page requests:

<filter-mapping>
    <filter-name>yourFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>ERROR</dispatcher>
</filter-mapping>

Inside the filter, you can then check by the presence of a request attribute keyed with RequestDispatcher#ERROR_REQUEST_URI if it was being triggered on an error page or not.

String errorRequestURI = (String) request.getAttribute(RequestDispatcher.ERROR_REQUEST_URI);

if (errorRequestURI != null) {
    // Error page was triggered on the given URI.
}
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you BalusC, you're a legend ! I can now handle all pages from my app ! But tell me, how could I get bean from doFilter() method ? I have to access methods in some controllers, and I was able to do it with PhaseListener. I look for answers on the web, but I do not understand everything. Thanks again. – Alexsoyes Feb 01 '15 at 14:40
  • `@Inject` would be easiest if it's a `@Named` http://stackoverflow.com/q/2633112/ – BalusC Feb 01 '15 at 15:27
  • I have figure it out how to use the Inject method, thank you again. I assume that it is not safe to inject some EntityManager in order to get access to the database. I will have to get the FacesContext from cookie like you mentioned here : http://stackoverflow.com/questions/14045242/how-do-i-retrieve-the-facescontext-within-a-filter – Alexsoyes Feb 02 '15 at 10:18
  • "Get FacesContext from cookie"? Not sure I understand .. Do you? As to injecting the entity manager, only do it in service classes (EJBs), not in managed beans. – BalusC Feb 02 '15 at 11:48
  • I meant the second part of your answer from the link above. It is difficult to me to understand your responses due certainly to a lack of knowledge of the topic. I will look for on the net in order to find some inject with EM. Thank you for your time :) – Alexsoyes Feb 03 '15 at 10:01