File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do I retrieve Javascript values from a Java servlet?

 
Koldo Urrutia
Ranch Hand
Posts: 85
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64178
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Koldo Urrutia
Ranch Hand
Posts: 85
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13045
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 85
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64178
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 85
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15385
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64178
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic