我的Java文件是:
public class MyClass {
public void method1() {
// some code
}
public void method2() {
//some code
}
public void method3() {
//some code
}
}
在我的JSP页面中,我有三个HTML按钮。
如果单击button1
,则仅会调用method1
;如果单击button2
,则仅会执行method2
;如果单击button3
,则只会执行method3
,依此类推。
我该如何实现?
您只需要确保按钮输入具有type="submit"
和<input type="submit">
中的<button type="submit">
和
不是 type="button"
,它仅为onclick
内容和全部。
<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>
with
@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 type="submit">
而不是<input type="submit">
,然后可以给它们取相同的名称,但使用唯一的值。<button>
的值将不会用作标签,您只需指定自己为孩子即可。例如
<form action="${pageContext.request.contextPath}/myservlet" method="post"> <button type="submit" name="button" value="button1">Button 1</button> <button type="submit" name="button" value="button2">Button 2</button> <button type="submit" name="button" value="button3">Button 3</button> </form>
with
@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); } }
另请参见:
<form method="post" action="servletName">
<input type="submit" id="btn1" name="btn1"/>
<input type="submit" id="btn2" name="btn2"/>
</form>
在按下请求时,将转到servlet页面上的servlet,检查按下了哪个按钮,然后相应地将所需的方法作为objectName.method进行调用
无论如何,如果您告诉我们您要使用哪种按钮,a4j:commandButton或您正在使用的任何按钮,会更容易为您提供帮助。
HTML / JSP
<form action="${pageContext.request.contextPath}/myservlet" method="post">
<input type="submit" name="button1" value="Button 1" />
</form>
web.xml
<servlet>
<display-name>Servlet Name</display-name>
<servlet-name>myservlet</servlet-name>
<servlet-class>package.SomeController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myservlet</servlet-name>
<url-pattern>/myservlet</url-pattern>
</servlet-mapping>
SomeController.javaJava
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Write your code below");
}