我使用带有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");
}
}
}
请帮我解决这个问题!
我为因英语不完美而道歉。
答案 0 :(得分:2)
您应该访问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