欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

读源码学Servlet(5)GenericServlet中的service 方法

程序员文章站 2022-05-24 21:03:05
...

观察GenericServlet源码中关于service()方法的实现:

 

//实现父类的service(ServletRequest req,ServletResponse res)方法   
 //通过参数的向下转型,然后调用重载的service(HttpservletRequest,HttpServletResponse)方法   
  
    public void service(ServletRequest req, ServletResponse res)   
        throws ServletException, IOException   
    {   
        HttpServletRequest        request;   
        HttpServletResponse        response;   
           
        try {   
            request = (HttpServletRequest) req;  //向下转型   
            response = (HttpServletResponse) res; //向下转型   
        } catch (ClassCastException e) {   
            throw new ServletException("non-HTTP request or response");   
        }   
        service(request, response);  //调用重载的service(HttpServelertRequest,HttpServletResponse)方法   
    }  
    
    
    //重载的service(HttpServletRequest req, HttpServletResponse resp)方法   
    protected void service(HttpServletRequest req, HttpServletResponse resp)   
        throws ServletException, IOException   
    {   
        String method = req.getMethod();   
  
        if (method.equals(METHOD_GET)) {   
            long lastModified = getLastModified(req);   
            if (lastModified == -1) {   
                // servlet doesn't support if-modified-since, no reason   
                // to go through further expensive logic   
                doGet(req, resp);   
            } else {   
                long ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE);   
                if (ifModifiedSince < (lastModified / 1000 * 1000)) {   
                    // If the servlet mod time is later, call doGet()   
                    // Round down to the nearest second for a proper compare   
                    // A ifModifiedSince of -1 will always be less   
                    maybeSetLastModified(resp, lastModified);   
                    doGet(req, resp);   
                } else {   
                    resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);   
                }   
            }   
  
        } else if (method.equals(METHOD_HEAD)) {   
            long lastModified = getLastModified(req);   
            maybeSetLastModified(resp, lastModified);   
            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 {   
            //   
            // Note that this means NO servlet supports whatever   
            // method was requested, anywhere on this server.   
            //   
  
            String errMsg = lStrings.getString("http.method_not_implemented");   
            Object[] errArgs = new Object[1];   
            errArgs[0] = method;   
            errMsg = MessageFormat.format(errMsg, errArgs);   
               
            resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg);   
        }   
    }

 

  

在抽象类中GenericServlet中service()是一个抽象方法,但在HttpServlet中对这个方法进行了实现。
servlet接口中定义的service()方法中的两个参数分别是servletRequest 和 ServletResponse 这两个类型。当前的http请求,如果需要在这个service()方法内部使用http消息特有的功能,也就是要调用HttpServletRequest 和HttpServletResponse来中定义的方法时,需要将请求和响应对象进行一个类型的转换,所以,在GenericServlet中,使用了两个方法来共同完成这个工作。


实现父类GenericServlet中的service(ServltRequest req,ServeltResponse res)抽象方法
   

 public void service(ServletRequest req, ServletResponse res)   
        throws ServletException, IOException   
    {   
        HttpServletRequest        request;   
        HttpServletResponse        response;   
           
        try {   
            request = (HttpServletRequest) req;  //向下转型   
            response = (HttpServletResponse) res; //向下转型   
        } catch (ClassCastException e) {   
            throw new ServletException("non-HTTP request or response");   
        }   
        service(request, response);  //这里是调用重载的service(HttpServelertRequest,HttpServletResponse)方法   
    }  

     
   

 service(HttpServelertRequest,HttpServletResponse)方法: 
 
  //重载的service(HttpServletRequest req, HttpServletResponse resp)方法 ,注意参数类型
    
   

 protected void service(HttpServletRequest req, HttpServletResponse resp)   
        throws ServletException, IOException   
    {   
        String method = req.getMethod();   
  
        if (method.equals(METHOD_GET)) {   
            long lastModified = getLastModified(req);   
            if (lastModified == -1) {   
                // servlet doesn't support if-modified-since, no reason   
                // to go through further expensive logic   
                doGet(req, resp);   
            } else {   
                long ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE);   
                if (ifModifiedSince < (lastModified / 1000 * 1000)) {   
                    // If the servlet mod time is later, call doGet()   
                    // Round down to the nearest second for a proper compare   
                    // A ifModifiedSince of -1 will always be less   
                    maybeSetLastModified(resp, lastModified);   
                    doGet(req, resp);   
                } else {   
                    resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);   
                }   
            }   
  
        } else if (method.equals(METHOD_HEAD)) {   
            long lastModified = getLastModified(req);   
            maybeSetLastModified(resp, lastModified);   
            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 {   
            //   
            // Note that this means NO servlet supports whatever   
            // method was requested, anywhere on this server.   
            //   
  
            String errMsg = lStrings.getString("http.method_not_implemented");   
            Object[] errArgs = new Object[1];   
            errArgs[0] = method;   
            errMsg = MessageFormat.format(errMsg, errArgs);   
               
            resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg);   
        }   
    }   
    

     

相关标签: Servlet Go 工作