• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or you can add hidden inputs to the submitted form.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
reply
    Bookmark Topic Watch Topic
  • New Topic