Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / January 2006

Tip: Looking for answers? Try searching our database.

Change to a form display

Thread view: 
Cardinal - 27 Jan 2006 20:33 GMT
I have a small JSP 2.0 application that sends information to a class
file, gets it back and displays it in a web page. The code is below.

Kilowatt.class
--------------------
package electric;

public class Kilowatt
{
    private double costKiloWattHours;  //example 8.42 which means cents
    private double hoursUsedPerYear;   //example 653
    private double annualCost;

    public void setCost(double cost)
    {
        costKiloWattHours = cost / 100;
    }

    public void setHours(double hours)
    {
        hoursUsedPerYear = hours;
    }

    public double getAnnualCost()
    {
        annualCost = costKiloWattHours * hoursUsedPerYear;
        return annualCost;
    }
}

jmElectric.jsp
--------------------

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
    <title>electric page</title>
</head>

<body>
<jsp:useBean id="myKilo" scope="page" class="electric.Kilowatt"/>

<jsp:setProperty name="myKilo" property="cost" value="8.42"/>
<jsp:setProperty name="myKilo" property="hours" value="653"/>

The annual cost is: <fmt:formatNumber value="${myKilo.annualCost}"
type="currency"/>

</body>
</html>

As you can see, I use the setProperty tag to send the values to the
class file. My question is how can I turn this into a form so the user
can enter the values? I've tried researching this but haven't been able
to come up with an example. Thanks very much.
dmcquay@gmail.com - 27 Jan 2006 22:45 GMT
I am not familiar with JSP specifically, but I am with PHP and some
others and I think the main idea should be the same.

I believe the JSP stuff is always processed by the server before
sending the result to the user for display.  So the only way to tell
the server those properties is to send them with the request for the
page.  This could be sent in the url (called a query string) or it
could be sent by an HTML form.  Let me know if you need further
explanation on any of that.  I don't know how to process the
information from there, but I know it can be done in JSP and that's
probably what you are looking for.

Another simple option would be to just do this with javascript.
Example:

**** CODE ****

<html>
<head>
<title>Annual Cost</title>
<script language="javascript">
 function calcCost()
 {
   var cost = document.getElementById("cost").value;
   var hours = document.getElementById("hours").value;
   var result = cost * hours;
   document.getElementById("result").value = result;
 }
</script>
</head>
<body>
Cost: <input type="text" id="cost" /><br />
<input type="text" id="hours" /><br />
Annual Cost: <span id="result"></span><br />
<hr />
<button onclick="javascript:calcCost()">Calculate</button>
</body>
</html>

**** END CODE ****

That is just a bad looking rough outline without currency formatting,
but you get the idea.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.