Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Get Javascript values and use them in a servlet?

905759Dec 16 2011 — edited Dec 19 2011
Hey

I have a HTML which has Javascript scripts which I have to get values from (a array to be exact) and pass them to a servlet so I can process the information in a certain way. I cant seem to find a way to do this properly as the servlet is server side and Javascript is client side. How can I implement this?

Using a quick example example I have

C:/index.html
C:/js/script.js

Index.html contains:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="js/script.js">
</script>
</head>
<body>
a href="javascript:void(0);" id="add"><img src="images/menunew.png" alt="plus" border="0" />
/a>
</body>
</html>



Script.js contains:


var Script = Class.create({
a:4,
b:5,

add: function(){
return (this.a + this.b);
}
});

(Ignore syntaxis errors as this is a quick example I wrote up just to see if you can give me a practical example on how to read it.)

How can from a Java servlet access a and b from the script.js file (which contains a class named Script) and also access the function add which returns that value?

Thank you for the help

Edited by: 902756 on 16-dic-2011 0:18

Comments

gimbal2
I can think of three ways:

1. Use ajax
2. a form submit to the servlet (set value in hidden input field, submit form all using javascript)
3. a form submit performed inside an iframe (same as 2, but the current page doesn't reload)

I would go for option 1, that is the "web 2.0" way to do it.
905759
gimbal2 wrote:
I can think of three ways:

1. Use ajax
2. a form submit to the servlet (set value in hidden input field, submit form all using javascript)
3. a form submit performed inside an iframe (same as 2, but the current page doesn't reload)

I would go for option 1, that is the "web 2.0" way to do it.
1: Yes, it seems like the logically answer but Im not too up on AJAX and even less on mixing it up with servlets. Do you mind giving me a simple add (function) sample or something similar?
2: Not possible as I have to pass several values, a array, etc
3: The iframe is similar as 2 and its problems.

Thanks for the suggestions.
DrClap
902756 wrote:
1: Yes, it seems like the logically answer but Im not too up on AJAX and even less on mixing it up with servlets. Do you mind giving me a simple add (function) sample or something similar?
You are looking at it the wrong way. What you actually want to do is to have the browser send a request (which somehow contains that Javascript data) to a URL which causes the servlet to receive the data. There's no "mixing it up" going on there. The client and the server are entirely separate.

And since that request would be an HTTP request, it's just text. There's no concept in HTTP of transmitting objects, whether they be Javascript objects or Java objects. Anything you want to transmit has to be converted into text by the sender and converted back into objects by the receiver.

So there's no such thing as "a simple add function". You're going to have to stop thinking in that way and start thinking of client-server systems in the way they actually are.
905759
DrClap wrote:
902756 wrote:
1: Yes, it seems like the logically answer but Im not too up on AJAX and even less on mixing it up with servlets. Do you mind giving me a simple add (function) sample or something similar?
You are looking at it the wrong way. What you actually want to do is to have the browser send a request (which somehow contains that Javascript data) to a URL which causes the servlet to receive the data. There's no "mixing it up" going on there. The client and the server are entirely separate.

And since that request would be an HTTP request, it's just text. There's no concept in HTTP of transmitting objects, whether they be Javascript objects or Java objects. Anything you want to transmit has to be converted into text by the sender and converted back into objects by the receiver.

So there's no such thing as "a simple add function". You're going to have to stop thinking in that way and start thinking of client-server systems in the way they actually are.
The thing is I want to start simple to grab a simple concept....


Im trying a AJAX route and I have the following:

/index.html
/js/Adding.js

Index contains:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<script type="text/javascript" src="js/Adding.js"></script>
</head>

<body>
<select name="num1">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<select name="num2">
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
</select>
<form method="get" name="adding">
Do
<span id="res"></span>

</form>

</body>
</html>

Adding.js contains

var Adding = Class.create({
x: null,
y: null,

add: function()
{
var xmlHttp = new XMLHttpRequest();
var value1 = document.getElementById("num1").value;
var value2 = document.getElementById("num2").value;
xmlHttp.open("GET", "index.html", false);
xmlHttp.send(value1 + value2);
var result = document.getElementById("res");
result.innerHTML = xmlHttp.responseText;
}

});


This shows 2 comboboxes full of numbers and link saying "Do" which calls the javascript function and adds the two numbers in the comboboxs and shows them on the screen without refreshing the page. This does not work and I would like this to work before I continue onto the bigger picture.
EJP
This is clearly a Javascript question in a Java forum. Locking.
1 - 5
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jan 16 2012
Added on Dec 16 2011
5 comments
3,960 views