0

I do a project Fahrenheit to Celsius conversion using netbeans with Tomcat 8.0.37 when i try to run project, i get a issue HTTP Satus 404.

My index.html

<html>
<head>
</head>

<body>

<h3>Please enter Fahrenheit temperature:</h3><p>

<form action="/conv/test"> 
Temperature(F) : <input type="text" name="temperature"><br><br>
<input type="submit" value="Submit">
</form>

</body>
</html>

My web.xml

<web-app>

  <servlet>
    <servlet-name>testServlet</servlet-name>
    <servlet-class>doGetMethod.TestServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>testServlet</servlet-name>
    <url-pattern>/test</url-pattern>
  </servlet-mapping>

</web-app>

My TestServlet.java

public class TestServlet extends HttpServlet 
{
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws javax.servlet.ServletException, java.io.IOException 
    {       
        String temperature = req.getParameter("temperature");
            DecimalFormat twoDigits = new DecimalFormat("0.00");

            try 
            {
              double tempF = Double.parseDouble(temperature);
              String tempC = twoDigits.format((tempF -32)*5.0/9.0);

              PrintWriter out = res.getWriter();
          out.println("<html>");
          out.println("<head>");
          out.println("</head>");
          out.println("<body>");
          out.println("<h3>" + temperature + " Fahrenheit is 
                 converted to " + tempC + " Celsius</h3><p>");              
          out.println("</body>");
          out.println("</html>");
            }
            catch(Exception e)
            {   

              res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
              "There was an input error");           
            }       
    }
}

Please help me to solve this issue !

I apologize for not being word-perfect in English.

hyphens2
  • 166
  • 6
  • 15

1 Answers1

2

You should access the index.html at localhost:8080/contextRoot/index.html. The action associated to the form should map to the servlet, so it should be action="/test". The servlet-class tag in the web.xml should specify your servlet class full name such as mypackage.TestServlet. You could avoid using a web.xml and save you some time by using annotation on the Servlet class as well explained here https://docs.oracle.com/javaee/7/tutorial/servlets004.htm#BNAFU. Also look here for a similar example https://stackoverflow.com/a/2395262/6848537

Community
  • 1
  • 1
chess4ever
  • 134
  • 11
  • 1
    I have also found an example where the action maps to the url without a slash: https://docs.oracle.com/javaee/7/tutorial/servlets016.htm – chess4ever Oct 09 '16 at 16:56