30

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?

Braiam
  • 1
  • 11
  • 47
  • 78
Sarde
  • 658
  • 1
  • 8
  • 19
  • 3
    You submit you request to a Servlet and conditionally call the required function from the servlet. – Ravindra Gullapalli Feb 06 '13 at 07:36
  • 1
    You cannot do it directly, as JavaScript is ran at client-side and JSP at server-side, so you need to submit your request to the servlet and the servlet will do processing and returns the result. You may use AJAX to do it asynchronously – asifsid88 Feb 06 '13 at 07:38
  • Is it training task? Do you plan to use web frameworks? – Taky Feb 06 '13 at 07:42
  • Can you do this testing just from an internet page on a home computer without the hassle of having to use a server. Could I have a page made on my desktop and then just use that to call a method in a class that will call a java method? – Doug Hauf Feb 12 '14 at 22:42

4 Answers4

53

Just give the individual button elements a unique name. When pressed, the button's name is available as a request parameter the usual way like as with input elements.

You only need to make sure that the button inputs have type="submit" as in <input type="submit"> and <button type="submit"> and not type="button", which only renders a "dead" button purely for onclick stuff and all.

E.g.

<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);
    }

}

Alternatively, use <button type="submit"> instead of <input type="submit">, then you can give them all the same name, but an unique value. The value of the <button> won't be used as label, you can just specify that yourself as child.

E.g.

<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);
    }

}

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
3
<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

Meherzad
  • 8,433
  • 1
  • 30
  • 40
  • I have used Java a lot in the past six months, so I am fairly familar with the code. I however have not had to develop any pages that use the java class to call a button. Can I just create class with public methods and then have my web page call each method individual with JQuery or some other intermediate language? – Doug Hauf Feb 12 '14 at 22:45
  • @DougHauf you can use servlet for this. Servlet post and get method can be used by javascript. This post will give you rough idea on how it works http://www.doublecloud.org/2012/12/developing-web-application-with-jquery-and-java-servlet/ – Meherzad Feb 13 '14 at 03:10
3

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.

0

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");
}
LKG
  • 41
  • 3