servletconfig

  java.lang.String getInitParameter(java.lang.String name)  //根据参数名获取参数值

  java.util.Enumeration getInitParameterNames()    //获取所有参数

servletcontext

  java.lang.String getContextPath()   --得到当前web应用的路径

  java.lang.String getInitParameter(java.lang.String name)  --得到web应用的初始化参数

  java.util.Enumeration getInitParameterNames()

  void setAttribute(java.lang.String name, java.lang.Object object) --域对象有关的方法

  java.lang.Object getAttribute(java.lang.String name)

  void removeAttribute(java.lang.String name)

  RequestDispatcher getRequestDispatcher(java.lang.String path)   --转发(类似于重定向)

  java.lang.String getRealPath(java.lang.String path)     --得到web应用的资源文件

  java.io.InputStream re(java.lang.String path)

区别

  一个是把servlet配置封装到servlet中(servletconfig)

  一个web.xml整个文件信息封装(servletcontext)

  一个web项目可以存在多个servletconfig但是只能存在一个servletcontext

servletconfig应用场景配置信息

  列如:读文件的路径改变时需要频繁修改信息

    解决方案:把配置文件写入到servlet中

      <servlet>

        <servlet-name></servlet-name>

        <servlet-class></servlet-class>

        <init-param>

          <param-name>path</param-name>//这是参数名

          <param-value>c:/a.txt</param-value>//可以在这里修改路径

        </init-param>

      </servlet>

  调用

  写一个servlet(Demo)继承httpservlet

  this.getServletConfig("path");//this代表了当前类(Demo) getsetvletconfig是sun公司封装config方法 返回的是value

servletcontext

servletContext被sun公司封装在servleconfig中所以调用的时候为servletcontext contest = config.getServletContext();或者servletcontext contest =this.servletcontext

Sun公司设计

1)创建ServletContext对象   ServletContext  context = new ServletContext()

2)创建ServletConfig对象

             ServetConfig config = new ServletConfig();

 config.setServletContxt(context);

      class  ServletConfig{

      ServletContext context;

      public ServletContext getServletContxt(){

      return contxt;

      }

      }    

      public void init( ServletConfig config ){

      得到ServletConfig对象

      从ServletConfig对象中得到ServletContext对象

      SerlvetContext context = config.getServletContext();

      }

获取路径

    String str = this.getServletContext().getContextPath();//str为  /+当前项目名称(应用与请求重定向资源路径中)

contextpath应用一(转发)

    this.getServletContext().getRequestDispatcherType("path").forward(request,response);

应用与域对象保存分享数据(但是会覆盖原来的数据)

加载外部资源

    String path = this.getServletContext().getRealPath("资源路径");// "/WEB-INF/class/db.properties"

加载外部资源比较

  例如src下有一个外部文件db.properties

  java项目 获取路径为"./src/db.properties"

  web项目 用函数getRealPath()或者getResourseAsStream();

为什么不能用 " . "获取

  因为 . 代表的是java当前运行的目录(servlet中在tomcat服务器的bin目录下面)所以不建议

  

