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. BZOJ 1305: [CQOI2009]dance跳舞 网络最大流_二分答案_建模

    Description 一次舞会有n个男孩和n个女孩.每首曲子开始时,所有男孩和女孩恰好配成n对跳交谊舞.每个男孩都不会和同一个女孩跳两首(或更多)舞曲.有一些男孩女孩相互喜欢,而其他相互不喜欢(不会 ...

  2. 爬虫工具--Beautifusoup

    import requests from bs4 import BeautifulSoup s=requests.Session() r=s.get('https://www.tumblr.com/l ...

  3. ELO kernels 记录

    these kernel for discuss how to handle outliers in target values. 一:Ashish Gupta: 在16年6月到18年8月,激活卡的人 ...

  4. nyoj2-吝啬的国度

    吝啬的国度 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 在一个吝啬的国度里有N个城市,这N个城市间只有N-1条路把这个N个城市连接起来.现在,Tom在第S号城市,他有 ...

  5. Vue接口日常学习

    最近使用uni.app 进行app的开发  页面搭完之后,发现不会调接口,今天学习了下 各个程序运行时,都会发起网络请求,网络相关的API在使用之前都会在使用前配置域名白名单 首先   现在中间件上一 ...

  6. 【我所认知的BIOS】—&gt; uEFI AHCI Driver(6) AtaAtapiPassThruSupported的局部变量们

    [我所认知的BIOS]-> uEFI AHCI Driver(6) - AtaAtapiPassThruSupported的局部变量们 LightSeed 5/7/2014 前面5个篇文章把EF ...

  7. linux c获取mac

    #include <stdio.h> #include <string.h> #include <net/if.h> #include <sys/ioctl. ...

  8. HDU 2732 Leapin&#39; Lizards(拆点+最大流)

    HDU 2732 Leapin' Lizards 题目链接 题意:有一些蜥蜴在一个迷宫里面,有一个跳跃力表示能跳到多远的柱子,然后每根柱子最多被跳一定次数,求这些蜥蜴还有多少是不管怎样都逃不出来的. ...

  9. Gradle:Gradle入门

    一.安装Gradle  1.首先确保你安装的JDK1.5或以上版本号.  C:\Users\chengxiang.peng.QUNARSERVERS>java -version java ver ...

  10. Agile实践日志一 -- Grooming Session

    Agile实践日志一  -- Grooming Session GroomingSession 这个Session主要Go through我们下一个Sprint须要做的Story,大家都清楚之后,在每 ...