Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9255055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:37:35+00:00 2026-06-18T11:37:35+00:00

My Java file is: public class MyClass { public void method1() { // some

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T11:37:36+00:00Added an answer on June 18, 2026 at 11:37 am

    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:

    • Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
    • How do I pass current item to Java method by clicking a hyperlink or button in JSP page?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class InversionCounter { public static void main(String[]
For example I have a file name call A.java. Here its structure: public class
Right now, I have my code looking like this: SqlTables.java: // Separate file public
Is it by spec that, given a MyClass.java file containing package com.mycorp.foo; public class
following is the code listed in MainClass.java. public class MainClass { public static void
If i have my class as import java.io.File; import java.io.FileOutputStream; //Extends Activity public class
Unsure about static variables. import java.io.File; public class Logger { public static final File
I have a java file Test.java (below) which uses Guava's HashMultiMap (downloaded from http://code.google.com/p/guava-libraries/
import java.util.*; import java.io.*; public class LootGenerator{ public static void main (String[] args) throws
This is my java file : import java.io.File; import java.lang.String; public class ListFiles {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.