How to store JavaScript variable value in JSP session
-
I want to store a JavaScript variable value in JSP session to use it further in my Java classes methods. Please provide solution for storing any JavaScript variable in JSP session.
-
Answer:
Make an AJAX call from your JSP to a different JSP that does that. The jsp will take a variable name and its value as request parameters and will put the variable in the session. You cannot do this with client side (javascript) code only !
pankaj rathod at Stack Overflow Visit the source
Other answers
Pass that variable to Servlet using ajax OR set it in some hidden field when form will get submit it will also get POSTed.
Jigar Joshi
Your best bet is to just store the value in a cookie. Both client side javascript and server side jsp can access cookies.
herostwist
There should be a natural flow to your program, so that you don't have to have a mechanism specifically for storing variables just for the sake of storing variables. By that I mean that if you are storing a userName, it should be while login is occurring, or if you are obtaining contactInfo, then you should have a servlet specifically for that purpose. Regardless, here is a three part answer to your question. It is very general and non-specific, but will accomplish what you want once fleshed out: Here is the web.xml file: <servlet> <servlet-name>StoreVariableServlet</servlet-name> <servlet-class>tests.servlets.StoreVariableServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>StoreVariableServlet</servlet-name> <url-pattern>/storeVariableServlet</url-pattern> </servlet-mapping> Here is the jQuery Code: $.ajax({ type: "POST", url: "/storeVariableServlet", data: ({ variable : $(variableName).val() }), success: function(){ alert("Success"); } }); Here is the Servlet Code: String variableToBeStored = request.getParameter("variable"); HttpSession session = request.getSession(); session.setAttribute("variableXYZ", variableToBeStored);
Owen Berger
Related Q & A:
- How to pass javascript jQuery variable value in php array?Best solution by Stack Overflow
- How to retrieve a variable name given its value?Best solution by Stack Overflow
- How to store multiple record in Mysql variable?Best solution by Stack Overflow
- How to get JavaScript value in PHP?Best solution by Stack Overflow
- How much should I charge for a tutoring session?Best solution by angieslist.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.