Using a User Defined Method to Convert Temperatures

 

Introduction.  I think it's important to work more with developing user defined methods in JSP pages.  They aren't all that difficult to do.  But they can be convenient for all the same reasons developing your own methods are when developing applications and applets.  For example,
  • code reusability
  • debugging code segments
  • developing functioning parts of classes

Personally, I think developing user interfaces is usually easier in HTML than in Java.  So I find I have tendencies towards developing things in JSP rather than more basic Java.

So, the next series of web pages will survey a variety of methods.  We will also work with some error handling and importing pre-built packages so that you get more experience with these sorts of things.  Almost all of the code should seem very familiar.

You should call this JSP page sticky_temperature.jsp.

 

 

<html>
<head>
<title>A Sticky Form for Converting Temperatures</title>
</head>
<body bgcolor = "003344" text="cccccc">
<%
String str_degrees_fahrenheit = request.getParameter("txt_degrees_fahrenheit");
if (str_degrees_fahrenheit == null) str_degrees_fahrenheit = "";

String str_degrees_celsius = request.getParameter("txt_degrees_celsius");
if (str_degrees_celsius == null) str_degrees_celsius = "";

// checking to see if this form has been submitted
if (request.getMethod().equals("POST"))
{

// establishing a boolean variable to denote whether required inputs are present
boolean tempFahrenheitPresent = true;
String skipped_input = "";
// checking to ensure the temperature to be converted has sbeen entered
if (str_degrees_fahrenheit.length() == 0)
{

skipped_input = "<font size = 4>You forgot to enter a temperature in fahrenheit!<br></font>";
tempFahrenheitPresent = false;
str_degrees_celsius = "";

}


// if all of the required fields haven't been entered
// inform the user
if (!tempFahrenheitPresent)
{

out.println(skipped_input);

}
else
{

str_degrees_celsius = convertToCelsius(str_degrees_fahrenheit);

}

}
%>
<form action="sticky_temperature.jsp" method = "POST">
<table>
<tr>
<td><font size=4>Degrees Fahrenheit:</font>
</td>
<td><input type="text" name="txt_degrees_fahrenheit" value="<%=str_degrees_fahrenheit%>">
</td>
</tr>
<tr>
<td><font size=4>Calculate Conversion:</font>
</td>
<td><input type="submit" name="cmd_convert" value="Convert!">
</td>
</tr>
<tr>
<td><font size=4>Degrees Celsius:</font>
</td>
<td><input type="text" name="txt_degrees_celsius" value="<%=str_degrees_celsius%>">
</td>
</tr>
</table>
</form>
</body>
</html>
<%!
// a user defined method
String convertToCelsius(String str_tempf)
{

String str_tempc;
double degrees_fahrenheit = Double.parseDouble(str_tempf);
double degrees_celsius = (degrees_fahrenheit - 32.0) * 5.0 / 9.0;
return str_tempc = Double.toString(degrees_celsius);

}
%>

 

After copying this code and uploading it and accessing it on your web you should see something like the following after entering the initial input and submitting the form for computation.

 

 

Notice how the decimal places run wild.  We will improve on this with a second method based on importing the text.DecimalFormat classes in the next web page.