How can I pass arguments from a HTML page to a Javaapplet 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.
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?
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 ]
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.
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.
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 ]
Joined: May 07, 2005
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?
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 ]
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: