What is the best way to retrieve a JavaScript session attribute within a JSP page?

function saveToSession() {
    var name = document.getElementById('name').value;
    var mobile = document.getElementById('number').value;

    sessionStorage.setItem("name", name);
    sessionStorage.setItem("mobile", mobile);
    document.getElementById("leadForm").action = "/validate";
    document.leadForm.submit();
}

After the validation, I need to display the saved name and mobile on another JSP page along with an additional attribute selected on the previous page.

I am struggling with passing session values from JavaScript to JSP.

In my JSP page, I have tried:

String storedName = (String) session.getAttribute("name");
out.println(storedName);

However, I keep receiving a value of null.

Answer №1

sessionStorage holds data in the browser, and the variables name and mob are defined on the client side rather than within the request.

You have the option to either transmit them to the server side or showcase their values using JavaScript once more.

  sessionStorage.getItem('name');

Answer №2

It's important to note that JS sessionStorage and JSP session are not interchangeable.

If you've filled out an HTML form with your name and mobile number, follow these steps:

submit.jsp

<form action='confirm.jsp'>
   Name: <input type='text' name='name'/><br/>
   Mobile No: <input type='text' name='number'/><br/>
   <input type='submit' value='Submit'/>
</form>

confirm.jsp

Name: ${param.name}<br/>
Mobile: ${param.number}<br/>

Answer №3

To keep track of important information, you can hide fields for these variables within the leadForm.

<form name="leadForm" method="post" action="/confirm">
    <input type="hidden" name="name"/>
    <input type="hidden" name="mob"/>
    ...
</form>

In your coding, make sure to assign values to them using your #input() function:

document.leadForm.name.value = name;
document.leadForm.mob.value = mob;

When you submit the form, you will have access to the stored values:

String name = (String) request.getParameter("name");
String mob = (String) request.getParameter("mob");

These details will not be saved in the session since HTTP is stateless. However, they will be part of the HTTP request. If needed later on, they can be stored in the session.

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

What is the correct way to integrate the Ant Design library with Next.js for seamless server-side rendering?

I duplicated the official Next.js example using Ant Design at this link: https://github.com/vercel/next.js/tree/canary/examples/with-ant-design After cloning, I proceeded with npm install to install all dependencies. Then, I ran npm run dev to check if ev ...

Collapse the Bootstrap Menu upon selecting a Link

I am working on a side menu with an offcanvas display feature. The current functionality is such that when the button is clicked, the canvas pushes left and opens the menu. However, I want the menu to collapse and close automatically when a link is selecte ...

What is the process for adjusting the input value dynamically in reactjs?

I am working on a dynamic time input row and I need to ensure that the values are updated correctly. This is the code snippet I am using: https://codesandbox.io/s/624vq8y7y3 When I run the code, the values in the TimeInput field do not change as expected ...

Setting the Datetimepicker to be used with every input field

I currently have four textboxes identified by their unique ids: title, sdate, edate, and pdate. My goal is to assign a Datetimepicker to the textboxes that contain the word 'date' in their id. This means I want the Datetimepicker to be assigned ...

"Utilize Vuejs to establish a binding between two objects when necessary

With the help of moment, my calendar generates 41 days. for (let x = 0; x < 42; x++) { context.add(1, 'd'); let day = { 'date': moment(context), 'events': [] }; } ...

Enhance your Javascript skills by practicing the algorithm to remove negative numbers from an

I'm really struggling to understand this concept :/ (Eliminating Negative Numbers) Suppose you have an array X containing various values (e.g. [-3,5,1,3,2,10]). Develop a program that removes all negative numbers from the array. By the end of your ...

Is it possible to enclose a form inside a Bootstrap popover?

<div class="container"> <div class="row" style="padding-top: 240px;"> <a href="#" class="btn btn-large btn-primary" rel="popover" data-content="<form ...

Having trouble retrieving the file attachment with Node js

Hey there, I am currently facing a problem with downloading files in Node Js when using post and get methods. Below you will find the code snippet that I have been working on: router.post('/download',function(req,res,next){ var files=req.bod ...

Issue with the bootstrap toggle menu script not functioning as expected

My toggler menu is not working and I'm not sure why. I'd also like to know which links to remove and how to improve my website's speed. When I resize the browser window, the toggle menu icon is not displayed correctly and does not open a new ...

The toggle in the dialog box refuses to submit the information

I am facing an issue with a dialog box that I am displaying using the jQuery .dialog() function. The dialog box contains a button that I want to use to post back to another page, but for some reason, it is not working as expected. I have tried setting the ...

Does an inverted v-cloak exist?

As stated in the VueJS documentation, the v-cloak directive serves to conceal uncompiled mustache bindings until the Vue instance is fully prepared. This means that a particular element, such as a div, can be hidden and only displayed once Vue is ready. I ...

Identifying a successful link click using the selenium-webdriver npm package

Is there a foolproof method to confirm a successful link click to an unfamiliar page using selenium-webdriver npm? I have the necessity to verify a basic page load on my website and ensure that an external link to an unknown page loads correctly. The proj ...

What is the best way to patiently wait for lines to be printed out one by one

I am in the process of creating my own personal website with a terminal-style design, and I'm looking to showcase a welcome banner followed by a welcoming message. The effect I have in mind involves lines appearing individually from top to bottom and ...

Can the script be loaded into a TypeScript file?

I'm currently in the process of integrating this script tag into my Angular 2 project, but I'm searching for a way to incorporate it into the typescript file so that I can access its methods within the .ts file. <script type="text/javascript" ...

Error: The function cannot be applied to req.body

I am designing a feature to calculate the total nutritional value of selected food items from a table. Users can input the quantity for each food item and choose which items to include using checkboxes. The program will multiply the quantity by the nutri ...

Tips for integrating @mdx-js/loader with your create-react-app project

Following the guidelines found at: The steps I took were: $ npx create-react-app react-app $ cd react-app $ npm install @mdx-js/loader Then, according to the instructions, I created the src/content.mdx file like this: # Hello, world! This is **markdown* ...

jquery string concatenation formatting

Hey there, I'm currently working on getting this to display properly. I have the following code, but I seem to be struggling with concatenating JavaScript strings, especially when it comes to the background div line. $( "<div class=\"images&b ...

Error: The value is null and cannot be read (specifically the property '_id')

Having trouble retrieving a document from mongodb and getting an error message. Not sure if it's a database issue or a code problem. Any assistance would be appreciated! Encountered an error on event listener "guildMemberAdd" for event "guildMemberAdd ...

Issue with Bootstrap.js causing mobile menu to not expand when toggled in Rails app

Despite adding the .toggled class, my hamburger menu is not expanding when I click on the mobile menu. I've tried adjusting the order of the required javascript commands, but it doesn't seem to be working as expected. Here are the steps I'v ...

Utilizing Node.js in conjunction with xlsjs or exceljs allows for seamless

Currently, I am exploring the options for manipulating an Excel workbook within a node environment by utilizing the xlsjs module. If you are interested, you can find more information about the xlsjs module here. In addition to this, I have also come acro ...