There appears to be an issue where the session object cannot be retrieved by the Struts2 action

I have a struts2 action that is invoked by a JavaScript function. The JavaScript function uses uploadify to enable multiple file uploads:

 <script type="text/javascript">
    $(document).ready(function() {
        $("#fileupload").uploadify({
            'uploader'       : '<%=path%>/js/uploadify-v2.1.4/uploadify.swf',
            'script'         : 'observationData/Upload.action',
            ......
        });
    }); </script>

In the Upload.action method, there's an attempt to retrieve the session object:

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;
    }
}

However, when the JavaScript calls the Upload.action, it only prints "in function uploadFile()" but fails to obtain the session userID.

I also tried manually invoking the Upload.action using this URL: "

http://localhost:8080/observationData/Upload.action
". This time, I was able to successfully access the session userID.

I am currently using struts2.1.6.

This is my struts.xml configuration:

<struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="false"/>
    <constant name="struts.action.extension" value="action"/>
    <package name="observationData" namespace="/observationData" extends="struts-default">
      <action name="Upload" class="observationDataAction" method="uploadFile">
      </action>
    </package> 
</struts>

Additionally, I am using org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter as the filter class.

I also attempted to implement the SessionAware interface, but it did not resolve the issue.

If anyone could help identify why the session is not functioning within the uploadFile() method, I would greatly appreciate it.

Extra information: Here is the function used to store the user in the session:

public static final String SESSION_USERINFO = "authority.userinfo";
private static void saveSessionUser(SessionUser sessionUser) {
        Map session = ActionContext.getContext().getSession();
        session.put(SESSION_USERINFO, sessionUser);
    }

Hi! I'm considering a possibility: Could it be that Ajax/JS creates a separate session when calling Upload.action? As a result, we cannot retrieve the user from the new session because we set it in the old session?

Thank you, Xuyuan

Answer №1

Here's a suggestion: include the jsessionid parameter in your script URL by following these steps.

<script type="text/javascript">
    $(document).ready(function() {
        $("#fileupload").uploadify({
            'uploader'       : '<%=path%>/js/uploadify-v2.1.4/uploadify.swf',
            'script'         : 'observationData/Upload.action?jsessionid=<%=session.getId()%',
            ......
        });
  }); </script>

Answer №2

Uploadify relies on flash technology for its functionality. However, because each flash application initiates a new session, any previous session settings are lost when the new session starts. To overcome this issue, you can try implementing a workaround by passing the session ID to uploadify. If you are using Java, you can achieve this like so:

