Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
543 views
in Technique[技术] by (71.8m points)

jakarta ee - Overriding HttpServlet service method

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(.....) {
    ...........................
}

}

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.

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

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...