I have session key that is a JavaScript variable which I got from a REST API call. I need to call my Java code in a servlet and pass that key as a parameter. What JavaScript function can I use to do that?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Several ways:
Use
window.locationto fire a GET request. Caveat is that it”s synchronous (so the client will see the current page being changed).Note the importance of built-in
encodeURIComponent()function to encode the request parameters before passing it.Use
form.submit()to fire a GET or POST request. The caveat is also that it”s synchronous.With
Alternatively you can also only set the hidden field of an existing form and just wait until the user submits it.
Use
XMLHttpRequest#send()to fire an asynchronous request in the background (also known as Ajax). Below example will invoke servlet”sdoGet().Below example will invoke servlet”s
doPost().Use jQuery to send a crossbrowser compatible Ajax request (above
xhrcode works in real browsers only, for MSIE compatibility, you”ll need to add some clutter 😉 ).Note that jQuery already transparently encodes the request parameters all by itself, so you don”t need
encodeURIComponent()here.Either way, the
keywill be just available byrequest.getParameter("key")in the servlet.See also: