0

I've searched for a good 2 hours now but obviously I'm not searching in the right place. I am trying to create a temperature converter calculator.It is three pages

  1. An html form that contains a form to input a temperature number and 2 radio buttons to decide if you want Celsius or Fahrenheit.

  2. A jsp page that takes the value of that form and does the math to convert the temperature.

  3. A jsp page that throws an error if the user enters invalid characters in the form.

I am having difficulty on making my jsp page convert the number and redirect the user to another page displaying the result. When I enter a number and press the submit button I am taken to my code for the jsp page. This is an assignment for a class if you are wondering. I am ready to flip tables and need help. My code is below.

Page 1:

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    </head>

    <body>
        <script type="text/javascript">
            function check(){
                document.getElementByValue("fahr").checked=true

            }
            function check2(){
                document.getElementByValue("cel").checked=true
            }

            // -->
        </script>

        <form name="Temperature Converter" method="get" action="convert.jsp">
            <h2 style="font-family:arial;color:red">Temperature Calculator</h2>

            <table border="1">
                <tbody>
                    <tr>
                        <td>Enter a temperature in number form:</td>
                        <td>
                            <input type="text" name="fTemp" id="fTemp"  />
                        </td>
                        <td>
                            <input type="radio" name="temp" value="cel" onclick="check2();return false">Convert to Celsius
                            <br>
                            <input type="radio" name="temp" value="fahr" onclick="check();return false">Convert to Fahrenheit
                        </td>
                    </tr>
                    <tr>
                        <td><input type="submit" value="Convert!">

                        </td>
                    </tr>
                </tbody>
            </table>
        </form>
    </body>
</html>

Page 2: This is where I feel like it's a mess.

<%@page contentType="text/html"%>
<%@page import="java.sql.*" %>
<%@page session="true"%>
<%!
    String inp = null;
    int temperature = 0;
        int c = 0;
        int f = 0;

%> 
<%
    inp = request.getParameter("fTemp");

    if (inp == null || inp.equals("") )
    {
        response.sendRedirect("errorMsg.jsp?msg=You are missing values.");
        return;
    }
    try
    {
     if (request.getParameter("cel") = checked)
                {
                f = Integer.parseInt(inp);
                c = (f - 32) * (5.0 / 9.0);
        }else{
                c = Integer.parseInt(inp);
        c = parseFloat(request.getParameter('inp').value);
                f = (c * (9.0 / 5.0)) + 32.0;

    }
    catch (Exception e)
    {
        response.sendRedirect("errorMsg.jsp?msg=Invalid numbers entered.");
        return;
    }
%>
<!DOCTYPE HTML>
<html>
    <head>
        <title>First jsp Page</title>
    </head>
    <body>
        <p>The temperature is <%= c %></p>
                <p>The temperature is <%= f %></p>
    </body>
</html>

Thank you very much for any advice/direction and I apologize for the long message.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
UltraNewb
  • 3
  • 1
  • 2
  • Why are you wanting to do this with a bare JSP, especially one with the old-style embedded Java code, instead of with a tool like Spring or Play? – chrylis -cautiouslyoptimistic- Oct 12 '13 at 21:25
  • 1
    @chrylis probably for learning purposes. – Luiggi Mendoza Oct 12 '13 at 21:27
  • It is for an assignment. So I am doing it how my teacher showed us. – UltraNewb Oct 12 '13 at 21:28
  • 2
    If you want to learn about JSP development, you should also learn about Servlets. First lesson: [**avoid usage of scriptlets (Java code inside JSP)**](http://stackoverflow.com/q/3177733/1065197). If your teacher things it's good to teach scriptlets usage, tell him/her to visit this site, specially to read the link above :). – Luiggi Mendoza Oct 12 '13 at 21:29
  • You have some syntax errors. – Sotirios Delimanolis Oct 12 '13 at 21:30
  • @SotiriosDelimanolis thank you! Where exactly are the syntax errors? – UltraNewb Oct 12 '13 at 21:45
  • Which web container/server are you using? Since you say the resulting page from the click on the submit button is the code of your JSP page, it seems to me that your problem lies in some mis-configuration/use of the container/server. Syntax errors (of which you have at least two when you check the value of the `cel` parameter) are not the culprit, because if the JSP were actually compiled by the container you would have noticed those. – Giulio Piancastelli Oct 12 '13 at 21:58
  • @GiulioPiancastelli I am using Tomcat 7. If it helps the debugger page following says "Unable to compile class for JSP" I think I'm not importing what I need to, I just don't know what. – UltraNewb Oct 13 '13 at 01:03
  • It does not compile because of the syntax errors. See my answer below. – Alex Theedom Oct 13 '13 at 01:20

1 Answers1

0

I have reviewed your code and there are a few syntax errors in the convert.jsp file.

  1. There are some missing declarations e.g. language="java" and an unnecessary import file <%@page import="java.sql.*"%>
  2. The variables used to store the temperature should be doubles as they are decimals.
  3. The if statement argument did not check correctly the value of the value radio button selected. Use equals() not ==. See this link What's the difference between ".equals" and "=="?
  4. There was a missing closing } after the else
  5. The code c = parseFloat(request.getParameter('inp').value); is not necessary and incorrect. Use " instead of '.

Here is the corrected code.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@page session="true"%>
<%!String inp = null;
double temperature = 0;
double c = 0;
double f = 0;%>
<%
inp = request.getParameter("fTemp");    

if (inp == null || inp.equals("")) {
    response.sendRedirect("errorMsg.jsp?msg=You are missing values.");
    return;
}
try {
    if ("cel".equals(request.getParameter("temp"))) {
        f = Integer.parseInt(inp);
        c = (f - 32) * (5.0 / 9.0);
    } else {
        c = Integer.parseInt(inp);
        f = (c * (9.0 / 5.0)) + 32.0;

    }
} catch (Exception e) {
    response.sendRedirect("errorMsg.jsp?msg=Invalid numbers entered.");
    return;
}
 %>

This will compile and execute as you expect.

Community
  • 1
  • 1
Alex Theedom
  • 1,604
  • 15
  • 16
  • Ack, coding is so frustrating. I understand now though the difference between == and .equals(). Also I later changed my variables to doubles but kept mismatching them to ints and such. Basically I was just doing random stuff. I was so close. Thanks a bunch Theedom, and for the link to see the difference between .equals and ==. I was able to finish and get everything working correctly. Thanks everyone who took the time to read and answer, it was a huge help. – UltraNewb Oct 13 '13 at 02:25
  • Glad to help and I agree coding is very frustrating at times. – Alex Theedom Oct 13 '13 at 02:30