【问题标题】:Error HTTP Status 404. The requested resource is not available. [Netbeans]错误 HTTP 状态 404。请求的资源不可用。 [网豆]
【发布时间】:2016-10-10 00:53:03
【问题描述】:

我使用带有 Tomcat 8.0.37 的 netbeans 进行华氏到摄氏转换项目 当我尝试运行项目时,我遇到了 HTTP Satus 404 问题。

我的 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>

我的 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>

我的 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");           
            }       
    }
}

请帮我解决这个问题!

抱歉,我的英语单词不够完美。

【问题讨论】:

标签: java jakarta-ee tomcat8 netbeans-8


【解决方案1】:

您应该在 localhost:8080/contextRoot/index.html 访问 index.html。与表单关联的操作应该映射到 servlet,所以它应该是 action="/test"。 web.xml 中的 servlet-class 标记应指定您的 servlet 类全名,例如 mypackage.TestServlet。您可以避免使用 web.xml 并通过在 Servlet 类上使用注释来节省一些时间,正如这里 https://docs.oracle.com/javaee/7/tutorial/servlets004.htm#BNAFU 所解释的那样。也可以在这里寻找一个类似的例子https://stackoverflow.com/a/2395262/6848537

【讨论】:

猜你喜欢
  • 2017-03-23
  • 2012-01-21
  • 2017-12-21
  • 1970-01-01
  • 1970-01-01
  • 2015-05-26
  • 2018-04-13
  • 2011-12-09
  • 1970-01-01
相关资源
最近更新 更多