Hey

I have a program in retrieving data from a HTML page with Javascript using Java. Lets say I have the structure of

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>

The script.js contains:

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

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

});

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?

(Note that the code I just wrote might be incorrect as I wrote it quickly just to demostrate whats my problem)

This is driving me crazy as I have tried with Map and retrieve it thru request but nothing. There isnt any way I see right now.

Thanks.

Ive been trying to use FireBug to where the code is generated (and store) and I think more or less Ive tracked it down (well, I can get it from more than once place)

Just need to know how using a servlet I can get those values.

Thanks for moving the thread to the correct section as I need help because I am totally lost.

Im surprised there is no reply as I often get quick and good replies on this site. Is there any information that is needed so I can try to see if I can share it and give more insite on what is happening?

I'm failing to understand a reason for this to be done, so not surprised that no one else replied. Why do you want to get JavaScript variables and returned value?

I'm failing to understand a reason for this to be done, so not surprised that no one else replied. Why do you want to get JavaScript variables and returned value?

To process that data in a Java servlet.

If you want it to process in servlet then you shouldn't put it in JavaScript

If you want it to process in servlet then you shouldn't put it in JavaScript

The information I need is in a Javascript so not putting it there is kind of out of the question...

This Javascript was given to me so I didnt have a chance to write it. I was given it and I have been told to get certain information from it.

Now that sounds as completely different thing, more of the old way to build/render site with complex

public class MyServlet extends HttpServlet{
	public void doGet(HttpServletRequest request, HttpServletResponse response) throw IOException{
		PrintWritter out = response.getWritter();
		java.util.Date today = new java.util.Date();
		out.print("<html "+
			"<body> "+
			"<h1>"+
			"MY SERVLET"+
			"</h1>"+
			"</body>"+
			"</html>");
	}
}

This is long abandoned practice, and even then you would just render page through print but not try to execute JavaScript

Now that sounds as completely different thing, more of the old way to build/render site with complex

This is long abandoned practice, and even then you would just render page through print but not try to execute JavaScript

It seems I have explained it wrong :) My apoligies.

In index.html posted in the first page, there is a link that executes that add function and returns (the numbe) 9. I need to get that 9 and in my servlet (for example) insert it into a database. Thats why I NEED the servlet to get the Javascript value it produces.

Thanks for helping out as it is killing me and I just have no direction at all for this. Im completely puzzled....

Lets step back and please explain what is this script doing, in what context it is used and what is the final outcome of whole operation

Lets step back and please explain what is this script doing, in what context it is used and what is the final outcome of whole operation

OK :)

There is a Javascript (with other several Javascript classes) that basically draws points and make a drawing. What I need is each points locations (x, y), the order, and the type of line. All of this is stored and calculated in Javascript.

This is loaded in a HTML page and is displayed. There is a button that says "Submit"

When I hit "Submit" I am suppose to have that information. Once I have that information, there is some other things I have to do inside the servlet.

Basically thats what I have to do :) If you need more information, ask and I will try to give it to you.

Thank you

Then you should have this script populate some field of form that you can submit and in doing so you will retrieve these values. Shopping basket is good example, you have price, you know number of items and based on that you will calculate sum for all of them

Then you should have this script populate some field of form that you can submit and in doing so you will retrieve these values. Shopping basket is good example, you have price, you know number of items and based on that you will calculate sum for all of them

Kind of sloppy to make (for example) a hidden list box with all the values and submit it using a form element no?

Sloppy is more suited to the design that was chosen for this. Sorry, but above is just absolutely messed up approach either of attempting to fix some bug or beginner not knowing what he is supposed to do. I may sound rude, but seriously till this minute I have no clear idea what you trying to do beside localized element/issue/idea on some supposedly Java web based project that for some reason wants to heavily relay on some JavaScript

Sloppy is more suited to the design that was chosen for this. Sorry, but above is just absolutely messed up approach either of attempting to fix some bug or beginner not knowing what he is supposed to do. I may sound rude, but seriously till this minute I have no clear idea what you trying to do beside localized element/issue/idea on some supposedly Java web based project that for some reason wants to heavily relay on some JavaScript

OK, then lets start small and once I get one thing working, we can continue and get more complex.

Here is a example of something related. Lets get this working and then we can move on.


Im going to try a AJAX route as from reading this seems to be the most logical form to do it. 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">
<a href="javascript:add();" id="add">Do</a>
<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 simple example 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. Forget everything else Ive said in the past in this thread. Lets get this working and then, once I understand this I might be able to explain better because this way I know how it works and I can explain the actual situation alot better.

And no, the Adding.js function add cannot be put in the html page. Not because I dont want to (id love to) but it just cant.

Thanks for the replies.

xmlHttp.open("GET", "servlet", false);

In doGet

//try
String input = request.getParameter("num1");

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.