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

  • Home
  • SEARCH
  • 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 8353219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:11:37+00:00 2026-06-09T09:11:37+00:00

First, here is the code: <form action=FirstServlet method=Post> Last Name: <input type=text name=lastName size=20>

  • 0

First, here is the code:

<form action="FirstServlet" method="Post">
    Last Name: <input type="text" name="lastName" size="20">
    <br><br>
    <input type="submit" value="FirstServlet">
    <input type="submit"value="SecondServlet">
</form>

I’d like to understand how to send information in case the FirstServlet button was pressed to FirstServlet and in case the SecondServlet button was pressed to SecondServlet.

important:
I want to do it in the same form so the same information would be transered to both the servlets. (of course, in the servlets I’ll use the info accordingly)

  • 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-09T09:11:38+00:00Added an answer on June 9, 2026 at 9:11 am

    There are several ways to achieve this.

    Probably the easiest would be to use JavaScript to change the form’s action.

    <input type="submit" value="SecondServlet" onclick="form.action='SecondServlet';">
    

    But this of course won’t work when the enduser has JS disabled (mobile browsers, screenreaders, etc).


    Another way is to put the second button in a different form, which may or may not be what you need, depending on the concrete functional requirement, which is not clear from the question at all.

    <form action="FirstServlet" method="Post">
        Last Name: <input type="text" name="lastName" size="20">
        <br><br>
        <input type="submit" value="FirstServlet">
    </form>
    <form action="SecondServlet" method="Post">
        <input type="submit"value="SecondServlet">
    </form>
    

    Note that a form would on submit only send the input data contained in the very same form, not in the other form.


    Again another way is to just create another single entry point servlet which delegates further to the right servlets (or preferably, the right business actions) depending on the button pressed (which is by itself available as a request parameter by its name):

    <form action="MainServlet" method="Post">
        Last Name: <input type="text" name="lastName" size="20">
        <br><br>
        <input type="submit" name="action" value="FirstServlet">
        <input type="submit" name="action" value="SecondServlet">
    </form>
    

    with the following in MainServlet

    String action = request.getParameter("action");
    
    if ("FirstServlet".equals(action)) {
        // Invoke FirstServlet's job here.
    } else if ("SecondServlet".equals(action)) {
        // Invoke SecondServlet's job here.
    }
    

    This is only not very i18n/maintenance friendly. What if you need to show buttons in a different language or change the button values while forgetting to take the servlet code into account?


    A slight change is to give the buttons its own fixed and unique name, so that its presence as request parameter could be checked instead of its value which would be sensitive to i18n/maintenance:

    <form action="MainServlet" method="Post">
        Last Name: <input type="text" name="lastName" size="20">
        <br><br>
        <input type="submit" name="first" value="FirstServlet">
        <input type="submit" name="second" value="SecondServlet">
    </form>
    

    with the following in MainServlet

    if (request.getParameter("first") != null) {
        // Invoke FirstServlet's job here.
    } else if (request.getParameter("second") != null) {
        // Invoke SecondServlet's job here.
    }
    

    Last way would be to just use a MVC framework like JSF so that you can directly bind javabean methods to buttons, but that would require drastic changes to your existing code.

    <h:form>
        Last Name: <h:inputText value="#{bean.lastName}" size="20" />
        <br/><br/>
        <h:commandButton value="First" action="#{bean.first}" />
        <h:commandButton value="Second" action="#{bean.Second}" />
    </h:form>
    

    with just the following javabean instead of a servlet

    @ManagedBean
    @RequestScoped
    public class Bean {
    
        private String lastName; // +getter+setter
    
        public void first() {
            // Invoke original FirstServlet's job here.
        }
    
        public void second() {
            // Invoke original SecondServlet's job here.
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the deal I have HTML code like this <form action=/test.php> First name: <input
I have a simple situation here. lets face html code first => <form name=geoKey
Please refer to code. <%= form_tag(:action => create_user, :method => post) do%> <p><label for=first_name>First
i have two dropdown list. first drop down:1 enter code here <form:select path=custName id=custName>
first post here, and probably an easy one. I've got the code from Processing's
Here is the code: string str; cin>>str; cout<<first input:<<str<<endl; getline(cin, str); cout<<line input:<<str<<endl; The
I have a basic question about the form action. Consider the below code here,
First off here is the code! <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Strict//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd>
So, first off here's my code to open the dotx and create a new
my first question here! I have a problem with a piece of code that

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.