Book Image

Spring MVC Beginner's Guide

By : Amuthan Ganeshan
Book Image

Spring MVC Beginner's Guide

By: Amuthan Ganeshan

Overview of this book

Table of Contents (19 chapters)
Spring MVC Beginner's Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – intercepting offer page requests


For example, consider a situation where you want to show the special offer products page to only those users who have a valid promo code. The others trying to access the special offer products page with an invalid promo code should be redirected to an error page. Achieve this piece of functionality with the help of the interceptor by performing the following steps:

  1. Create a class named PromoCodeInterceptor under the com.packt.webstore.interceptor package in the source folder src/main/java and add the following code into it:

    package com.packt.webstore.interceptor;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
    
    public class PromoCodeInterceptor extendsHandlerInterceptorAdapter {
    
      private String promoCode;
      private String errorRedirect;
      private String offerRedirect;
    
    
      public boolean preHandle(HttpServletRequest request...