aspose file tools*
The moose likes JSP and the fly likes Passing values to and from HTML Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Passing values to and from HTML" Watch "Passing values to and from HTML" New topic
Author

Passing values to and from HTML

Tim Dee
Greenhorn

Joined: May 07, 2005
Posts: 22
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

Joined: Jul 08, 2003
Posts: 24185
    
  34

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?


[Jess in Action][AskingGoodQuestions]
Matthew Egbers
Greenhorn

Joined: Sep 20, 2006
Posts: 7
well tim, i would do it with some java script like this


[ September 20, 2006: Message edited by: Bear Bibeault ]
Matthew Egbers
Greenhorn

Joined: Sep 20, 2006
Posts: 7
also, you must change the onglick to "on click" with no space. It wouldnt let me post with 0nclick
Tim Dee
Greenhorn

Joined: May 07, 2005
Posts: 22
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

Joined: Jan 10, 2002
Posts: 61232
    
  66

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 ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Tim Dee
Greenhorn

Joined: May 07, 2005
Posts: 22
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

Joined: Jan 10, 2002
Posts: 61232
    
  66

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

Joined: May 07, 2005
Posts: 22
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

Joined: Jan 10, 2002
Posts: 61232
    
  66

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

Joined: Dec 11, 2004
Posts: 13410

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 ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Tim Dee
Greenhorn

Joined: May 07, 2005
Posts: 22
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

Joined: Sep 19, 2006
Posts: 47
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.


SCJP
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61232
    
  66

There is no need to complicate this with the addition of applets.
 
jQuery in Action, 2nd edition
 
subject: Passing values to and from HTML