GenericServlet是Servlet接口的实现类,我们可以通过继承GenericServlet来编写自己的Servlet。
GenericServlet提供除了service()方法外所有接口中方法的缺省实现。
除了Servlet接口外,GenericServlet也实现了ServletConfig接口,处理初始化参数和servlet上下文,提供对授权传递到init()方法中的ServletConfig对象的获取的方法getServletConfig()(获取所有的初始的配置的参数)。
GenericServlet的方法,只有service(req,res)是抽象方法,只需要实现这个方法就好。
继承GenericServlet实现servlet,也就是说,继承GenericServlet可以实现servlet 的功能
public class MyGenericServlet extends GenericServlet{
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
res.setContentType("text/html;charset=UTF-8");
res.getWriter().print("你好,"+new Random().nextInt(100));
res.getWriter().print("<hr/>");
//直接调用获取servletconfig就可以了
res.getWriter().print("此类在web.xml中配置的名称为 :"+getServletConfig().getServletName());
}
}
GenericService总结
1:适配了javax.serlvet.Selvet接口。用户只需要重写service方法 。
2:包装了SevletConfig.用户直接调用