ServletConfig和ServletContext

1.ServletContext在整个web应用程序生命周期内存在,用来保存全局对象,整个web应用都可以使用其获取context参数。当启动服务器后,servlet容器将读取web.xml中配置的context参数,进行初始化工作。ServletContext可以获取该参数。比如我们可以配置编码信息供web程序使用。

web.xml

  1. <web-app>
  2. <context-param>
  3. <param-name>encoding</param-name>
  4. <param-value>UTF-8</param-value>
  5. </context-param>
  6. </web-app>

ReadEncoding.java读取context参数中的编码信息。

="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" />ReadEncoding

public class ReadEncoding extends HttpServlet
{

//获取servletcontext对象
ServletContext sc = this.ServletConfig.getServletContext();
//获取初始化参数
String encoding = sc.getInitParameter("encoding");
//设置响应的编码格式
response.setCharacterEncoding(encoding);
}

为了方便获取ServletContext对象,servlet提供了一种简化的写法,this.getServletContext();

其实该方法内部也是调用了this.getServletConfig.getSerlvetContext。

2.ServletConfig对象保存着servlet的配置信息,当servlet被实例化后,web容器读取初始化参数,保存到config对象中,

每一个servlet都有一个servletconfig对象保存着自己的参数信息。

web.xml

web.xml

<web-app>
<servlet-name>myServlet</servlet-name>
<servlet-class>com.cnblogs.7days.helloServlet</servlet-class>
<intit-param>
<param-name>blogname</param-name>
<param-value>cnblogs</param-value>
</intit-param>
</web-app>

ReadBlogName.java

  1. public class ReadBlogName extends HttpServlet
  2. {
  3. //读取配置的blogname
  4. String blogName = this.getServletConfig.getInitParameter("blogName");
  5. System.out.println(blogName);
  6. }

同ServletContext一样,servlet容器也提供了一个简化获取servletconfig参数的方法,this.getInitParameter("blogName")。

ServletConfig和ServletContext 区别的更多相关文章

  1. JavaEE:Servlet简介及ServletConfig、ServletContext

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

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

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

  3. ServletConfig与ServletContext

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

  4. ServletConfig和ServletContext

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

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

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

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

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

  7. ServletConfig、ServletContext 的应用

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

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

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

  9. 谈谈 ServletConfig 和 ServletContext

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

随机推荐

  1. C语言标准库之setjmp

    协程的介绍 协程(coroutine),意思就是“协作的例程”(co-operative routines),最早由Melvin Conway在1963年提出并实现.跟主流程序语言中的线程不一样,线程 ...

  2. hdu-1317 XYZZY---Floyd判连通+bellman最短路

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1317 题目大意: 题意:有n个房间(n<=100),每个房间有一个点权(第1号房间和第n号房间 ...

  3. 【HHHOJ】NOIP模拟赛 捌 解题报告

    点此进入比赛 得分: \(30+30+70=130\)(弱爆了) 排名: \(Rank\ 22\) \(Rating\):\(-31\) \(T1\):[HHHOJ260]「NOIP模拟赛 捌」Dig ...

  4. sessionStorage 和 localStorage

    html5 中的 web Storage 包括了两种存储方式:sessionStorage 和 localStorage. sessionStorage 用于本地存储一个会话(session)中的数据 ...

  5. Oracle 函数 之 Coalesce()、greatest()、least()

    Coalesce().greatest().least() oracle比较一列的数据大小时,我们一般使用max()/min()函数,比较一行的最大值或者最小值时,使用函数Coalesce()/gre ...

  6. @property 关键字的区别详解

    @property 关键字的区别详解:assign 与weak. __block 与 __weak.strong 与copy的区别 字数911 阅读269 评论2 喜欢8 在开发中经常使用属性@pro ...

  7. linux网络编程之断点传输文件

    以下载链接"http://www.boa.org/boa-0.94.13.tar.gz"为例: 断点续传实验大概步骤: ===================== 1,使用geth ...

  8. org.hibernate.hql.internal.ast.QuerysyntaxException:user is not mapped [from User where user_code=? and user_password=?]

    初学者,一个很低级的错误吧! 找不到映射,最后发现没把类的Hibernate映射文件 添加到Hibernate核心配置文件中去,所以报了这个异常! 在核心文件中添加映射   <mapping r ...

  9. Django项目中"expected str, bytes or os.PathLike object, not list"错误解决:

    对于这个错误,也在于自己对django基础的掌握不是很牢固,忽略了MEDIA_ROOT的类型是string,而不是list. 错误的写法: MEDIA_ROOT = [ os.path.join(BA ...

  10. EasyUI获取正在编辑状态行的索引

    function getRowIndex(target){ var tr = $(target).closest("tr.datagrid-row"); return paseIn ...