My Java file is:
public class MyClass {
public void method1() {
// some code
}
public void method2() {
//some code
}
public void method3() {
//some code
}
}
In my JSP page I have three HTML buttons.
If I click on button1
, then only method1
will be called, if I click on button2
then only method2
will execute, and if button3
, then only method3
, and so on.
How can I achieve this?
You can try adding action="#{yourBean.function1}" on each button (changing of course the method function2, function3, or whatever you need). If that does not work, you can try the same with the onclick event.
Anyway, it would be easier to help you if you tell us what kind of buttons are you trying to use, a4j:commandButton or whatever you are using.
<form method="post" action="servletName">
<input type="submit" id="btn1" name="btn1"/>
<input type="submit" id="btn2" name="btn2"/>
</form>
on pressing it request will go to servlet on the servlet page check which button is pressed and then accordingly call the needed method as objectName.method
If you have web.xml then
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>
Java SomeController.java
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Write your code below");
}
Source: Stackoverflow.com