Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

How can you copy a JavaScript variable to a JSP session variable without using cookies?

user-image
Question added by Yasmeen Husam , Web Developer , Al Fahid Systems
Date Posted: 2019/01/30
Parmod Prajapati
by Parmod Prajapati , Java Developer , Indianmesh Pvt. Ltd.

You may use ajax, when you want to save variable value into session

Ammar Ayaz
by Ammar Ayaz , Senior Web Developer , Instacommerce

JSPs run at the server, while JavaScript runs at the client - never the twain shall meet. 

 

The JSP already knows the URL of the page it's generating - what else are you looking for?But might be this can help you:-

There are few things you have to do in order to make it to work.

 

A. You need a servlet to store the selected value of SELECT in session in the first place. If the SELECT list is static , just store the selectedIndex in the session.

 

B. On the same JSP where SELECT resides, you need put a server side code close to this

 

%  

final HttpSession   curSession   = request.getSession( false );

  final String    selectedIndex  = (String)curSession.getValue(selectedIndexKeyName);

%>

to pull the selectedIndex out of the session you put in the first place.

 

C. write onLoad method for JSP where SELECT resides

<body onLoad='initPage ()'>

where intiPage() is a JavaScript function that will set SELECT list selectedIndex to previous value.

document.frmCity.selCity.selectedIndex = %= selectedIndex %>

 

More Questions Like This

Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.