Servlet handling multiple post requests

前端 未结 2 468
轻奢々
轻奢々 2020-12-31 13:41

I have one Servlet name EditEvent and one JSP which contains two forms. One for adding new event, The other one is for removing an event.

Is it considered as good p

2条回答
  •  一个人的身影
    2020-12-31 14:13

    For handling multiple requests by same servlet you have to make a contract to have a request parameter like 'ACTION'. Then in your forms add this as hidden field with values like 'ADD' and 'REMOVE'. So, in doPost() you can check this parameter value and can invoke respective handling methods in same servlet.

    class YourServlet extends HttpServlet{
    
          public void doPost(HttpReq req, HttpResp resp){
                   String action = reg.getParameter('ACTION');
                   if('ADD'.equals(action)){
                       addEvent();
                   }
                   if('REMOVE'.equals(action)){
                       removeEvent()
                   } else {
                       defaultAction();
                   }
          }
    
    }
    

提交回复
热议问题