3

I have a payment form. When user submit the form the payment process runs successfully, but clicking the back button brings user to same form. I want to expire the form after successful submission, to prevent user from multiple payment (in case user goes back and submit form). Following Prevent user from going back tutorial, I added the filter but it's not working for me. What am I doing wrong? Here is what I added for filtering.

<filter>
    <filter-name>paymentFilter</filter-name>
    <filter-class>path to PaymentFilter class</filter-class>
</filter>
<filter-mapping>
    <filter-name>paymentFilter</filter-name>
    <url-pattern>/order/*/payment</url-pattern>
</filter-mapping>

and my filter class is

public class PaymentFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // TODO Auto-generated method stub          
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {

        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
        httpServletResponse.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
        httpServletResponse.setHeader("Pragma", "no-cache"); // HTTP 1.0.
        httpServletResponse.setDateHeader("Expires", 0); // Proxies.
        System.out.println("In filter");            
    }

    @Override
    public void destroy() {
        // TODO Auto-generated method stub          
    }

}

I have added a System.out.println("In filter") but I can't see its output ("In filter") on console after running the page.

When I use the URL pattern as /* the System.out prints on console,

<url-pattern>/*</url-pattern> (it works as expected)

but when I change the URL pattern to /order/*/payment (* is order id what changes for each order). then System.out does not print anything on console.

<url-pattern>/order/*/payment</url-pattern> (it doesn't work)

I am using spring mvc, apache, tomcat7.0

Community
  • 1
  • 1
Shahzeb Khan
  • 3,582
  • 8
  • 45
  • 79

4 Answers4

2

As yourself already found, * can only be a prefix or a suffix of the url-pattern. The reasoning for this is that a lot of ambiguity would arise if it was defined otherwise.

Further, if you submit your form with GET, the user always can go to the resulting screen by hitting the back button. If you use POST, the browser will say that this may not be possible.

2

I have an alternate solution to your problem. You can try javascript or jquery to disable the back or forward button.

Sayan
  • 145
  • 2
  • 15
1

try by adding

chain.doFilter(request, response);  

as your last line in doFilter method.

someone
  • 6,577
  • 7
  • 37
  • 60
1

What gave me the solution to my problem is that i "can not" user regular expression in my url-patter for filter mapping. * can only be use as suffix or prefix.

Shahzeb Khan
  • 3,582
  • 8
  • 45
  • 79