my dog learned polymorphism*
The moose likes HTML, CSS and JavaScript and the fly likes How do I retrieve Javascript values from a Java servlet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "How do I retrieve Javascript values from a Java servlet?" Watch "How do I retrieve Javascript values from a Java servlet?" New topic
Author

How do I retrieve Javascript values from a Java servlet?

Koldo Urrutia
Ranch Hand

Joined: Dec 16, 2011
Posts: 85
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

The index.html contains:



The script.js contains:



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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61756
    
  67

You can't. The servlet runs on the server to format a response to send to the browser, which can be an HTML page containing JavaScript. The script is evaluated and executed on the browser.

What are you actually trying to accomplish?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Koldo Urrutia
Ranch Hand

Joined: Dec 16, 2011
Posts: 85
Thanks

Before getting the actual thing done, maybe we should try to resolve the example I posted to get a better idea.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
Well, obviously, the servlet is going to be getting a request so that request will have to contain any values you want to transmit.

Your choices are:
1. a GET request with parameters in the URL just like a HTML form with a GET
2. a POST request with parameters in the body of the request, just like a HTML form with a POST

Bill
Koldo Urrutia
Ranch Hand

Joined: Dec 16, 2011
Posts: 85
William Brogden wrote:Well, obviously, the servlet is going to be getting a request so that request will have to contain any values you want to transmit.

Your choices are:
1. a GET request with parameters in the URL just like a HTML form with a GET
2. a POST request with parameters in the body of the request, just like a HTML form with a POST

Bill

Well yes but the thing is how do I pass those values from the Javascript to the request (irrelevent if it is via GET or POST)?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61756
    
  67

For a GET, append the values to the URL as part of the query string. For a POST, set them as values of the submitted form; hidden elements work well for this.

In any case, this really isn't a servlet question -- so it's been moved to the HTML/JavaScript forum.
Koldo Urrutia
Ranch Hand

Joined: Dec 16, 2011
Posts: 85
Bear Bibeault wrote:For a GET, append the values to the URL as part of the query string. For a POST, set them as values of the submitted form; hidden elements work well for this.

In any case, this really isn't a servlet question -- so it's been moved to the HTML/JavaScript forum.

But how do I pass that to a servlet?
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
You submit an HTML form to the server or you make an Ajax call to the server with the data you want the server to have.

Eric
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61756
    
  67

Koldo Urrutia wrote:
Bear Bibeault wrote:For a GET, append the values to the URL as part of the query string. For a POST, set them as values of the submitted form; hidden elements work well for this.

In any case, this really isn't a servlet question -- so it's been moved to the HTML/JavaScript forum.

But how do I pass that to a servlet?

If the link or the form action addresses the servlet, the servlet will be invoked and can retrieve the values.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do I retrieve Javascript values from a Java servlet?