title: Struts2 action which is called in javascript cannot get session object
body: <p>I have a struts2 action which is called from a javascript(the javascript uses uploadify to upload multiple files):</p>
  
  <blockquote>
  <pre><code> &lt;script type="text/javascript"&gt;
      $(document).ready(function() {
          $("#fileupload").uploadify({
              'uploader'       : '&lt;%=path%&gt;/js/uploadify-v2.1.4/uploadify.swf',
              'script'         : 'observationData/Upload.action',
              ......
          });
      }); &lt;/script&gt;
  </code></pre>
  </blockquote>
  
  <p>The Upload.action will execute a method which tries to get the session object: </p>
  
  <blockquote>
  <pre><code>public class ObservationDataAction extends ActionSupport{
         ......
         public String uploadFile() throws Exception {
          System.out.println("in function uploadFile()");
          ......
          Map session = ActionContext.getContext().getSession();
          System.out.println("Session userID is:"+((SessionUser)session.get("authority.userinfo")).getUserID());
          return null;
      }
  }
  </code></pre>
  </blockquote>
  
  <p>But when the javascript calls the Upload.action, it just prints "in function uploadFile()" but cannot get the session userID. </p>
  
  <p>I tried to call Upload.action explicitly by tying the url in the address bar:
  "<code>http://localhost:8080/observationData/Upload.action</code>", this time I can get the session userID. </p>
  
  <p>I am using struts2.1.6. </p>
  
  <p>This is my struts.xml:</p>
  
  <blockquote>
  <pre><code>   &lt;struts&gt;
      &lt;constant name="struts.enable.DynamicMethodInvocation" value="false"/&gt;
      &lt;constant name="struts.action.extension" value="action"/&gt;
      &lt;package name="observationData" namespace="/observationData" extends="struts-default"&gt;
        &lt;action name="Upload" class="observationDataAction" method="uploadFile"&gt;
        &lt;/action&gt;
      &lt;/package&gt; &lt;struts&gt;
  </code></pre>
  </blockquote>
  
  <p>And I use org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter as the filter class. </p>
  
  <p>I also tried to implement the SessionAware interface, it doesn't work neither. </p>
  
  <p>Can anyone help point out the problem why in function uploadFile() the session does not work?</p>
  
  <p>More info: 
  Here is the function I called to place the user in session:</p>
  
  <pre><code>public static final String SESSION_USERINFO = "authority.userinfo";
  private static void saveSessionUser(SessionUser sessionUser) {
          Map session = ActionContext.getContext().getSession();
          session.put(SESSION_USERINFO, sessionUser);
      }
  </code></pre>
  
  <p><em>Hi I am thinking of a possibility:</em>
  <strong>Is it possible that Ajax/JS create a separate session when they call Upload.action? As a result we cannot get the user in the new session because we set it in the old session?</strong></p>
  
  <p>Thanks
  Xuyuan</p>