覆盖HttpServlet服务方法 [英] Overriding HttpServlet service method

查看:133
本文介绍了覆盖HttpServlet服务方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的servlet:

I have a servlet which looks like :

public class MyServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
             throws IOException, ServletException {
    doTheJob(request, response);
}//method doGet

public void doPost(HttpServletRequest request, HttpServletResponse response)
              throws IOException, ServletException {
    doTheJob(request, response);
}//method doPost

private void doTheJob(.....) {
    ...........................
}

}

由于我的应用程序的工作方式,我只需要从 doGet() doPost()调用 doTheJob()。所以我想,我最好覆盖 HttpServlet 的方法 service()

Because of the way my application works, I only need to call doTheJob() both from doGet() and from doPost(). So I think, I better override the method service() of the HttpServlet.

但我想知道是否会制造任何东西或导致任何问题。

But I would like to know if that will brake anything or will cause any issues.

推荐答案

这是 service()通常是实现的(非常简化):

This is how service() is typically implemented (very simplified):

protected void service(HttpServletRequest req, HttpServletResponse resp) {
    String method = req.getMethod();

    if (method.equals(METHOD_GET)) {
            doGet(req, resp);
    } else if (method.equals(METHOD_HEAD)) {
        doHead(req, resp);
    } else if (method.equals(METHOD_POST)) {
        doPost(req, resp);

    } else if (method.equals(METHOD_PUT)) {
        doPut(req, resp);   

    } else if (method.equals(METHOD_DELETE)) {
        doDelete(req, resp);

    } else if (method.equals(METHOD_OPTIONS)) {
        doOptions(req,resp);

    } else if (method.equals(METHOD_TRACE)) {
        doTrace(req,resp);

    } else {
        resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg);
    }
}

正如您所见,它几乎不会委托给 doGet() doPost()取决于HTTP方法。所以一方面用 service()替换 doGet() doPost() code>没问题。另一方面,你的servlet也将处理 PUT DELETE HEAD 和其他方法,而单独的 doGet() doPost()它将返回 405方法不允许

As you can see it barely delegates to doGet() and doPost() depending on HTTP method. So from one hand replacing doGet() and doPost() with service() is fine. On the other hand your servlet will also handle PUT, DELETE, HEAD and other methods while with separate doGet() and doPost() it will return 405 Method not allowed.

这就是为什么我实际上会建议单独的 doGet() doPost()委托你的代码让servlet处理其他方法。如果这是代码中的重复模式,请考虑以下帮助程序servlet:

That's why I would actually advice separate doGet() and doPost() delegating to your code and let servlet handle other methods. If this is a recurring pattern in your code, consider the following helper servlet:

public class AbstractServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
                 throws IOException, ServletException {
        doGetOrPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
                  throws IOException, ServletException {
        doGetOrPost(request, response);
    }

    abstract protected void doGetOrPost(.....);

}

这篇关于覆盖HttpServlet服务方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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