Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing values to and from HTML

 
Tim Dee
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I pass arguments from a HTML page to a Java applet and back again?

Ie. If I have a web page that needs to convert Fahrenheit to Celsius. I would have a HTML page to enter the temp in Fahrenheit then this value needs to be passed to a Java app which returns a Celsius value.
This value then needs to be displayed on the HTML page.

I have a Java app �FtoC� that if I type:
Java FtoC 70
It returns 21
This value is just printed. In the app the Celsius value is �cel� but I�m not sure how this value can be read by the HTML.

Many thanks for any help.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim,

As you might imaging for something so important, there are many ways to do this. The whole Web is built this way, so many different solutions to your problem have been created. Most of these solutions, however, sit on top of two core concepts: JSPs (Java Server Pages) and Servlets. You probably want to start with JSPs. Therefore, I'm going to move this thread to our JSP forum (up at the top of the main Saloon page). Can somebody there please clue Tim into some good beginner's resources?
 
Matthew Egbers
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well tim, i would do it with some java script like this


[ September 20, 2006: Message edited by: Bear Bibeault ]
 
Matthew Egbers
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also, you must change the onglick to "on click" with no space. It wouldnt let me post with 0nclick
 
Tim Dee
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the Java Script solution.

However what I'm really trying to figure out is how to pass information from a web page to an app and then display that result.

How can I do this?

Many thanks again for the help.
Tim
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64833
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sticking with Java, does it really need to be a stand-alone app, or are you willing to adopt a more web-friendly solution?

If you code your logic into a encapsulated, reisable class (you know, that whole OO thing) such that it could be called both from a stand-alone app or a servlet (or any other Java class for that matter), your job will be much much easier.

Not only that, but it's just good OO design.
[ September 20, 2006: Message edited by: Bear Bibeault ]
 
Tim Dee
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The stand alone app I have created works fine from a DOS box, it's just not pretty. I could write into the app some Swing code to make a nice GUI. But thats not what I'm trying to figure out.

I'm just interested in finding out how I would write a HTML front end to enter a value into the app and for it to return the result back. I know the temperature conversion thing can be done in JavaScript and it works very well, thank you for the code. I also know I can create a nice GUI using Swing within the app. But what I'm trying to do is transfer values from a web page to an app and back again. Just as an experiment, to find out how it's done.

Here is another example:
If I create an app or class to add 3 values together I can, in DOS, type 'Java appName 1 2 3' and the values are passed to the app from DOS. The sum of these 3 values would be returned to DOS using the Print command and a 6 will be displayed.

Is it possible to write some web page front end to pass these values to the app/class? Be this web page standard HTML, JavaScript, a Form or PHP etc. I don't know. It would appear it is podssible, but I don't know how to achieve this. Can anyone give me an idea of how to do this?

Cheers
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64833
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're approaching it wrong.

You've tied yourself to a specific user interface, in this case the command line -- and yes, the command line IS a form of user interface -- and are trying to shoe-horn it into other UIs.

The correct approach is to code your logic into a class that is entirely UI-agnostic. That includes ignoring the command line.

This class can then be used over and over again in any UI environment be it a command line, Swing, the web, a cell phone, spoken interface, or what have you.

This is the heart of the MVC pattern in which the "model" code is divorced from any specific UI.

The first step is to back away from pre-conceived notions that a "java progam" is by nature a command line application.
[ September 20, 2006: Message edited by: Bear Bibeault ]
 
Tim Dee
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK thanks. So the way I have coded it only accepts input via the command line.

What I am trying to do is have the code be accessed by various front ends. At the moment I want it to be accessed from a Web page. Later I would indeed like to port it to a Cell phone. So I thought that if I created an app to 'do the math' I could then get various front ends to access it.

The first thing is to get it to talk to a web page. Can you tell me how I can achieve this?

Many thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64833
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tim Dee:
So I thought that if I created an app to 'do the math' I could then get various front ends to access it.


Close. Again, stop thinking "app" and start thinking "class".

The first thing is to get it to talk to a web page.


Npoe, first thing to do is to create the UI-agnostic class that does the work, Until you do, there's nothing to tie your web UI to.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
Npoe, first thing to do is to create the UI-agnostic class that does the work, Until you do, there's nothing to tie your web UI to.


Here's a UI Agnostic class that happens to have one UI built in; the main method.


This class can be called from the command line but it could also be instanciated from a servlet.




This has the separation that Bear talked about.
The UiAgnosticThing object has no idea that it's being run in a servlet app and doesn't care. It would work just find if called from a Swing app or from its built in main method from the command line.

Likewise, the servlet has no idea how UiAgnosticThing works. It could be making database calls, webservice calls, system calls, etc...
The servlet app doesn't care, it just knows that it can instanicate it and call it's methods.

Help?
[ September 20, 2006: Message edited by: Ben Souther ]
 
Tim Dee
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It all seems a lot more complex than I imagined.
Thanks for the help and demos. I will have to take some time to digest this and get my head round it.

Thanks again.
 
Tom Joiner
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure I am going to address your question in the way you intend. But if you were to have a jsp page which had embedded on it a java applet, you could communicate from the jsp page to the java applet in the following manner:

- The jsp page has a variable it wishes to pass to the applet
- When it constructs the web page, it puts the variable into the html. Every applet has "params" which are part of the applet description:


<applet code = AppletName.class name = AppletName width=100 height=100 VIEWASTEXT>
<param name=yourvariable value="008080">
</applet>

The applet then can use this to retrieve information from the web page.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64833
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no need to complicate this with the addition of applets.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic