zoukankan      html  css  js  c++  java
  • Java学习11.13

    学习servelet第二天

    参考引用链接https://www.runoob.com/servlet/servlet-intro.html

    Servlet 创有三种方式。

    1、实现 Servlet 接口

    因为是实现 Servlet 接口,所以我们需要实现接口里的方法。

    //Servlet的生命周期:从Servlet被创建到Servlet被销毁的过程
    //一次创建,到处服务
    //一个Servlet只会有一个对象,服务所有的请求
    /*
     * 1.实例化(使用构造方法创建对象)
     * 2.初始化  执行init方法
     * 3.服务     执行service方法
     * 4.销毁    执行destroy方法
     */
    public class ServletDemo1 implements Servlet {
    
        //public ServletDemo1(){}
    
         //生命周期方法:当Servlet第一次被创建对象时执行该方法,该方法在整个生命周期中只执行一次
        public void init(ServletConfig arg0) throws ServletException {
                    System.out.println("=======init=========");
            }
    
        //生命周期方法:对客户端响应的方法,该方法会被执行多次,每次请求该servlet都会执行该方法
        public void service(ServletRequest arg0, ServletResponse arg1)
                throws ServletException, IOException {
            System.out.println("hehe");
    
        }
    
        //生命周期方法:当Servlet被销毁时执行该方法
        public void destroy() {
            System.out.println("******destroy**********");
        }
    //当停止tomcat时也就销毁的servlet。
        public ServletConfig getServletConfig() {
    
            return null;
        }
    
        public String getServletInfo() {
    
            return null;
        }
    }

    2、继承 GenericServlet 类

    它实现了 Servlet 接口除了 service 的方法

    public class ServletDemo2 extends GenericServlet {
    
        @Override
        public void service(ServletRequest arg0, ServletResponse arg1)
                throws ServletException, IOException {
            System.out.println("heihei");
    
        }
    }

    3、继承 HttpServlet 方法

    public class ServletDemo3 extends HttpServlet {
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            System.out.println("haha");
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            System.out.println("ee");
            doGet(req,resp);
        }
    
    }

    创建 Servlet 的第三种方法,也是我们经常用的方法。

  • 相关阅读:
    sql server 2008 64位连接sql 2000服务器的时候出现
    MySQL忘记密码怎么修改密码
    vs2015 行数统计
    javascript和c#aes加密方法互解
    vs2015 不能启动 iis express
    修改oracle的字符集操作方法
    PL/SQL Developer连接本地Oracle 11g 64位数据库
    CodeSmith Generator 7.0.2激活步骤
    PetaPoco利用ODP.NET Managed Driver连接Oracle
    解决Chrome插件安装时出现的“程序包无效”问题亲测可用
  • 原文地址:https://www.cnblogs.com/Lizhichengweidashen/p/14138383.html
Copyright © 2011-2022 走看看