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