• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

How do I retrieve Javascript values from a Java servlet?

 
Ranch Hand
Posts: 85
  • 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.
 
Marshal
Posts: 67366
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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
  • 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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • 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
  • 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
Marshal
Posts: 67366
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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
  • 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?
 
author
Posts: 15385
6
  • 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
Marshal
Posts: 67366
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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.
 
You’ll find me in my office. I’ll probably be drinking. And reading this tiny ad.
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!