title: Skip Java Filter based on Condition
body: <p>I have looked in to <a href="https://stackoverflow.com/questions/3738162/how-to-skip-a-filter-in-the-filter-chain-in-java">How to skip a filter in the filter chain in java</a> and followed the solution but this didn't help.</p>
  
  <p>I have three filters in my web.xml. Struts filter, custom filter and IAM Agent filter in this order. I want to skip IAM filter based on some condition. I do not have control on IAM filter its external peace of code. So when ever the front end parameter says that the usertype is internal I need to call IAM agent filter else I need to skip this filter.</p>
  
  <p>Web.xml </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns="http://java.sun.com/xml/ns/javaee" 
  xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
  id="WebApp_ID" 
  version="2.5"&gt;
  &lt;display-name&gt;App&lt;/display-name&gt;
  &lt;!-- Struts2 Filter --&gt;
  &lt;filter&gt;
      &lt;filter-name&gt;struts2&lt;/filter-name&gt;
      &lt;filter-class&gt;org.apache.struts2.dispatcher.FilterDispatcher&lt;/filter-class&gt;
  &lt;/filter&gt;
      &lt;filter-mapping&gt;
      &lt;filter-name&gt;struts2&lt;/filter-name&gt;
      &lt;url-pattern&gt;*.action&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
   &lt;!-- Custom App Filter for Handling Internal And External User--&gt;
  &lt;filter&gt;
     &lt;filter-name&gt;UserType&lt;/filter-name&gt;
     &lt;filter-class&gt;com.App.web.filter.LoginFilter&lt;/filter-class&gt;
  &lt;/filter&gt;
  &lt;filter-mapping&gt;
     &lt;filter-name&gt;UserType&lt;/filter-name&gt;
     &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  
  &lt;!-- IAM Filter for Internal Users only--&gt;
  &lt;filter&gt;
     &lt;filter-name&gt;Agent&lt;/filter-name&gt;
     &lt;filter-class&gt;com.sun.identity.agents.filter.AmAgentFilter&lt;/filter-class&gt;
  &lt;/filter&gt;
  &lt;filter-mapping&gt;
     &lt;filter-name&gt;Agent&lt;/filter-name&gt;
     &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;   
  
  &lt;welcome-file-list&gt;
      &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
  &lt;/welcome-file-list&gt;
  &lt;listener&gt;
      &lt;listener-class&gt;org.apache.tiles.web.startup.TilesListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  &lt;listener&gt; 
      &lt;listener-class&gt;com.App.init.InitListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  &lt;context-param&gt;
      &lt;param-name&gt;org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG&lt;/param-name&gt;
      &lt;param-value&gt;/WEB-INF/tiles.xml&lt;/param-value&gt;
  &lt;/context-param&gt;
  
  &lt;!-- Web Service --&gt;
  &lt;servlet&gt;
      &lt;servlet-name&gt;EventManagementWS&lt;/servlet-name&gt;
      &lt;servlet-class&gt;com.App.eventmanagement.ws.EventManagementWS&lt;/servlet-class&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;EventManagementWS&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/EventManagementWS&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  &lt;resource-ref&gt;
         &lt;res-ref-name&gt;url/ConfigProperty&lt;/res-ref-name&gt;
         &lt;res-type&gt;java.net.URL&lt;/res-type&gt;
         &lt;res-auth&gt;Container&lt;/res-auth&gt;
         &lt;res-sharing-scope&gt;Shareable&lt;/res-sharing-scope&gt;
  &lt;/resource-ref&gt;
  </code></pre>
  
  <p></p>
  
  <p>I am using RequestDispatcher#forward(), but I not getting anything in my index.jsp page output, just a blank page. The css files are giving 404 error. When I was using Struts tag it gave error </p>
  
  <pre><code>Uncaught exception created in one of the service methods of the servlet /index.jsp in application App. Exception created: The Struts dispatcher cannot be found.  This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the request has passed through its servlet filter, which initializes the Struts dispatcher needed for this tag.
  </code></pre>
  
  <p>So i removed S tag and used hard coding, but now I am getting 404 error in all css which i was trying to include.</p>
  
  <p>I feel as if the forward is not working fine.</p>
  
  <p>Java Filter Code:</p>
  
  <pre><code>package com.App.web.filter;
  
  import java.io.IOException;
  import javax.servlet.Filter;
  import javax.servlet.FilterChain;
  import javax.servlet.FilterConfig;
  import javax.servlet.ServletException;
  import javax.servlet.ServletRequest;
  import javax.servlet.ServletResponse;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpSession;
  
  import org.apache.commons.lang.StringUtils;
  
  import com.App.constant.Constant;
  import com.App.init.WIMSInitListener;
  import com.App.log.WIMSMessageLogger;
  
  public class LoginFilter implements Filter{
  
  FilterConfig _filterConfig = null;
  @Override
  public void destroy(){
      // TODO Auto-generated method stub
  
  }
  
  @Override
  public void doFilter(ServletRequest request, ServletResponse response, 
         FilterChain filterChain) throws IOException, ServletException{
      // Check IAM is enabled
      if (InitListener.isIAMEnabled()){
          HttpSession session = httpRequest.getSession();
          String userType = (String) session.getAttribute(Constant.USER_TYPE);
          if(userType == null || "".equals(userType)){
              MessageLogger.putMessageLog("User type is null : First Request" + userType, getClass());
              userType = request.getParameter(Constant.USER_TYPE);
              session.setAttribute(Constant.USER_TYPE, userType);
              request.getRequestDispatcher(((HttpServletRequest)request).getServletPath() + 
                      StringUtils.defaultString(((HttpServletRequest)request).getPathInfo()))
                          .forward(request, response);
  
          }
          if("external".equals(userType)){
              MessageLogger.putMessageLog("External User" + userType, getClass());
              request.getRequestDispatcher(((HttpServletRequest)request).getServletPath() + 
                      StringUtils.defaultString(((HttpServletRequest)request).getPathInfo()))
                          .forward(request, response);
  
          }
          else if("internal".equals(userType)){
              MessageLogger.putMessageLog("Internal User" + userType, getClass());
              //IAM Filter com.sun.identity.agents.filter.AmAgentFilter
              filterChain.doFilter(request, response);
          }
     }
     //Its always External User
     else{
         MessageLogger.putMessageLog("User IAM Not Enabled", getClass());
         request.getRequestDispatcher(((HttpServletRequest)request).getServletPath() + 
                 StringUtils.defaultString(((HttpServletRequest)request).getPathInfo()) )
                        .forward(request, response);
  
     }
  }
  
  @Override
  public void init(FilterConfig filterConfig) throws ServletException{
      this._filterConfig = filterConfig;
  } 
  }
  </code></pre>
  
  <p>Index.jsp</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  pageEncoding="ISO-8859-1"%&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01     Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;App&lt;/title&gt;
  &lt;link rel="stylesheet" type="text/css" media="screen"
  href="/App/css/common.css" /&gt;
  &lt;link rel="stylesheet" type="text/css" media="screen"
  href="/App/css/rcorp.css" /&gt;
  &lt;link rel="stylesheet" type="text/css" media="all"
  href="/App/css/style.css" /&gt;
  &lt;link rel="stylesheet" type="text/css" media="all"
  href="/App/css/rcorp.css" /&gt;
  &lt;link rel="stylesheet" type="text/css" media="all"
  href="/App/css/default.css" /&gt;
  &lt;script type="text/javascript"&gt;
  function selectUserType(){
  var frm = document.forms["UserTypeForm"];
  if(document.getElementById("userTypeInternal").checked){
      //frm.action='&lt;s:url action="loginAction.action?usertype=internal" namespace="" /&gt;';
      frm.action='/App/loginAction.action?usertype=internal';
  }
  else{
      //frm.action='&lt;s:url action="loginAction.action?usertype=external" namespace="" /&gt;';
      frm.action='/App/loginAction.action?usertype=external';
  }   
  frm.target='_parent';
  frm.submit();
  }
  function checkUserType(){   
  //var userType = '&lt;s:property value="#session.usertype" /&gt;';
  var userType = '&lt;%= session.getAttribute("usertype") %&gt;';
  if(userType == 'internal'){
      //frm.action='&lt;s:url action="loginAction.action?usertype=internal" namespace="" /&gt;';
      frm.action='/App/loginAction.action?usertype=internal';
      frm.target='_parent';
      frm.submit();
  }
  else if(userType == 'external'){
      //frm.action='&lt;s:url action="loginAction.action?usertype=external" namespace="" /&gt;';
      frm.action='/App/loginAction.action?usertype=external';
      frm.target='_parent';
      frm.submit();
  }
  }
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body onload="javascript:checkUserType();"&gt;
  &lt;form method="POST" action="loginAction.action" name="UserTypeForm"&gt;
      &lt;div id="display_option"
          style="DISPLAY: none; position: absolute; top: 40%; width: 100%;"
          align="center"&gt;
          &lt;div class="rcorp-tbl"&gt;
              &lt;div style="width: 340px;"&gt;
                  &lt;div style="border: 1px solid #6da6fe; padding: 5px;"&gt;
                      &lt;table style="background-color: #f0f8ff;" cellspacing=2 border=0&gt;
                          &lt;tr&gt;
                              &lt;td class="t-cellsecnowrap"&gt;Select User type:&lt;/td&gt;
                              &lt;td&gt;
                              &lt;td class="t-cell"&gt;
                                  &lt;div id="wwgrp_systemType" class="wwgrp"&gt;
                                      &lt;div id="wwctrl_systemType" class="wwctrl"&gt;
                                          &lt;input type="radio" name="UserType" id="internal" value="internal" checked/&gt;
                                              &lt;label for="userTypeInternal"&gt;Internal User (Sydney Trains Users)&lt;/label&gt;&amp;nbsp; 
                                          &lt;input type="radio" name="userType" id="external" value="external"/&gt;
                                              &lt;label for="userTypeExternal"&gt;External User&lt;/label&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
                                          &lt;input type="button" name="applyBtn" id="applyBtn" class="btn" value="Go" title="Go" onclick="javascript:selectUserType();"/&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
                              &lt;/td&gt;
                          &lt;/tr&gt;
                      &lt;/table&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>