Accessing Session Objects in Struts2 Using a Different Session Object as a Key

I am fairly new to working with Struts2 and currently, I am faced with the challenge of retrieving a session object using a dynamic session key.

Here's how my application flow goes: The user will trigger the action from their browser

http://localhost:8080/prjcr/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="76061712581715021f191849030513041f124b06620041">[email protected]</a>

Within the action class, I fetch a list of values from a web service by utilizing the [email protected] request parameter, and then proceed to store them in the session as shown below:

// Extracting the request parameter
userid = ServletActionContext.getRequest().getParameter("userid"); 
QDefn[] qDefn = proxy.getQDefns(userid); // making a call to the webservice   
List<QDefn> qdList = new ArrayList<QDefn>();   
qdList.add(Arrays.asList(qDefn));

We then extract the userid portion of the request parameter to serve as the session key:

userid = userid.substring("UserId", userid.substring(0, userid.indexOf('@'));
// Using the received request parameter itself as the key
ActionContext.getContext().getSession().put("UserId", userid);

Subsequently, we insert the associated list of values into the session:

ActionContext.getContext().getSession().put(userid, qdList);  

The next step involves forwarding to a JSP that showcases this list in a select drop down menu:

<s:select name="qdefn" 
id="qdefn"

list="#session.%{#session.UserId}"  ---What exactly does this notation mean??

listKey="defnName"
listValue="defnName"
headerKey="ALLQD"
headerValue="All" > </s:select>

I have been attempting to retrieve the qdList from the session in JSP using a dynamic session key such as the userid. In Java, we usually achieve this as session.get(userid). However, the OGNL notation used in Struts2/OGNL is posing some difficulties for me at the moment. Any help would be greatly appreciated.

Thank you in advance!

Answer №1

When performing

System.out.println(ActionContext.getContext().getSession().getClass());

in your action, you will receive 'class org.apache.struts2.dispatcher.SessionMap'. However, if you execute

out.println(session.getClass());

in your JSP, you may see 'class org.apache.catalina.session.StandardSessionFacade' (specific to Tomcat).

Therefore, consider using

session.getAttribute(userid);

instead of

session.get(userid);

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

Execute JavaScript function after completion of CSS animation using jQuery

I'm working on an accordion feature that uses CSS animation to expand the clicked item. The expansion is triggered by li:target. However, I'm facing an issue where when clicking on an item, the scroll position doesn't align correctly with t ...

Exiting the Protractor timeout setting for Safari WebDriver

While I have experience with unit tests using Karma, my office is now interested in integration tests, specifically to test cross-browser capabilities. Protractor seemed like the best option for this, so I began working on some basic dashboard tests. Howev ...

Why is the deployed Express server failing to establish a session?

After deploying a node express server on Digital Ocean, I encountered an issue where the session was not being created. To address this, I implemented a store to prevent memory leak and included app.set('trust proxy', 1) before initializing the s ...

Looking for a way to manipulate the items in my cart by adding, removing, increasing quantity, and adjusting prices accordingly. Created by Telmo

I am currently working on enhancing telmo sampiao's shopping cart series code by adding functionality for removing items and implementing increment/decrement buttons, all while incorporating local storage to store the data. function displayCart(){ ...

Ant design of the card

I am struggling to make the image appear in full width within the ant design card component. import React, { useState, useEffect } from 'react'; import uno from '../images/dualComida.jpg' import { Button, Space, Typography, ...

What could be causing me to struggle with finding elements on Flipkart by using the classname attribute?

When trying to find an element on Flipkart, I am utilizing the "class name" attribute in this way: WebElement element = driver.findElement(By.className("_1QZ6fC _3Lgyp8")); The issue I am encountering is: org.openqa.selenium.NoSuchElementException: Unabl ...

Converting large JSON data (approximately 100MB) into an Excel file using JavaScript

As a beginner in the realm of REST technology, I find myself navigating through a JSON response received from a server and showcasing the data on the client side. Currently, I am faced with handling approximately 22MB of JSON data that needs to be exporte ...

JSP page displaying a table with a text input field named "code" enclosed within <TD> tags

Recently, I designed a JSP page that features a table with two columns: Code and Description. The table data is an input type of "text" with the name attribute set to "code". The main functionality I need to implement is the automatic generation of the d ...

Tips for retrieving corresponding values from a TypeScript dictionary object?

I am currently working with a dictionary object that is filled in the following manner: const myDictionaryElement = this.myDictionary["abc"]; In this case, myDictionaryElement contains the values: ACheckStatus: "PASS" QVVStatus: "READY" VVQStatus: "READ ...

Postman: Iterating through requests with various input data sets to dynamically generate the request body

I am facing a challenge with my login API request that requires 3 parameters (userName, password, and remember) in the request body. Out of these parameters, userName and password are mandatory, while remember is optional. The input data is being pulled fr ...

Error: The iOS 14.2 system is unable to locate the variable "webkit."

Currently, I am developing a web application using AngularJS 1.7. The app runs smoothly on Safari with iOS versions 12, 14.0, and 14.1. However, upon upgrading my iOS to version 14.2/14.3 (tested on both), I encountered the following error: Error: Referen ...

When the input field is clicked, the file:/// URL is sent

Currently, my HTML page contains a form that includes an input field for URLs. Ideally, upon typing in the URL and clicking the button, I intend to be redirected to that website. However, the issue lies in the fact that instead of redirecting me to the p ...

retrieving information from a data attribute

When setting a data attribute for a user on a link, the code looks like this: <input type="button" class="btn" data-user={"user": "<%= @user.name %>"} value="Start" id="game"> Upon listening for the click event in the JavaScript function, co ...

Creating a SAS URL for Azure Blob storage in Node.js using the generateBlobSASQueryParameters method from the @azure/storage-blob module

Hello, I am working with an Azure storage account where I upload and create a SAS URL to download images. Below is the code snippet that I have used: const { BlobServiceClient, StorageSharedKeyCredential, BlobSASPermissions, generateBlobSASQueryPar ...

Tips for declaring a particular type during the process of destructuring in Typescript

I have my own custom types defined as shown below: export type Extensions = | '.gif' | '.png' | '.jpeg' | '.jpg' | '.svg' | '.txt' | '.jpg' | '.csv' | '. ...

Terminate a targeted recipient following the completion of the on event

I am currently running a node service with socket.io and utilizing an event emitter to send updates based on user context. For example, context A may have users 1, 2, and 3 while context B has users 4 and 5. Once a successful connection is established wit ...

How can I extract the initial values from PHP after receiving the JSON + serialized data in the Ajax response following the first submission?

I am currently utilizing ajax to store data for multi part forms. My goal is to save each form's data upon clicking the next button. I have utilized form data to serialize the information, however, the format of the data is not aligning with my expect ...

Ways to determine the types of props received by a function when the arguments vary for each scenario?

I have a specialized component that handles the majority of tasks for a specific operation. This component needs to invoke the onSubmit function received through props, depending on the type of the calling component. Below is an example code snippet show ...

Manually initializing Angular bootstrap with async in the Angular script tag

I am encountering an issue when trying to asynchronously download the Angular script in my application and manually bootstrap the application upon loading. The error message states: Failed to instantiate module wt due to: Error: [$injector:modulerr] htt ...

Automate your browsing experience by using Selenium to effortlessly click on links

Struggling to access a link within a list? Take a look at the screenshot showing the "Algeria" link. Need help getting there? View image of the list and elements CSS: #\33 \2c ALG Xpath: //*[@id="3,ALG"] Attempts have been made using xpath an ...