I have several forms that give the data to the doPost and I need to determine within which doPost from which form the data came from.

 // тут id <form method="POST" action="UserServlet"> <input type="number" name="id"><br> <input type="submit" value="submit"> </form> // а тут name <form method="POST" action="UserServlet"> <input type="text" name="name"><br> <input type="submit" value="submit"> </form> @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.?????() //Вот тут как-то определить от какой формы пришло } 

How can I find out which form the data came from if there are several of them in one jsp .

And if the forms on different jsp but use one doPost ?

  • one
    file a hidden field with the form identifier in the form if you want to track - Mikhail Rebrov
  • one
    Why not use multiple servlets if the forms are on different jsp? - Alex78191
  • one
    You can send in different ways of the form. - Alex78191
  • one
    By the way, yes ... you have different fields in the forms .. they mean different things, right? - Mikhail Rebrov
  • 2
    @Pavel can attach a servlet to all url starting with a specific line at stackoverflow.com/questions/12972914/wildcard-path-for-servlet - Alex78191

1 answer 1

  1. Add a hidden field to the form <input type="hidden" name="command" value="addUser"/>

  2. Extract command in controller and perform any actions depending on its value.

     @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } private void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String nameCommand = request.getParameter("command"); } 
  • one
    You can use the submit buttons to assign different values. And thus do without extra fields. - Sergey
  • one
    Or notice that one form has a name field, and the other does not. But the other one has an id, but the first one does not. Thus, it is also possible to guess which form entered the server - Sergey
  • @Sergey and how do I specify when I call request.getParameter("value"); that I need the value from the button on the form, on the page there can still be all sorts of value attributes appearing in different elements ... How can I highlight this value ? - Pavel
  • 2
    @Pavel to start to learn how to perform HTTP requests. - Alex78191
  • one
    @Pavel is correct, these are request parameters - Alex78191