如何覆盖GenericServlet?

时间:2018-04-07 18:05:39

标签: java compiler-errors

这是我的代码

public class hi extends GenericServlet{
    public void init(ServletConfig parm1){

    }

    @Override
    public void service(ServletRequest req,ServletRequest res){
        PrintWriter pw = res.getWriter();
        pw.println("hi");
    }
}

当我尝试编译时:

hi.java:16: error: hi is not abstract and does not override abstract method service(ServletRequest,ServletResponse) in GenericServlet
public class hi extends GenericServlet{
       ^
hi.java:21: error: method does not override or implement a method from a supertype
    @Override
    ^
hi.java:23: error: cannot find symbol
        PrintWriter pw = res.getWriter();
                            ^
  symbol:   method getWriter()
  location: variable res of type ServletRequest
3 errors  

我想我已经覆盖了GenericServlet的service方法,但编译器确实得到了它,让我感到很困惑......

1 个答案:

答案 0 :(得分:0)

服务方法的第二个参数应该是ServletResponse