慕容4345310
只需给各个按钮元素一个唯一的名称即可。按下按钮后,按钮的名称可用作请求参数,就像输入元素一样。您只需要确保按钮输入的type="submit"in 和and not一样 type="button",这仅呈现一个“死”按钮,仅用于onclick填充所有内容。例如 与@WebServlet("/myservlet")public class MyServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { MyClass myClass = new MyClass(); if (request.getParameter("button1") != null) { myClass.method1(); } else if (request.getParameter("button2") != null) { myClass.method2(); } else if (request.getParameter("button3") != null) { myClass.method3(); } else { // ??? } request.getRequestDispatcher("/WEB-INF/some-result.jsp").forward(request, response); }}另外,也可以使用代替,然后为它们赋予相同的名称,但使用唯一的值。的值将不会用作标签,您只需将自己指定为孩子即可。例如 Button 1 Button 2 Button 3与@WebServlet("/myservlet")public class MyServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { MyClass myClass = new MyClass(); String button = request.getParameter("button"); if ("button1".equals(button)) { myClass.method1(); } else if ("button2".equals(button)) { myClass.method2(); } else if ("button3".equals(button)) { myClass.method3(); } else { // ??? } request.getRequestDispatcher("/WEB-INF/some-result.jsp").forward(request, response); }}