My servletfilter for logging page views is receiving a GET and a POST with a single page request. I tracked it back to using Omnifaces ViewScope on page backing bean.
@Named
@org.omnifaces.cdi.ViewScoped
I happen to notice double page views in my log file with exact timestamps. Debugging with the below simplified version, on a single page request, the doFilter is executed twice, first time is a GET and the URL is the URL I am browsing to. Then doFilter is executed again and it is a POST and the URL is the page I came from. If I page refresh I'll see a GET then a POST to the same page. If I use javax.faces.view.ViewScoped
, only GET requests come in.
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
System.out.println(new java.util.Date() + " " + ((HttpServletRequest) request).getMethod() + " " + httpRequest.getServletPath());
chain.doFilter(request, response);
}
Example, if I am viewing http://localhost:8080/myApp/page1.xhtml and I change url to page2.xhtml
the filter will write out
Wed Aug 26 12:17:04 EDT 2020 GET /page2.xhtml
Wed Aug 26 12:17:04 EDT 2020 POST /page1.xhtml
Perhaps by design?. But I only want to log the page that the user is browsing to, not the one they came from. Is it as simple as?:
if(((HttpServletRequest) request).getMethod().equalsIgnoreCase("GET")){
//Write to actual log file
}
or am I using omnifaces viewscope wrong?