aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes How to pass a javascript variable on a jsp to a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "How to pass a javascript variable on a jsp to a servlet" Watch "How to pass a javascript variable on a jsp to a servlet" New topic
Author

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

jordan campbell
Greenhorn

Joined: Jul 11, 2009
Posts: 7


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

Joined: Feb 23, 2009
Posts: 1183
Please put your code between the code tags.

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



JDBCSupport - An easy to use, light-weight JDBC framework -
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60997
    
  65

Why is it not "appropriate"? After you submit it, of course.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
jordan campbell
Greenhorn

Joined: Jul 11, 2009
Posts: 7
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

Joined: Jan 10, 2002
Posts: 60997
    
  65

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

Joined: Aug 03, 2008
Posts: 9293
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60997
    
  65

As the OP is a novice, I think bringing up Ajax may be a bit premature.
jordan campbell
Greenhorn

Joined: Jul 11, 2009
Posts: 7

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

Joined: Jul 11, 2009
Posts: 7
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Jan 10, 2002
Posts: 60997
    
  65

Or you can add hidden inputs to the submitted form.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60997
    
  65

I'm moving this to the HTML forum as it really has nothing to do with servlets or JSP.
jordan campbell
Greenhorn

Joined: Jul 11, 2009
Posts: 7
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?
 
Consider Paul's rocket mass heater.
 
subject: How to pass a javascript variable on a jsp to a servlet