This discussion is archived
5 Replies Latest reply: Dec 19, 2011 1:20 AM by EJP RSS

Get Javascript values and use them in a servlet?

905759 Newbie
Currently Being Moderated
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
  • 1. Re: Get Javascript values and use them in a servlet?
    gimbal2 Guru
    Currently Being Moderated
    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.
  • 2. Re: Get Javascript values and use them in a servlet?
    905759 Newbie
    Currently Being Moderated
    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.
  • 3. Re: Get Javascript values and use them in a servlet?
    DrClap Expert
    Currently Being Moderated
    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.
  • 4. Re: Get Javascript values and use them in a servlet?
    905759 Newbie
    Currently Being Moderated
    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.
  • 5. Re: Get Javascript values and use them in a servlet?
    EJP Guru
    Currently Being Moderated
    This is clearly a Javascript question in a Java forum. Locking.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points