import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;代码很短,我真的很不理解为什么不能写红色的那句,重写从HttpServlet 继承的service方法不添加SUPER怎么会完成其功能呢?
而且一定要加上SUPER也是个错的?如果要加SUPER怎么加啊?
public class HelloWS extends HttpServlet { @Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
    super.service(req, resp);
PrintWriter out = resp.getWriter();
out.println("<html><head><title></title></head><body></body></html>");
    out.close();
} /*@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
PrintWriter out = resp.getWriter();
out.println("<html><head><title>adfadfad</title></head><body>a星星得过</body></html>");
    out.close();
}*/ private static final long serialVersionUID = 1L;
}

解决方案 »

  1.   

    这玩意是框架,只要你动用doget()或者dopost(),他会自动的调用service()方法
      

  2.   

    J2EE API 是这么说的:Receives standard HTTP requests from the public service method and dispatches them to the doXXX methods defined in this class,大概意思就是:当service接到请求时就会转发给doXXX(doGet)方法。而这些方法里都有超类的super调用,所以我们在覆盖的时候重写这个方法了,我是这么理解的。
      

  3.   

    答:
    1)楼主弄错了. super.service(req, resp); 当然可以加,这是从语法角度说的(即:语法上正确的,可以加的).但从HTTP SERVLET的语义处理来说,是错误的.
    2)执行过程是:容器自动调用service(...)方法,而service(...)方法内部根据HTTP Request方法的不同,分别再自动调用doGET(....)或者是doPost(....)等.
    3)当你不加上super.service(req, resp); 时,容器自动调用你自己定制的service(...)方法,而你自己定制的service(...)方法代码中没有再去调用doGET(..)或doPOST(...),因而:doGET()与doPOST()是没有用到的.
      

  4.   

    答:我贴上HttpServlet的源代码,这下楼主就更清楚了:(由于只能8000字,我只好贴出核心)package javax.servlet.http;public abstract class HttpServlet extends GenericServlet
        implements Serializable
    {
        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");
        /*synthetic*/ static Class class$javax$servlet$http$HttpServlet;
        
        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
    String protocol = req.getProtocol();
    String msg = lStrings.getString("http.method_get_not_supported");
    if (protocol.endsWith("1.1"))
        resp.sendError(405, msg);
    else
        resp.sendError(400, msg);
        }
        
        protected long getLastModified(HttpServletRequest req) {
    return -1L;
        }
        
        protected void doHead(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
    NoBodyResponse response = new NoBodyResponse(resp);
    doGet(req, response);
    response.setContentLength();
        }
        
        protected void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
    String protocol = req.getProtocol();
    String msg = lStrings.getString("http.method_post_not_supported");
    if (protocol.endsWith("1.1"))
        resp.sendError(405, msg);
    else
        resp.sendError(400, msg);
        }
        
        protected void doPut(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
    String protocol = req.getProtocol();
    String msg = lStrings.getString("http.method_put_not_supported");
    if (protocol.endsWith("1.1"))
        resp.sendError(405, msg);
    else
        resp.sendError(400, msg);
        }
        
        protected void doDelete
    (HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
    String protocol = req.getProtocol();
    String msg = lStrings.getString("http.method_delete_not_supported");
    if (protocol.endsWith("1.1"))
        resp.sendError(405, msg);
    else
        resp.sendError(400, msg);
        }
        
        private static Method[] getAllDeclaredMethods(Class c) {
    if (c.equals(class$javax$servlet$http$HttpServlet == null
         ? (class$javax$servlet$http$HttpServlet
    = class$("javax.servlet.http.HttpServlet"))
         : class$javax$servlet$http$HttpServlet))
        return null;
    Method[] parentMethods = getAllDeclaredMethods(c.getSuperclass());
    Method[] thisMethods = c.getDeclaredMethods();
    if (parentMethods != null && parentMethods.length > 0) {
        Method[] allMethods
    = new Method[parentMethods.length + thisMethods.length];
        System.arraycopy(parentMethods, 0, allMethods, 0,
         parentMethods.length);
        System.arraycopy(thisMethods, 0, allMethods, parentMethods.length,
         thisMethods.length);
        thisMethods = allMethods;
    }
    return thisMethods;
        }
        
        protected void doOptions
    (HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
    Method[] methods = getAllDeclaredMethods(this.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 += ", HEAD";
    }
    if (ALLOW_POST) {
        if (allow == null)
    allow = "POST";
        else
    allow += ", POST";
    }
    if (ALLOW_PUT) {
        if (allow == null)
    allow = "PUT";
        else
    allow += ", PUT";
    }
    if (ALLOW_DELETE) {
        if (allow == null)
    allow = "DELETE";
        else
    allow += ", DELETE";
    }
    if (ALLOW_TRACE) {
        if (allow == null)
    allow = "TRACE";
        else
    allow += ", TRACE";
    }
    if (ALLOW_OPTIONS) {
        if (allow == null)
    allow = "OPTIONS";
        else
    allow += ", OPTIONS";
    }
    resp.setHeader("Allow", allow);
        }
        
        protected void doTrace
    (HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
    String CRLF = "\r\n";
    String responseString
        = "TRACE " + req.getRequestURI() + " " + req.getProtocol();
    Enumeration reqHeaderEnum = req.getHeaderNames();
    while (reqHeaderEnum.hasMoreElements()) {
        String headerName = (String) reqHeaderEnum.nextElement();
        responseString += ((String) CRLF + headerName + ": "
           + req.getHeader(headerName));
    }
    responseString += (String) CRLF;
    int responseLength = responseString.length();
    resp.setContentType("message/http");
    resp.setContentLength(responseLength);
    ServletOutputStream out = resp.getOutputStream();
    out.print(responseString);
    out.close();
        }
        
    //这是核心:根据请求的方法不同,自动地分别调用doXXX(...)方法
        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")) {
        if (lastModified >= 0L)
    resp.setDateHeader("Last-Modified", lastModified);
    }
        }
        //这个是容器真正自动调用的service(...)方法
        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);  //自动调用HttpServlet版本的service(...)方法
        }
        
       }
      

  5.   

    错误的原因是因为,在Httpservlet当中有两个service方法:
    protected  void service(HttpServletRequest req, HttpServletResponse resp) 
     
     void service(ServletRequest req, ServletResponse res) 
    jvm不知道该调用那个,所以会出现问题
      

  6.   

    继承HttpServlet类没有必要重写Service方法,你只需要重写
    init,doGet(),doPost(),destroy()
      

  7.   

    你说的错指的是什么?没有输出你的out.println(" <html> <head> <title> </title> </head> <body> </body> </html>"); 吧
    调用完类HttpServlet service()方法之后就根据请求的方法直接调用你的doGet() doPost()方法了,后边的PrintWriter out = resp.getWriter(); 
    out.println(" <html> <head> <title> </title> </head> <body> </body> </html>"); 
        out.close(); 没有执行的机会。
      

  8.   

    7楼讲的很清楚了,一般是重写doGet()和doPost()方法。
      

  9.   

    一般是重写doGet()和doPost()方法。
      

  10.   

    我是来学习的  看的很畅快在 自己重写的 service() 方法里面 再调用他父类的 service()方法 我觉得解释不通  
      

  11.   

    java这个破东西多看看源代码就OK了,实在不行,debug一下就OK