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. css3媒介查询使用规则小结

    响应式布局可以为不同终端的用户提供更加舒适的界面和更好的用户体验,而且随着目前大屏幕移动设备的普及,用大势所趋来形容也不为过. 用一句话来说:使用同一套Html代码来适配不同设备和满足不同场景不同用户 ...

  2. java 常用API 包装 练习

    package com.oracel.demo01; import java.util.Random; public class Swzy { public static void main(Stri ...

  3. 初级模拟电路:1-2 PN结与二极管

    回到目录 1.   掺杂半导体 上面我们分析了本征半导体的导电情况,但由于本征半导体的导电能力很低,没什么太大用处.所以,一般我们会对本征半导体材料进行掺杂,即使只添加了千分之一的杂质,也足以改变半导 ...

  4. Mysql5.7安装过程----win10

    ---恢复内容开始--- 1.Mysql官网:https://www.mysql.com/downloads/ 有两种下载方式:msi和zip压缩包 2.我下载的是zip压缩包,选择mysql com ...

  5. 常用HTML小部件

  6. Project Euler 20 Factorial digit sum( 大数乘法 )

    题意:求出100!的各位数字和. /************************************************************************* > Fil ...

  7. BZOJ 3439 Kpm的MC密码 (Trie树+线段树合并)

    题面 先把每个串反着插进$Trie$树 每个节点的子树内,可能有一些节点是某些字符串的开头 每个节点挂一棵权值线段树,记录这些节点对应的原来字符串的编号 查询的时候在线段树上二分即可 为了节省空间,使 ...

  8. Hibernate 的核心配置文件

    核心配置文件 <!-- SessionFactory,相当于之前学习连接池配置 --> <session-factory> <!-- 1 基本4项 --> < ...

  9. vue 如何动态切换组件,使用is进行切换

    日常项目中需要动态去切换组件进行页面展示. 例如:登陆用户是“管理员”或者“普通用户”,需要根据登陆的用户角色切换页面展示的内容.则需要使用 :is 属性进行绑定切换 <template> ...

  10. Centos如何安装 jdk 环境变量

    一.编辑 profile 文件 vim /etc/profile 二.在 profile 文件下面最下面加上以下内容 export JAVA_HOME=/usr/local/java/jdk1.7.0 ...