以下是我在qq中提问的聊天纪录
礼拜天()  (2007-05-21 10:44:23)
大家好 问个问题
Servlet和网页交互时,网页提交请求后,通过web.xml找到相应的servlet,此时servlet中的多个方法,如init(),doPost(),getServletInfo()等是按在servlet类中的顺序全部依次执行吗?敏()  (2007-05-21 10:47:18)
首先init()
get
doGet()
post
doPost() 礼拜天()  (2007-05-21 10:55:30)
也就是说关于servlet中的方法调用是按一定顺序执行的,这不同于其他普通java类中的调用哪个方法就执行哪个方法对吗? 敏()  (2007-05-21 10:57:31)
调用哪个方法就执行哪个方法礼拜天()  (2007-05-21 11:00:10)
是不是通过html文件中的
<form name="Sayhi" Method="Post" action="/JSPBook/CH2/Sayhi" >
的Method="Post" 就知道要调用doPost()?
问题到这里就没有继续回答下去
如果是根据 Method="Post" 就知道要调用doPost() 那岂不是意味着servlet中的方法是一些名字已经被固定下来的方法了?

解决方案 »

  1.   

    servlet只在生命周期的开始和结束各调用一次init()与destroy()用以初始化和销毁资源..而doGet()  和doPost()方法是在每一次请求之后反复调用的
      

  2.   

    如果是根据 Method="Post" 就知道要调用doPost() 那岂不是意味着servlet中的方法是一些名字已经被固定下来的方法了?
    这个是我现在疑问的重点。
      

  3.   

    这个问题是不是和service()有关系?
      

  4.   

    什么啊?不怎么懂?servlet里的doGet和doPost方法名本来就是固定的了
      

  5.   

    所有的请求都首先调用service方法,在父类HttpServlet中已经做好判断request.getMethod来判断应该执行哪个方法
      

  6.   

    结贴:
      
    之所以由 Method="Post" 就知道要调用doPost()是因为:所有的servlet都必须实现javax.servlet.Servlet接口这一般通过继承实现了该接口的javax.servlet.GenericServlet或javax.servlet.http.HttpServlet来实现servlet-api中的javax.servlet.Servlet接口源码:
    public interface Servlet
    {
        public abstract void init(ServletConfig servletconfig)
            throws ServletException;    public abstract ServletConfig getServletConfig();    public abstract void service(ServletRequest servletrequest, ServletResponse servletresponse)
            throws ServletException, IOException;    public abstract String getServletInfo();    public abstract void destroy();
    }
    servlet-api中的javax.servlet.GenericServlet源码:
    public abstract class GenericServlet
        implements Servlet, ServletConfig, Serializable
    {    public GenericServlet()
        {
        }    public void destroy()
        {
        }    public String getInitParameter(String name)
        {
            return getServletConfig().getInitParameter(name);
        }    public Enumeration getInitParameterNames()
        {
            return getServletConfig().getInitParameterNames();
        }    public ServletConfig getServletConfig()
        {
            return config;
        }    public ServletContext getServletContext()
        {
            return getServletConfig().getServletContext();
        }    public String getServletInfo()
        {
            return "";
        }    public void init(ServletConfig config)
            throws ServletException
        {
            this.config = config;
            init();
        }    public void init()
            throws ServletException
        {
        }    public void log(String msg)
        {
            getServletContext().log((new StringBuilder()).append(getServletName()).append(": ").append(msg).toString());
        }    public void log(String message, Throwable t)
        {
            getServletContext().log((new StringBuilder()).append(getServletName()).append(": ").append(message).toString(), t);
        }    public abstract void service(ServletRequest servletrequest, ServletResponse servletresponse)
            throws ServletException, IOException;    public String getServletName()
        {
            return config.getServletName();
        }    private transient ServletConfig config;
    }
      

  7.   

    servlet-api中的javax.servlet.http.HttpServlet源码:
    public abstract class HttpServlet extends GenericServlet
        implements Serializable
    {    public HttpServlet()
        {
        }    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException
        {
         }    protected long getLastModified(HttpServletRequest req)
        {
            return -1L;
        }    protected void doHead(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException
        {
        }    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException
        {
               }    protected void doPut(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException
        {
              }    protected void doDelete(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException
        {
               }    private static Method[] getAllDeclaredMethods(Class c)
        {
            if(c.equals(javax/servlet/http/HttpServlet))
                return null;
            Method parentMethods[] = getAllDeclaredMethods(c.getSuperclass());
            Method thisMethods[] = c.getDeclaredMethods();
            if(parentMethods != null && parentMethods.length > 0)
            {
                   }    protected void doOptions(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException
        {
            Method methods[] = getAllDeclaredMethods(getClass());
            boolean ALLOW_GET = false;
            boolean ALLOW_HEAD = false;
            boolean ALLOW_POST = false;
            boolean ALLOW_PUT = false;
            boolean ALLOW_DELETE = false;
            boolean ALLOW_TRACE = true;
            boolean ALLOW_OPTIONS = true;
            for(int i = 0; i < methods.length; i++)
            {
                Method m = methods[i];
                if(m.getName().equals("doGet"))
                {
                    ALLOW_GET = true;
                    ALLOW_HEAD = true;
                }
                if(m.getName().equals("doPost"))
                    ALLOW_POST = true;
                if(m.getName().equals("doPut"))
                    ALLOW_PUT = true;
                if(m.getName().equals("doDelete"))
                    ALLOW_DELETE = true;
            }        String allow = null;
            if(ALLOW_GET && allow == null)
                allow = "GET";
            if(ALLOW_HEAD)
                if(allow == null)
                    allow = "HEAD";
                else
                    allow = (new StringBuilder()).append(allow).append(", HEAD").toString();
            if(ALLOW_POST)
                if(allow == null)
                    allow = "POST";
                else
                    allow = (new StringBuilder()).append(allow).append(", POST").toString();
            if(ALLOW_PUT)
                if(allow == null)
                    allow = "PUT";
                else
                    allow = (new StringBuilder()).append(allow).append(", PUT").toString();
            if(ALLOW_DELETE)
                if(allow == null)
                    allow = "DELETE";
                else
                    allow = (new StringBuilder()).append(allow).append(", DELETE").toString();
            if(ALLOW_TRACE)
                if(allow == null)
                    allow = "TRACE";
                else
                    allow = (new StringBuilder()).append(allow).append(", TRACE").toString();
            if(ALLOW_OPTIONS)
                if(allow == null)
                    allow = "OPTIONS";
                else
                    allow = (new StringBuilder()).append(allow).append(", OPTIONS").toString();
            resp.setHeader("Allow", allow);
        }    protected void doTrace(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException
        {
          }    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);
            }
        }    private void maybeSetLastModified(HttpServletResponse resp, long lastModified)
        {
            if(resp.containsHeader("Last-Modified"))
                return;
            if(lastModified >= 0L)
                resp.setDateHeader("Last-Modified", lastModified);
        }    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);
        }    private static final String METHOD_DELETE = "DELETE";
        private static final String METHOD_HEAD = "HEAD";
        private static final String METHOD_GET = "GET";
        private static final String METHOD_OPTIONS = "OPTIONS";
        private static final String METHOD_POST = "POST";
        private static final String METHOD_PUT = "PUT";
        private static final String METHOD_TRACE = "TRACE";
        private static final String HEADER_IFMODSINCE = "If-Modified-Since";
        private static final String HEADER_LASTMOD = "Last-Modified";
        private static final String LSTRING_FILE = "javax.servlet.http.LocalStrings";
        private static ResourceBundle lStrings = ResourceBundle.getBundle("javax.servlet.http.LocalStrings");}
      

  8.   


    由于自己的servlet继承自javax.servlet.http.HttpServlet
    而javax.servlet.http.HttpServlet中定义了doXxxxx(),service()
    且service()中涉及“GET” “POST”等验证
    所以才会实现了
    根据 Method="Post" 就知道要调用doPost() 不知道我的回答对不对  
    新的疑问又来了
    GET,POST等要大写????