title: Adding interceptors in struts.xml for all Action classes
body: <p>I've used the Struts 2 framework and I have created a web application which has a Login Page. I have three different <code>Action</code> classes named <code>Action1</code>, <code>Action2</code>, <code>Action3</code>, and different views for JSP pages which are rendered by running some business logic in the <code>Action</code> classes.</p>
  <p>Now, I want to check whether a user has logged in before the <code>Action</code> class carries out processing. So,</p>
  <p><strong>I created an interceptor below that works fine:</strong></p>
  <pre><code>public String intercept(ActionInvocation invocation) throws Exception 
  {
      HttpServletRequest  request  = ServletActionContext.getRequest();
      HttpServletResponse response = ServletActionContext.getResponse();
      HttpSession         session  = request.getSession();
      
      if(session.isNew())
      {
          response.sendRedirect(&quot;Login.action&quot;);
      }
      
      System.out.println(&quot;Interceptor Fired&quot;);
      String result = invocation.invoke();
      return result;
  }
  </code></pre>
  <p>What I want to be in <code>struts.xml</code> is instead of adding an interceptor for all the actions like the one below</p>
  <pre><code>&lt;interceptor-ref name=&quot;newStack&quot;/&gt;
  </code></pre>
  <p><strong>My <code>struts.xml</code> file has:</strong></p>
  <pre><code>&lt;package name=&quot;default&quot; extends=&quot;struts-default&quot;&gt;       
     &lt;interceptors&gt;   
      &lt;interceptor name=&quot;printMsgInterceptor&quot; class=&quot;LoginInterceptor&quot;&gt;&lt;/interceptor&gt;
           &lt;interceptor-stack name=&quot;newStack&quot;&gt;
          &lt;interceptor-ref name=&quot;printMsgInterceptor&quot;/&gt;
          &lt;interceptor-ref name=&quot;defaultStack&quot; /&gt;
           &lt;/interceptor-stack&gt;
       &lt;/interceptors&gt;
  
      &lt;action name=&quot;actone&quot; class=&quot;Action1&quot;&gt;
        &lt;result name=&quot;success&quot;&gt;/success.jsp&lt;/result&gt;
        &lt;interceptor-ref name=&quot;newStack&quot;/&gt;
      &lt;/action&gt;
          &lt;action name=&quot;acttwo&quot; class=&quot;Action2&quot;&gt;
        &lt;result name=&quot;success&quot;&gt;/success.jsp&lt;/result&gt;
        &lt;interceptor-ref name=&quot;newStack&quot;/&gt;
      &lt;/action&gt;
           &lt;action name=&quot;actthree&quot; class=&quot;Action3&quot;&gt;
        &lt;result name=&quot;success&quot;&gt;/success.jsp&lt;/result&gt;
        &lt;interceptor-ref name=&quot;newStack&quot;/&gt;
      &lt;/action&gt;
      &lt;/package&gt;
  </code></pre>
  <p>For every action I want to have some definition written in <code>struts.xml</code> which runs the interceptor rather than manually adding</p>
  <pre><code>&lt;interceptor-ref name=&quot;newStack&quot;/&gt; 
  </code></pre>