我有404问题,但未找到错误,但我找不到问题所在。这是我的index.html代码
<html>
<form name='f1' action='Add' method='get'>
First No. <input type='text' name='t1'><br>
Second No. <input type='text' name='t2'><br>
<input type='submit' name='submit' value='submit'>
</form>
</html>
和我的add.java Servlet页面:
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class Add extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException,
ServletException {
PrintWriter pr = res.getWriter();
res.setContentType("text/html");
try {
int x = Integer.parseInt(req.getParameter("t1"));
int y = Integer.parseInt(req.getParameter("t2"));
pr.println("The addition of numbers is " + (x + y));
} catch (Exception e) {
pr.println("Invalid Input");
}
}
}
用@WebServlet("/Add")
注释您的班级以匹配您的网址。然后将doGet
方法更改为doPost
,并将html格式的方法更改为post
。