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. web内置对象

    内置对象,宿主对象,自定义对象的区别?     内置对象:         系统所提供的对象:Object,Array,Math,Date等等.     宿主对象:         JS所运行的环境提 ...

  2. UVA 12633 Super Rooks on Chessboard (生成函数+FFT)

    题面传送门 题目大意:给你一张网格,上面有很多骑士,每个骑士能横着竖着斜着攻击一条直线上的格子,求没被攻击的格子的数量总和 好神奇的卷积 假设骑士不能斜着攻击 那么答案就是没被攻击的 行数*列数 接下 ...

  3. POJ 2376 Cleaning Shifts (线段树优化DP)

    题目大意:给你很多条线段,开头结尾是$[l,r]$,让你覆盖整个区间$[1,T]$,求最少的线段数 题目传送门 线段树优化$DP$裸题.. 先去掉所有能被其他线段包含的线段,这种线段一定不在最优解里 ...

  4. 「Poetize4」创世纪

    在tyvj上怀疑爆栈了.....或许一定是我写挂了.以后调吧... UPD:bzoj上过了... 题解:https://blog.csdn.net/popoqqq/article/details/39 ...

  5. Jquery 根据HTML内容选择元素

    选择所有包含 "is" 的 元素: $("p:contains(is)")

  6. Linux(CentOS 6.4)系统中安装mplayer

    整了一个上午终于把mplayer安装上了,我的系统是centos 6.4,真是不容易啊! 一.准备工作 需要的安装包及下载地址:1.mplayer源代码包(MPlayer-1.0rc4.tar.bz2 ...

  7. MyEclipse 2014 有用的几个快捷键

    ctrl+h  fileSearch ------------------------------------- MyEclipse 快捷键1(CTRL) ---------------------- ...

  8. hadoop的理解

    知乎上有个人写的感觉挺好理解的,不知道对不对,先截图存着

  9. 机房工程-在线式、后备式UPS选择(转载)

    原文网址:http://oa.yesky.com/10/31061510all.shtml#p31061510 1后备式UPS还是在线式UPS? 作为机房设备的一项重要保护措施,UPS起着无可替代的作 ...

  10. cogs 106. [NOIP2003] 加分二叉树(区间DP)

    106. [NOIP2003] 加分二叉树 ★☆   输入文件:jfecs.in   输出文件:jfecs.out   简单对比时间限制:1 s   内存限制:128 MB [问题描述] 设 一个 n ...