<form action="${pageContext.request.contextPath}/myservlet" method="post">
<input type="submit" name="button1" value="Button 1" />
<input type="submit" name="button2" value="Button 2" />
<input type="submit" name="button3" value="Button 3" />
</form>
与
@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.function1();
} else if (request.getParameter("button2") != null) {
myClass.function2();
} else if (request.getParameter("button3") != null) {
myClass.function3();
} else {
// ???
}
request.getRequestDispatcher("/WEB-INF/some-result.jsp").forward(request, response);
}
}
我没有像本例中那样获得所需的输出。该方法通过servlet独立运行,但这些if else条件不起作用。
答案 0 :(得分:0)
您可以在此处实现嵌套表单或多个单独的表单。