• Post Reply Bookmark Topic Watch Topic
  • New Topic

Access a JavaScript variable from a Java program

 
learner
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to access a JavaScript variable from a Java program (not a JSP program) ? The java program is written as a Web Server and has to retrieve variables from a form.

thanks
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tell us about "the java program is written as a web server" in a bit more detail. Does it really open a server socket, accept connections from a browser and read raw requests from the input stream? I did this for my Wiki so it is certainly possible (and fun) but a bit involved.
[ January 19, 2006: Message edited by: Stan James ]
 
learner
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stan

As you have said, the web server opens a server socket, accepts connections from a browser and reads raw requests from the input stream. The request is to view a file. So only the get method is used now. But I would like to add a checkbox and give option to the user for selecting files for viewing. The script to handle onclick is written in javascript where the checkbox values are assigned to variables. I want to know how to pass these javascript variables to the web server so that I can do some processing.

The web server has been written based on http://www.oreilly.com/catalog/javanp2/chapter/ch11.html


thanks
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're setting about to implement the server side of the HTTP protocol, which is fun and educational. It's a lot less cost effective than just picking up somebody else's implementation but I did it myself just so I'd have something non-trivial to work on at home.

Look into the W3C docs for what is supposed to happen. It's a little dense to follow some times, so you might also pick up an HTTP sniffer and watch exactly what your browser sends to a real server and what the server sends back.

In short, read all the headers using the equivalent of readline. An empty line marks the end of the headers. You've already done GET so you know the first header. For POST the content-type header will either be multi-part mime or a simple form. For a simple form, the line after the header will be a bunch of name=value&name=value pairs with the fields from the form that was posted.

Is that enough to get started?
 
learner
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stan,

Your suggestions worked.

thanks
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool! Let us know how your server turns out!
 
learner
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having problems passing the request parameters (name, value pairs). When I click on a checkbox in the form, nothing should happen. When I click on a link (href), I call the submit function of javascript. So the request parameters (values of checkboxes) are sent as a query string (http://....?name=on...etc). I store these parameters as a collection. But I get the following error. "Socket Error # 10053 Software caused connection abort." Looks like the socket is closed or buffer is not big. how else can I handle the request parameters ?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!