Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to pass a javascript variable on a jsp to a servlet

 
jordan campbell
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


i want to pass the var num to a servlet. I am aware of how to retrieve a variable using
String num = request.getParameter("var"); but i do not think this is appropriate for what i am trying to do. Does anyone have any idea of what i am doin wrong?
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please put your code between the code tags.

The easiest way is to append the variable to the new location as a query string.

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64838
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is it not "appropriate"? After you submit it, of course.
 
jordan campbell
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a complete novice so i do not really understand what you mean by that. Could you explain it a bit further?Thank you for your help


 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64838
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To get the value of the JavaScript variable, a request needs to be submitted to the servlet, passing the value as a request parameter. That can happen on the URL, or as part of a form submission.

In the servlet, you'd use the getParameter() method to retrieve it.
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what you are trying to achieve here, but if you want to send some values to the server from your JSP without reloading the page (i.e. submit), then you can use AJAX to make a request to the servlet...
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64838
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As the OP is a novice, I think bringing up Ajax may be a bit premature.
 
jordan campbell
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

this is the request that i have in my servlet which i hoped would get the parameter.

Is this how i would pass it?

 
jordan campbell
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
basically the reason i want to do this is that i have a dynamic form that extends to add multiple inputs. I want to assign a unique id to each impairment as the form extends. Then the servlet requests the impairment name with the unique id so i can iterate over them and add them into the datase
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this how i would pass it?

No, you'd need to make a valid URL, like foo?bar=baz&plugh=xyzzy. Note the ampersand.

You might want to check out some basic tutorials on webapps.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64838
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you can add hidden inputs to the submitted form.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64838
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm moving this to the HTML forum as it really has nothing to do with servlets or JSP.
 
jordan campbell
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String nume = request.getParameter("myValue");
This is how i tried to retrieve myValue on the servlet



i used

to try to assign the num value to myValue




When the request is sent for myValue it keeps coming out as null. Can anyone explain what it is im doing wrong?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic