Servlet处理多个帖子请求 [英] Servlet handling multiple post requests

查看:105
本文介绍了Servlet处理多个帖子请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Servlet名称EditEvent和一个包含两个表单的JSP。
一个用于添加新事件,另一个用于删除事件。

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.

使用两个单独的servlet来处理一个JSP被认为是一种好习惯?
如果没有,你将如何处理来自一个servlet的两个post请求?即添加事件和删除事件请求。

Is it considered as good practice to be using two separate servlets to handle one JSP? If not, how would you handle two post requests from one servlet? i.e The Add event and remove event request.

欢呼

推荐答案

为了处理同一个servlet的多个请求,你必须签订一个像'ACTION'这样的请求参数的契约。然后在表单中将其添加为隐藏字段,其值为ADD和REMOVE。因此,在doPost()中,您可以检查此参数值,并可以在同一个servlet中调用相应的处理方法。

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

}

这篇关于Servlet处理多个帖子请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