$("#uploadify_div).uploadify("settings", "formData", {"sessionID",<%=session.getId()%>});

In your action or servlet, retrieve the passed session ID from uploadify and resume the appropriate session. Please note that there are no built-in APIs for this purpose, so you will need to implement your own solution by mapping the session object to the corresponding ID.

I hope this information proves helpful to you.

Answer №3

Considering that uploadify relies on Flash technology for file uploads, it is advisable to investigate any potential issues with your Flash plugin or its configurations. It would be worthwhile exploring if the problem persists when attempting the upload from a different computer.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountering a npm script error while running on a Windows operating

While using webpack to build my application, I encountered the following error message in the command prompt: [email protected] dev D:\Myprograms\java script\forkify webpack --mode development The error mentioned: Insufficient num ...

The technique for handling intricate calls in node.js

My goal is to create a social community where users are rewarded for receiving upvotes or shares on their answers. Additionally, I want to send notifications to users whenever their answers receive some interaction. The process flow is detailed in the com ...

What is the best method to retrieve the Harvard GSD calendars with Ajax?

Currently, I'm working on setting this up using Ajax technology, and here's what I've managed to come up with so far. Despite numerous attempts, I still haven't been able to crack the code. function retrieveCalendarData() { $(".cals"). ...

Encountering a Next.js Strapi error. TypeError: Fetch request unsuccessful

An error occurred during module build: UnhandledSchemeError: The plugin does not support reading from "node:assert" URIs (Unhandled scheme). Webpack natively supports "data:" and "file:" URIs. You might require an extra plugin to handle "node:" URIs. ...

Adjusting the value of 'this' within a service using a function

I am a newcomer to Angular and currently delving deeper into its intricacies. Despite my efforts in researching, I have not come across a solution for the issue at hand. My service sets the initial value of this.totalCount = 0; Within my controller, upo ...

Sequencing the loading of resources in AngularJS one after the other by utilizing promises

While working in a service, my goal is to load a resource using $http, store it in a variable, load a child resource and store that as well. I understand the concept of promises for this task, but I am struggling with how to properly implement it in my cod ...

Automated Menu Selection using Selenium

I'm currently facing a challenge in writing a Python script using Selenium to interact with a webpage. I am struggling to use the .click() method to select an expandable list on the page. Despite successfully logging in and navigating to the desired p ...

Tips for arranging various information into a unified column within an Antd Table

Is there a way to display multiple data elements in a single cell of an Ant Design table, as it currently only allows insertion of one data element? I am attempting to combine both the 'transactionType' and 'sourceNo' into a single cell ...

Modifying radio inputs to update when a state variable is altered in react.js

In my HTML, I have a div that includes 4 radio inputs, and another div with an onClick event that increments a counter. Every time I click on the div, I want the radio inputs to reload so that no input is selected. Below is the code snippet: 'use clie ...

Switching minimum and maximum input values based on a specific condition: A step-by-step guide

I am looking for a way to reverse the minimum and maximum values of two input elements that I have defined. Is there a way to achieve this using HTML or JavaScript (Angular)? Here is an example of what I am trying to do: <label> Width: < ...

When I request the value of a JavaScript object, I am met with 'undefined'

I have been working on creating a Google map application that involves loading markers from a database and displaying them on a map. To accomplish this, I decided to create an array and define an object as shown below: function shop_info(name, latitude, l ...

Unable to save or create files in Store.js

Recently, I've been trying to save a file on client storage using Store.js. After changing the date with store.set and logging it to the console successfully, I encountered an issue where the data was not being saved in the app directory as expected. ...

Error occurs in Windows script while running a project installed globally

Upon installing my project globally, I encountered a Windows Script Host error. https://i.stack.imgur.com/unFVu.png What steps can I take to resolve this issue? The following is my JavaScript code snippet: Object.defineProperty(exports, "__esModule ...

Transform Django Model Instance from Serialized Back to Object using Ajax

Currently, I'm utilizing Ajax to search for a model instance. Once found, I return that instance and pass it as a variable to a template tag within my template. To achieve this, in my view, I serialize the object before sending it to the Ajax success ...

Testing Vue Components - Simulating the return value of a plugin

I have a unique scenario where I need to mock the return value of a custom plugin without importing it directly into my test. By creating a mock function for the plugin, I can easily achieve this goal. However, I am unsure how to change the return value of ...

Show individual row information within a bootstrap modal upon clicking the edit button

As a beginner in programming, I am attempting to use bootstrap modal to showcase row data from a mysql table within the modal window. Following the advice found on stackoverflow regarding "Pull information from mysql table to bootstrap modal to edit" didn ...

Arranging Angular Array-like Objects

I am in possession of an item: { "200737212": { "style": { "make": { "id": 200001510, "name": "Jeep", "niceName": "jeep" }, "model": { "id": "Jeep_Cherokee", "name": "Cherokee", "nice ...

How to show the raw image content-type using Vue.js

When retrieving an image from a REST API through an HTTP GET with a request body, I have successfully verified the returned content using node.js and chai.js: expect(res).to.have.header('Content-Type', 'image/jpeg'); expect ...

Combine two scope arrays in AngularJS

Is there a way to merge two arrays of scope in AngularJS within my controller so that I can display them in a single table? The merging would be based on a common field present in both arrays, similar to an SQL join operation where data from both tables ...

Linking a radio button to another mirrored radio button for selection

It should be a straightforward task. I have two sets of radio buttons that mirror each other, known as set A and set B Set A includes buttons 1, 2, and 3 Set B also consists of buttons 1, 2, and 3 The desired behavior is for clicking button 1 in set A t ...