title: How to use @ExceptionHandler with Spring Interceptor for two JSP pages?
body: <p>I am working on Spring MVC controller project. I am trying to add security on two jsp pages. Meaning I will intercept the IP Address coming from the request and see whether that IP Address is a valid IP Address or not. If it is not Valid IP Address, then show an error jsp page.</p>
  
  <p>As I am adding this security on two JSP page. So I would like to show different error messages for above two jsp call for invalid IP Address call.</p>
  
  <p>As soon as I hit below url on the browser, it will show my <code>testOperation</code> and <code>testProcess</code> JSP page if it's a valid ip address call -</p>
  
  <pre><code>http://localhost:8080/testweb/testOperation
  http://localhost:8080/testweb/testProcess
  </code></pre>
  
  <p>So I implemented the Interceptor for this and then configured Spring MVC Interceptors in the XML as well. Now I am checking for valid ip address case with the below code.</p>
  
  <pre><code>@Component
  public class IpCheckingInterceptor extends HandlerInterceptorAdapter {
      public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  
          String requestURI = request.getRequestURI();
          System.out.println(requestURI);
  
          String ipAddress = request.getHeader("X-FORWARDED-FOR");  
          if (ipAddress == null) {  
              ipAddress = request.getRemoteAddr();  
          }
          if (!isValid(ipAddress) &amp;&amp; requestURI.equalsIgnoreCase("/testweb/testOperation")) {
              // need to show a proper error message on the jsp
  
              return false;
          } else if (!isValid(ipAddress) &amp;&amp; requestURI.equalsIgnoreCase("/testweb/testProcess")) {
              // need to show a below JSON response as an error on the browser
              // {"response":"You are not authorized to make a call.","error":"AUTHORIZATION_ERROR","status":"ERROR"}
  
              return false;
          }
  
          return true;
      }
  }
  </code></pre>
  
  <p>In my context.xml file -</p>
  
  <pre><code>&lt;mvc:interceptors&gt;
      &lt;bean class="com.testing.interceptor.IpCheckingInterceptor" /&gt;
  &lt;/mvc:interceptors&gt;
  </code></pre>
  
  <p>Is this possible to do anyhow? Now as I mentioned above, I need to show different error jsp messages for invalid ip address call depending on which page was called. What is the best way to do this?</p>
  
  <p>I don't want to redirect to other JSP page if possible at all. I want to be on the same page and then show the error message appropriately if possible at all?</p>