Servlet快速入门

来源:互联网 发布:网络热门微拍百度云 编辑:程序博客网 时间:2024/05/21 17:44

Servlet的架构简述

Servlet --GenericServlet--HttpServlet--myClass

Servlet接口

public class servletShow implements Servlet {    //servlet 的生命周期  实例化,初始化,服务,销毁    //此接口有五个方法,init  service destory 是生命周期方法         //getServletConfig是获取任何启动信息,getServletInfo是垃圾方法    /**     * 在访问此servelt的时候打印如下信息:     * (可以修改配置,让服务器一启动就实例化,初始化servlet,还可以       *  配置不同的servlet出生的先后顺序)      *  老子实例化了,只有第一次访问的时候        老子初始化了,只有第一次访问的时候        老子服务了        当服务器关闭或者项目卸载的时候回打印“老子销毁了”        第一次访问后,刷新页面只会重复打印“老子服务了”,--------------------所以Servlet是单实例-----------------------        第一次访问实例化一个servlet,只有一个,但是service()方法是多线程的     */    //构造器    public servletShow() {        super();        System.out.println("老子实例化了");    }    //销毁    @Override    public void destroy() {        System.out.println("老子销毁了");    }    //获得配置    @Override    public ServletConfig getServletConfig() {        // TODO Auto-generated method stub        return null;    }    //垃圾方法,获得信息,作者,版本,版权等等无用信息    @Override    public String getServletInfo() {        // TODO Auto-generated method stub        return null;    }    //初始化    @Override    public void init(ServletConfig arg0) throws ServletException {        System.out.println("老子初始化了");    }    //服务    @Override    public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {        System.out.println("老子服务了");    }}

GenericServlet使用适配器设计模式,继承Servlet接口和多个其他的类

/** *  *----------------------- 适配器设计模式adapter模式----------------------------- *      class adaptee  {method2();} *      class adapter   extends adaptee implements target *      class client   {method1(target t);} *      interface target   {method3();} *       *      如上,client类想要在method1中使用adaptee中的方法,但是自己又没有关于adaptee的参数, *      target a=new adapter(); * *      将一个类的接口转换成客户希望的另外一个接口。 Adapter模式使得原本由于接口不兼容 而不能一起工作的那些类可以一起工作。  */public class servletShow_1 extends GenericServlet {    //GenericServlet实现了servlet的接口,不用把servlet中的所以方法都写出来    public servletShow_1() {        super();    }    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {    }}

HttpService使用模板方法设计模式,继承GenericServlet

public class servletShow2 extends HttpServlet {   /**    *      *  -------------模板方法设计模式 template method----------------------    *      *         1.带着炮友出去    *         2.吃东西   //吃的菜到底是什么?    *         3.愉快玩耍//到底去什么地方玩什么?    *         4.开炮    *  --------------------------------------------------------------    *  模板方法设计模式:定义好骨架,某些具体步骤在子类中实现,这样子类可以在不改变算法结构的    *  情况下定义具体实现。httpServlet把构架写好,我们通过重写    * doget()和doPost()来实现具体的功能。    *  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(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg); //返回error        } else {            resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);    //返回error        }    }        这是doGet()的源码,如果不重写,那么就会返回405错误信息在浏览器页面上。        httpServlet重写了和重载了service方法         public void service(ServletRequest req, ServletResponse res){                把req强转成HttpServletRequest类型                调用service(HttpServletRequest req, HttpServletResponse resp)         }       protected void service(HttpServletRequest req, HttpServletResponse resp){            根据http传来的method.equals(METHOD_GET)  调用不同的方法:doget() dopost()       }    */    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {    }    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        doGet(request, response);    }}

自己写的类继承HttpServlet,最常用的一种

public class servletShow3 extends HttpServlet {    /*  int num1=1;    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        num1++;//servlet是单例,多个人对num1进行++,线程会出现问题。        //所以不要用全局变量,使用局部变量。        int num2=1;        System.out.println(num1);    }*/    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        System.out.println("老子要办事!");        System.out.println("办不了,我找人来");        ServletContext application=this.getServletContext();  //servletContext域对象,代表整个app        RequestDispatcher rd=application.getRequestDispatcher("/servletShow4");        rd.forward(request, response);//把请求向下传递        System.out.println("事情办完了");    }    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        doGet(request, response);    }}//--------------------------------------------------public class servletShow4 extends HttpServlet {    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        System.out.println("我是show4,我来帮show3办事");    }    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        doGet(request, response);    }}