servletconfig和servletcontext学习的更多相关文章

  1. JaveWeb学习之Servlet(二):ServletConfig和ServletContext

    原文同步发表至个人博客[夜月归途] 原文链接:http://www.guitu18.com/se/java/2018-07-26/20.html 作者:夜月归途 出处:http://www.guitu ...

  2. JavaWeb学习笔记:ServletConfig()和ServletContext()

    ServletConfig()和ServletContext() 1.ServletConfig() ServletConfig是一个接口,它由server提供商来实现. ServletConfig封 ...

  3. Servlet学习笔记(二)之Servlet路径映射配置、Servlet接口、ServletConfig、ServletContext

    Servlet路径映射配置 要使Servlet对象正常的运行,需要进行适当的配置,以告诉Web容器哪个请求调用哪个Servlet对象处理,对Servlet起到一个注册的作用.Servlet的配置信息包 ...

  4. day05 Servlet 开发和 ServletConfig 与 ServletContext 对象

    day05 Servlet 开发和 ServletConfig 与 ServletContext 对象 1. Servlet 开发入门 - hello world 2. Servlet 的调用过程和生 ...

  5. 谈谈 ServletConfig 和 ServletContext

    目录 一.ServletConfig 和 ServletContext 的概念 二.ServletConfig 和 SerlvetContext 代码表示 一.ServletConfig 和 Serv ...

  6. ServletConfig与ServletContext

    ServletConfig与ServletContext对象详解 一.ServletConfig对象    在Servlet的配置文件中,可以使用一个或多个<init-param>标签为s ...

  7. ServletConfig和ServletContext

    ServletConfig和ServletContext Servlet初始化参数 在Servlet的配置文件web.xml中,可以使用一个或多个<init-param>标签为Servle ...

  8. JavaEE:Servlet简介及ServletConfig、ServletContext

    Servlet简介 1.Servlet是sun公司提供的一门用于开发动态web资源的技术*静态web资源:固定数据文件*动态web资源:通过程序动态生成数据文件2.Servlet技术基于Request ...

  9. ServletConfig、ServletContext 的应用

    一.ServletConfig对象及其应用(用的不多) 1. Context和ContextPath:一个web工程,若名为JavaWeb,访问的路径为:http://localhost:8080/J ...

随机推荐

  1. day06-08面向对象的三大特性

    一.继承 1.1什么是继承?继承是一种创建新类的方式,在python中,新建的类可以继承一个或多个父类,父类又可称为基类或超类,新建的类称为派生类或子类python中类的继承分为:单继承和多继承 cl ...

  2. JsonPluginsUtil

    package utils; import java.lang.reflect.Field;import java.text.SimpleDateFormat;import java.util.Arr ...

  3. 如何在IE11中设置兼容模式?设置的具体方法

    IE11浏览器软件版本:简体中文正式版 For Win7网络工具立即查看 1.同样进入需要兼容性模式的网站,点击菜单栏位工具--F12开发者人员工具!如下图所示. 2.在开发者选项左下侧菜单栏位,点击 ...

  4. 死磕itchat源码--目录结构

    阅读itchat源码时,先弄清itchat的目录结构 itchat │ config.py │ content.py │ core.py │ log.py │ returnvalues.py │ ut ...

  5. Oracle 知识积累

    1.oracle存储过程中is和as的区别 在存储过程(PROCEDURE)和函数(FUNCTION)中没有区别,在视图(VIEW)中只能用AS不能用IS,在游标(CURSOR)中只能用IS不能用AS ...

  6. Project Euler 43 Sub-string divisibility

    题意: 1406357289是一个0至9全数字数,因为它由0到9这十个数字排列而成:但除此之外,它还有一个有趣的性质:子串的可整除性.记d1是它的第一个数字,d2是第二个数字,依此类推,我们注意到: ...

  7. 3.1、Ansible命令简要说明及初步使用

    1.Ansible命令 1.1 Ad-hoc说明 Ansible中有一个很重要的功能就是可以执行ad-hoc命令,它表示即时.临时的意思,即表示一次性的命令.与之相对的是ansible playboo ...

  8. HttpClient异步请求Post传递Json

    HttpClient异步Post请求,HttpClient.PostAsync(String, HttpContent, CancellationToken),String为Post的Url,Http ...

  9. 操作服务器及MySQL数据库可以使其远程链接

    转自原文操作服务器及MySQL数据库可以使其远程链接 一般情况分三个地方准备,MySQL数据库,防火墙,还有你的服务器主机的准备 操作系统为centos6.5.其他系统大致差不多. 1:在服务器中安装 ...

  10. java web项目发生异常依然能运行

    由于JavaWeb应用业务逻辑的复杂性,容易发生一些意想不到的错误和异常,给系统的调试带来不必要的麻烦,不友好的提示信息使编程者对错误和异常无从下手.特别是当发生异常时,Java异常栈输出的信息只能给 ...