运行servlet的问题似乎一切都很好

问题描述 投票:0回答:1

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

  }
}
java servlets
1个回答
0
投票

@WebServlet("/Add")注释您的班级以匹配您的网址。然后将doGet方法更改为doPost,并将html格式的方法更改为post

请参见https://docs.oracle.com/javaee/6/tutorial/doc/bnafu.html

© www.soinside.com 2019 - 2024. All rights reserved.