0

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);
    }
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Programmer
  • 1,266
  • 5
  • 23
  • 44
  • What exactly is the problem here ? – QuakeCore Jan 24 '16 at 12:05
  • @QuakeCore It's not working. – Programmer Jan 24 '16 at 12:13
  • @QuakeCore The html page you see above has two buttons. Each of the two buttons are supposed to redirect to a different html page and this is done through a servlet, however, that's not working. – Programmer Jan 24 '16 at 12:14
  • I know its not working, your code wont even compile, change `else if(== "ID2"){ ` to `else if( "ID2".equals(name)){ `, to I strongly recommend that you use an IDE. – QuakeCore Jan 24 '16 at 12:22
  • @QuakeCore This is eclipse IDE – Programmer Jan 24 '16 at 12:23
  • Ok then can you explain why your code contains semantic errors ? ( `else if(== "ID2"){ ` ) – QuakeCore Jan 24 '16 at 12:25
  • @QuakeCore My apologies, I must have accidentally deleted that, but anyways, the entire logic seems flawed here. How can I simply redirect from a html page to another html page based on which button was pressed? – Programmer Jan 24 '16 at 12:27

2 Answers2

0
  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.

nzeshka
  • 9
  • 2
0
 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);
        }
Programmer
  • 1,266
  • 5
  • 23
  • 44