
编写HttpServ类继承HttpServlet类(抽象类),如果没有重写Service方法,接收请求时会调用HttpServlet类Service方法(注意参数类型是ServletRequest)。
HttpServlet类的service方法将ServletRequest req请求转换成HttpServletRequest请求,再调用HttpServlet类的service(request, response) 方法(注意参数类型是HttpServletRequest),判断用户发出是什么请求,如果是get则调用子类(HttpServ)的doGet方法,如果是post则调用子类(HttpSevr)的doPost方法。
那么,如果请求时get方式,子类HttpServ没有重写doGet方法,就会调用父类HttpServlet类的doGet方法,则会抛出异常。
这里说明一下,子类继承抽象父类,重写了父类的方法,如果父类成员方法内调用了该方法,会执行子类的方法,示例如下:
package ceshi;
public abstract class Shape{
void display(){//具体方法
System.out.println("Shape.display");
}
void hello(){//具体方法
display();
}
}
package ceshi;
class Rect extends Shape{
void display(){
System.out.println("Rect.display");
}
}
package ceshi;
public class Main {
public static void main(String[] args){
Rect r=new Rect();
r.hello();
}
}
运行结果如下:
这也就是为什么HttpServlet抽象类会调用子类的doget方法。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)