zoukankan      html  css  js  c++  java
  • Servlet学习-查漏补缺

    使用Filter作为控制器的好处:

    使用一个过滤器来做控制器,可以方便地在应用程序里对所有的资源(包括静态资源)进行控制访问。

    Servlet Vs Fileter

    1.Servlet能做的事情Filter都能做

    2.Filter能做的事情Servlet都可以完成吗? 拦截资源不是Servlet所擅长的,Filter中有一个 FilterChain,这个API在Servlet中是没有的。

    浏览器访问服务器的过程

    1.用户通过浏览器访问url
    2.客户机连上Web服务器
    3.客户发送Http请求
    4.服务器解释请求的 主机名 web应用 请求的页面
    5.首次的访问Servlet文件会创建实例对象
    6.调用Servlet的Init()方法完成Servlet的初始化
    7.创建Requser和Responce对象
    8.调用Servlet的Service()方法
    9.执行Service()方法,将回送的数据写进到Responce对象,
    10.Service()方法返回
    11.从Responce对象中取出数据,构建Http响应回写给客户机。
    12.服务器回写数据给客户端
    13.浏览器根据Http响应显示页面。

    Servlet的生命周期。


    1.当服务启动,客户机访问Servlet时,首次访问的Servlet会创建实例,并且调用Init()方法对Setvlet完成初始化。
    2.初始化之后,通过HttpServletRequser对象得客户端的请求数据调用Service()方法,
    3.Service()方法执行后通过HttpServletResponce对象将数据返回给客户端。
    4.当Web应用关闭或服务器被关闭,Servlet会调用destory()摧毁Servlet对象。

    Servletcontext与ServletConfig的关系

    ServletConfig:

     当servlet配置了初始化参数后,web容器在创建servlet实例对象时,会自动将这些初始化参数封装到ServletConfig对象中。

    ServletConfig中的参数是只提供给定义它的Servlet的,同一程序中的其他servlet是无法使用的,也就是说一个servlet的ServletConfig对象不能被另一个servlet访问。

    ServletContext:

    WEB容器在启动时,它会为每个WEB应用程序都创建一个对应的ServletContext对象,它代表当前web应用。

    也就是说一个web应用中的所有Setvlet是共享一个ServletContext。

    ServletContex参数的是定义在整个程序中且独立于各个单独的servlet的,同时也提供给程序中的所有servlet。

    也可以这样认为ServletContext相当于全局变量,ServletConfig相当于局部变量。

    使用配置文件,类型的选择 :

      1.xml 存进数据是有关系

      2.properties 存进数据没关系

    ServletConfig与ServletContext的应用

    ServletConfig的应用
      |--获取Servle的初始化配置信息
      |--获得字符集编码
        String charset = this.config.getInitParameter("charset");
      |--获得数据库连接信息
        String url = this.config.getInitParameter("url");
       |--获得配置文件
        String configFile = this.config.getInitParameter("config");


      Servletcontext的应用


      |--多个Servlet间实现数据共享(可能会有并发的线程安全问题)
        context.setAttribute("data", "共享数据"); //Servlet1向域中存了一个data属性
        String value = (String) context.getAttribute("data"); //Servlet2获取域中的data属性
      

      |--获取web应用的配置信息
        String url = this.getServletContext().getInitParameter("url");
      

      |--实现Servlet的转发
        this.getServletContext().getRequestDispatcher("/viewdata.jsp"). forward(request, response)
      

      |--读取资源文件
        |--使用ServletContext的getResourceAsStream方法:返回资源文件的读取字节流
          InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
          Properties prop = new Properties();
          prop.load(in);
          String url = prop.getProperty("url");


      |--使用ServletContext的getRealPath方法,获得文件的完整绝对路径path,再使用字节流读取path下的文件
        String path = this.getServletContext().getRealPath("/WEB-INF/classes/db.properties");
        FileInputStream in = new FileInputStream(path);
        Properties prop = new Properties();
        prop.load(in);
        String url = prop.getProperty("url");

      |--使用ServletContext的getResource方法,获得一个url对象,调用该类的openStream方法返回一个字节流,读取数据
        URL url = this.getServletContext().getResource("/WEB-INF/classes/db.properties");
        InputStream in = url.openStream();
        Properties prop = new Properties();
        prop.load(in);
        String url1 = prop.getProperty("url");


        |--web工程中,不同位置的资源文件的读取方式


          |--当资源文件在包下面时


            InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/cn/itcast/context/db.properties");
            System.out.println(in);
          

          |--资源文件在web-inf下
            InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/db.properties");
            System.out.println(in);
          

          |--资源文件在web工程中
            InputStream in = this.getServletContext().getResourceAsStream("/db.properties");
            System.out.println(in);
        

        |--在非servlet程序中如何读取配置文件:用类装载器(用类装载器读取资源文件时,千万要注意,资源文件绝对不能太大,否则极易导致内存溢出)
          

          |--用类装载方式读取(注意:在线程休眠过程中,即使改动了资源文件,获取到的还是原始内容)
            InputStream in = StudentDao.class.getClassLoader().getResourceAsStream("cn/itcast/context/db.properties");
            System.out.println(in);
          

          |--解决方案:用类装载方式读取,把资源当作url对待
            URL url = StudentDao.class.getClassLoader().getResource("db.properties");
            String path = url.getPath(); //这样可以获得资源文件名称
            FileInputStream in = new FileInputStream(path);
            Properties prop = new Properties();
            prop.load(in);
            System.out.println(prop.getProperty("url"));

            //测试强制休眠

            try {Thread.sleep(1000*15);} catch (InterruptedException e) {e.printStackTrace();}

            in = new FileInputStream(path);
            prop = new Properties();
            prop.load(in);
            System.out.println(prop.getProperty("url"));

  • 相关阅读:
    Apache Spark 2.2.0 中文文档
    Apache Spark 2.2.0 中文文档
    Apache Spark 2.2.0 中文文档
    Apache Spark 2.2.0 中文文档
    Apache Spark 2.2.0 中文文档
    Apache Spark RDD(Resilient Distributed Datasets)论文
    Apache Spark 2.2.0 中文文档
    Apache Spark 2.2.0 中文文档
    【机器学习实战】第10章 K-Means(K-均值)聚类算法
    [译]flexbox全揭秘
  • 原文地址:https://www.cnblogs.com/lhy_2011/p/4044455.html
Copyright © 2011-2022 走看看