httpservlet的service()、doget()、dopost方法

HttpServlet里的三个方法:service(HttpServletRequest req, HttpServletResponse resp) ,doGet(HttpServletRequest req, HttpServletResponse resp), doPost(HttpServletRequest req, HttpServletResponse res)的区别和联系:

  1. 在servlet中默认情况下,无论你是get还是post 提交过来 都会经过service()方法来处理,然后转向到doGet
  2. 或是doPost方法,我们可以查看HttpServlet 类的service方法:  我在tomcat的lib目录下,解压servlet-api.jar,然后用反编译软件把lib\javax\servlet\http下的HttpServlet.class反编译,看里头的service()方法的原代码:查看源码发现,httpservlet继承genericservlet并实现了service方法,从实现来看service起到了调度的作用。因此自定义servlet继承httpservlet不需要覆盖重写service方法,只需覆盖重写doget或者dopost方法。
  3. 注意,sun只是定义了servlet接口,而实现servlet接口的就是类似于tomcat的服务器,所以我是在tomcat的安装目录下找到实现的类。

public abstract class HttpServlet extends GenericServlet {

protected void service(HttpServletRequest req, HttpServletResponse resp)

        throws ServletException, IOException
    {
        String method = req.getMethod();
        if(method.equals("GET"))
        {
            long lastModified = getLastModified(req);
            if(lastModified == -1L)
            {
                doGet(req, resp);
            } else
            {
                long ifModifiedSince = req.getDateHeader("If-Modified-Since");
                if(ifModifiedSince < (lastModified / 1000L) * 1000L)
                {
                    maybeSetLastModified(resp, lastModified);
                    doGet(req, resp);
                } else
                {
                    resp.setStatus(304);
                }
            }
        } else
        if(method.equals("HEAD"))
        {
            long lastModified = getLastModified(req);
            maybeSetLastModified(resp, lastModified);
            doHead(req, resp);
        } else
        if(method.equals("POST"))
            doPost(req, resp);
        else
        if(method.equals("PUT"))
            doPut(req, resp);
        else
        if(method.equals("DELETE"))
            doDelete(req, resp);
        else
        if(method.equals("OPTIONS"))
            doOptions(req, resp);
        else
        if(method.equals("TRACE"))
        {
            doTrace(req, resp);
        } else
        {
            String errMsg = lStrings.getString("http.method_not_implemented");
            Object errArgs[] = new Object[1];
            errArgs[0] = method;
            errMsg = MessageFormat.format(errMsg, errArgs);
            resp.sendError(501, errMsg);
        }
    }}
  1. 从上面可以看出 这里的service是用来转向的,但是如果你在自己的servlet类中覆盖了service方法,比如说你的service是这样的:
  2. Java代码
  3. 1.publicvoid service(ServletRequest req, ServletResponse res)
  4. 2.                   throws ServletException, IOException {
  5. 3.          res.getOutputStream().print(
  6. 4.         "image is <img src='images/downcoin.gif'></img><br>");
  7. 5.      }
    1. 那么这时service就不是用来转向的,而是用来处理业务的,现在不论你的客户端是用pos还是get来请求此servlet
    2. 都会执行service方法也只能执行servlet方法,不会去执行doPost或是doGet方法。
    3. 比如说:你的客户端代码是:
    4. Java代码
    5. 1. <%@page contentType="text/html; charset=utf-8"%>
    6. 2. <html>
    7. 3. <head><title>选择</title></head>
    8. 4. <body>
    9. 5. 请选择你喜欢的水果:<br>
    10. 6. <form action ="Test" method = "post">
    11. 7. <input type="checkbox" name="fruit" value ="apple" >苹果<br>
    12. 8. <input type="checkbox" name="fruit" value ="orange">桔子<br>
    13. 9. <input type="checkbox" name="fruit" value ="mango">芒果<br>
    14. 10. <input type="submit" value="提交">
    15. 11. </form>
    16. 12. </body>
    17. 13. </html>
    18. 14.
    19. 15. 服务端servlet是:Test类
    20. 16.
    21. 17.import java.io.IOException;
    22. 18.
    23. 19.import javax.servlet.ServletException;
    24. 20.import javax.servlet.ServletOutputStream;
    25. 21.import javax.servlet.ServletRequest;
    26. 22.import javax.servlet.ServletResponse;
    27. 23.import javax.servlet.http.HttpServlet;
    28. 24.import javax.servlet.http.HttpServletRequest;
    29. 25.import javax.servlet.http.HttpServletResponse;
    30. 26.
    31. 27./**
    32. 28. * 演示service方法
    33. 29. */
    34. 30.publicclass Testextends HttpServlet {
    35. 31.
    36. 32.publicvoid service(ServletRequest req, ServletResponse res)
    37. 33.   throws ServletException, IOException {
    38. 34.             res.getOutputStream().print("This is the service");
    39. 35.
    40. 36. }
    41. 37.
    42. 38.protectedvoid doGet(HttpServletRequest request,
    43. 39.     HttpServletResponse response)throws ServletException, IOException {
    44. 40.    doPost(request,response);
    45. 41.
    46. 42. }
    47. 43.protectedvoid doPost(HttpServletRequest request,
    48. 44.     HttpServletResponse response)throws ServletException, IOException {
    49. 45.    ServletOutputStream out=response.getOutputStream();
    50. 46.    String[] args=(String[])request.getParameterValues("fruit");
    51. 47.  for(int i=0;i<args.length;i++){
    52. 48.     out.print(args[i]+"<br>");
    53. 49.    }
    54. 50.
    55. 51. }
    56. 52. }
    57. 点击提交后:页面输出结果为“This is the service“;
上一篇:Windows 10 RTM 官方正式版


下一篇:DedeCms文档关键词替换,优先替换长尾关键词