'How to redirect html button -> Servlet -> another html page?

I have a html page with 2 buttons with the same form action (When the user presses a button, the form redirects it to a servlet and then in that servlet, I want it to redirect to another html page based on the button pressed in the html page).

Html page

<form action ="ManageEmployeeRedirect" method = "post"> 
<input type="submit" value="Create New Employee Account" name="ID1"> 
<br>
<br>
<input type="submit" value="Update Existing Employee Account" name="ID2"> 
</form>
</div>
</body>
</html>

Servlet

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class ManageEmployeeRedirect extends HttpServlet { 

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        String name = request.getParameter("ID1");
        String name2 = request.getParameter("ID2"); 

        if("ID1".equals(name)){
            RequestDispatcher rs = request.getRequestDispatcher("index.html");
            rs.forward(request, response);
        }
        else if("ID2".equals(name2)){ 
            RequestDispatcher rs = request.getRequestDispatcher("changePassAdmin.html"); 
            rs.forward(request, response);
        }
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
               processRequest(request, response);
          }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
               processRequest(request, response);
    }
}


Solution 1:[1]

  1. In second "if" you missed "name" and I advise you to use "ID1".equals(name) format.
  2. Check that your form method correspond to servlet's method doPost().

Advice: In java you can use JSP technology. It's extended html format, which contain implict java-servlet inside.

Solution 2:[2]

 String button1 = request.getParameter("ID1");
        String button2 = request.getParameter("ID2"); 

        if(button1 != null) {
            RequestDispatcher rs = request.getRequestDispatcher("index.html"); 
            rs.forward(request, response);
        }
        else if(button2 != null) { 
            RequestDispatcher rs = request.getRequestDispatcher("changePassAdmin.html"); 
            rs.forward(request, response);
        }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 nzeshka
Solution 2 javaprogrammer