3

I am new to JSP/Servlet.

I want to use one servlet for handling different request sent from different JSP form/button.

For example

student.jsp =>

<form action="SingleController?action=student" method="POST">
      Student ID: <input type="text" name="stid"> 
      Name: <input type="text" name="stname"> 
      <input type="submit" value="Submit" name="student">                 
     </form>

teacher.jsp =>

 <form action="SingleController?action=teacher" method="POST">
     Teacher ID:<input type="text" name="tid"> 
      Name: <input type="text" name="tname"> 
      <input type="submit" value="Submit">                 
     </form>

SingleController

How can I manage these two different requests using a single Servlet ?

Pokechu22
  • 4,790
  • 8
  • 35
  • 59

1 Answers1

2

use if conditions in servlet on action .

String action = request.getParameter("action");

if (action.equals("student")){ 
    // to do rest of code..
}

else if (action.equals("teacher")){ 
    // to do rest of code..
}
Dis Honest
  • 77
  • 7