`
topfeng
  • 浏览: 2349 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

ServletConfig与ServletContext的关系及使用

阅读更多

ServletConfig:从一个Servlet被实例化后,对任何客户端在任何时候访问有效,但仅对本Servlet有效(即不是针对所有的Servlet),一个servlet的ServletConfig对象不能被另一个servlet访问。

的配置,为了不使用“硬编码”,应该配置为ServletContext参数,比如字符集的设定。
  ServletContext:对任何servlet,任何客户端在任何时间都有效(对整个应用而言),这才是真正全局的对象。

  那么,ServletConfig参数和ServletContext参数到底应该如何使用,如何取得?
  一般来说,对整个应用的配置,为了不使用“硬编码”,应该配置为ServletContext参数,比如字符集的设定。

  而如果只有一个特定的servlet要设定的参数,其它servlet不能共享,应该配置为ServletConfig参数,比如获得这个Servlet的作者。

  访问ServletConfig参数:
  首先要取得ServletConfig对象,然后调用它的getInitParameter();方法。要访问
ServletConfig对象,jsp中直接使用config内置对象,但因为你的JSP编译后的servlet一般不会被加到web.xml中的,所以一般不会通过jsp来取对本JSP编译后的servlet的配置参数,那么在servlet中要得到ServletConfig对象一般的方法:
  在init()方法中取到:通过init的重载方法传递 :
public class Test extends HttpServlet{
  ServletConfig config;
  public void init(ServletConfig config) throws ServletException {
  this.config = config;
}
  然后在下面的方法中就可以访问config对象。但要注意,为了确保能从构造方法中到到当前servlet的config对象,应该调用父类的构造方法:

public class Test extends HttpServlet
{
  ServletConfig config;
  public void init(ServletConfig config) throws ServletException {
  super.init(config);
  this.config = config;

  通过getServletConfig()方法直接到时,这样做的好处是不必调手工传递属性,想在任何时候都可以得到。
  要访问ServletContext对象,只要从现有的ServletConfig对象中servletContext=getServletContext()就可以了,然后调用servletContext.getInitParameter()方法就可以获取它的参数。

 

分享到:
评论

相关推荐

    ServletConfig与ServletContext.docx

    注意:每个servlet有一个ServletConfig,每个Web应用有一个ServletContext.要把初始化参数认为是部署时常量,可用在运行时得到这些初始化参数,但不能设置初始化参数,根本没有setInitParameter().如果修改XML来改变...

    ServletContext与ServletConfig关系

    ServletContext与ServletConfig关系

    有关ServletConfig与ServletContext的访问

    下面小编就为大家带来一篇有关ServletConfig与ServletContext的访问。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    ServletContext与ServletConfig的深度分析

    很清楚的讲解了ServletContext和ServletConfig,不光有理论上的讲解,还有例子。

    方立勋:JavaWeb视频前十一天教程配套ppt下载

    方立勋JavaWeb视频教程_servlet开发和ServletConfig与ServletContext对象(第五天) 方立勋JavaWeb视频教程_request response(第六天) 方立勋JavaWeb视频教程_Cookie和Session(第七天) 方立勋JavaWeb视频教程_...

    【动力节点】Java经典教程_Servlet经典实战视频教程

    包括Servlet生命周期、ServletConfig、ServletContext、欢迎页头面的设置,及的设置与匹配。第二部分为Servlet核心。除了包括基础的API外,例如GenericServl 资源太大,传百度网盘了,链接在附件中,有需要的同学...

    JAVA servlet API说明文档

    servlet容器的详细说明,包括里面接口的方法。内容如下:RequestDispatch, servlet, servletConfig, servletContext。。

    Servlet知识点和示例

    this.ServletConfig.getServletContext方法获得对ServletContext对象的引用.还可以直接通过 this.getServletContext方法获得对ServletContext对象的引用. (3)由于一个WEB应用中的所有Servlet共享同一个...

    Servlet的运行机制

    1. 手动编写java代码,实现Servlet的访问 2. 使用Eclipse创建Web Project,并创建一个Servlet继承HttpServlet,打印“Hello HttpServlet”在浏览器上。...7. 使用ServletContext对象,实现转发操作

    java_web_第3章_servlet2.ppt

    3.1 Web应用的结构和部署 3.2 部署描述文件 3.3 ServletConfig接口 3.4 ServletContext接口 3.5 Servlet的多线程问题 3.6 一个综合实例

    Learning-working-skill

    学习工作技能 2021.01.04-2021.01.07 Git学习 2021.01.10 内部匿名类,Lambda表达,Streamstream 2021.01.17 文件类,字节流,字节缓冲流 ... Servlet,ServletConfig,ServletContext,Servlet注解开发

    J2EE中文版.chm

    java开发web的帮助文档,内含Cookie ...ServletConfig ServletContext ServletException ServletInputStream ServletOutputStream ServletRequest ServletResponse SingleThreadModel UnavailableException

    Servlet+API中文API[China]

    Servlet处理请求、多线程和映射、Servlet的卸载、Servlet映射技术、通过类名调用Servlet、HTTP会话、建立Session、Request Dispatcher接口、Servlet接口、ServletConfig接口、ServletContext接口、SingleThreadModel...

    JavaWeb中Servlet技术

    主要包括Servlet基础和 ServletConfig接口,ServletContext接口两大部分; javaweb,servlet,接口,编写类; 初学者,大学生,复习,详细,课堂笔记;

    JAVA J2EE 类库文档

    ServletContext ServletContextAttributeEvent ServletContextAttributeListener ServletContextEvent ServletContextListener ServletException ServletInputStream ServletOutputStream ServletRequest ...

    学习servlet的实例和参考api

    类 ServletConfig ServletOutputStream ServletContext ServletInputStream ServletResponse GenericServlet ServletRequest Servlet <br>javax.servlet.http<br>接口 类 HttpSession Cookie...

    servletapi

    Servlet处理请求、多线程和映射、Servlet的卸载、Servlet映射技术、通过类名调用Servlet、HTTP会话、建立Session、Request Dispatcher接口、Servlet接口、ServletConfig接口、ServletContext接口、SingleThreadModel...

    JSP 从配置文件获取参数详解

    这一节将介绍 ServletContext 和 ServletConfig 这两个接口。 通过这两个接口,我们可以在web.xml中设置一些参数,如数据库地址、用户名密码等,供 Servlet 使用, 这样每次参数值变动时,不用重新编译Servlet了。 ...

    JavaWeb的三大组件详细总结

    1、ServletConfig API:2、GenericServlet类2、HttpServlet类:(重点掌握)1、doGet和doPost的区别:2、Servlet细节:3、web.xml文件(了解)4、ServletContext(重要):3、网站访问量统计小案例:二、过滤器三、监听...

    深入体验Java Web开发内幕

    4.1 Servlet的有关概念与前置知识  4.2 Servlet开发入门  4.3 Servlet的特点及运行过程  4.4 ServletConfig接口  4.5 GenericServlet与HttpServlet类  4.6 ServletContext接口  4.7 思考与...

Global site tag (gtag.js) - Google Analytics