1、ServletContext的概述

  • 一个项目只有一个ServletContext对象!application
  • 我们可以在N多个Servlet中获取这个唯一的对象,使用它来给多个Servlet传递数据!
  • 这个对象在Toncat启动时就创建,在Toncat关闭时才死去!

2、获取ServletContext对象

  • ServletConfig ### getSetvletContext()
  • GenericServlet ## getServletContext()
  • HttpServlet ## getServletContext()
  • ServletContextEvent ## getServletContext()
 public abstract class MyServlet implements Servlet {

     public void init(ServletConfig config) {
ServletContext context = config.getServletContext();
} }
 public class MyServlet02 extends HttpServlet{

     protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
ServletContext context = this.getServletContext();
} }

3、 域对象的功能

  1. void setAttribute(String name,Object object)  用来储存一个对象,也可以说用来储存一个属性,例如,servletContext.setAttribute("xxx",XXX); 在ServletContext就保存了一个域属性,域属性名为xxx,域属性值为XXX。与Map相同。
  2. ObjectgetAttribute(String name)  通过一个域属性名取得域属性值,如String value=(String)servletContext.getAttribute("xxx");
  3. EnumerationgetAttributeNames()   取得所有域属性名,是所有,不是一个
  4. void removeAttribute(String name)    用来移除指定name的域属性值,如果域属性值name不存在 ,则该方法什么也不做

下面举个例子来说明第一第二种功能

 public class AServlet extends HttpServlet {

     protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
ServletContext application = this.getServletContext();
application.setAttribute("name", "123");
}
}
public class BServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { ServletContext application=this.getServletContext();
String value=(String) application.getAttribute("name");
System.out.println("通过域属性名取得域属性值 :"+value);
}
}

启动服务器后,执行AServlet,再执行BServlet 后可以看到如下结果

4、 获取应用初始化参数

  • Servlet 也可以获取初始化参数,但它是局部的参数;也就是说,一个Servlet只能获取自己的初始化参数,不能获取别人的初始化参数,即初始化参数只为一个Servlet准备
  • 也可以配置公共的初始化参数,为所有的Srevlet 而用,这需要使用ServletContext才能使用!

如配置公共的初始化参数,首先要先配置web.xml文件

再来新建一个CServlet.java

package org.lxh.serletdemo;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet; public class CServlet extends HttpServlet { public void init() throws ServletException {
ServletContext application =this.getServletContext();
String value=(String)application.getInitParameter("context-param");
System.out.println(value);
}
}

启动服务器后,再执行CServlet可以看到如下结果

5、 获取相关资源方法

  1. 获取真实路径(******)
  2. 获取资源流
  3. 获取指定目录下的所有资源路径

1、可以使用ServletContext 对象来获取Web应用下的资源,例如在web目录下有个input.html文件,现在想在servlet中获取这个资源,就可以通过ServletContext来获取

 package org.lxh.serletdemo;

 import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class DServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletContext application=this.getServletContext(); //获取ServletContext对象
String realPath=application.getRealPath("input.html"); //通过getRealPath得到真实路径
System.out.println(realPath); //打印路径
}
}

2. 获取资源流

 package org.lxh.serletdemo;

 import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Set; import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class DServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletContext application=this.getServletContext();
InputStream input =application.getResourceAsStream("input/jsp");//获取资源路径后,再创建出输入流对象 }
}

3. 获取指定目录下的所有资源路径,如输出WEB-INF下 的所有路径

 package org.lxh.serletdemo;

 import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Set; import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class DServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletContext application=this.getServletContext(); //获取ServletContext对象
Set<String > paths=application.getResourcePaths("/WEB-INF");
System.out.println(paths);
}
}

Servlet----------ServletContext (重要)的更多相关文章

  1. 【原】tomcat 7 启动报错:java.lang.NoSuchMethodError: javax.servlet.ServletContext.getSessionCookieConfig()Ljavax/servlet/SessionCookieConfig的解决

    现象: tomcat 7 启动报错:java.lang.NoSuchMethodError: javax.servlet.ServletContext.getSessionCookieConfig() ...

  2. tomcat7 启动项目报错 java.lang.NoSuchMethodError: javax.servlet.ServletContext.getSessionCookieConfig()

    JDK版本:jdk1.8.0_77 Tomcat 版本:apache-tomcat-7.0.47 异常重现步骤: 1.完成项目部署 2.启动Tomcat 异常头部信息:java.lang.NoSuch ...

  3. java.lang.NoClassDefFoundError: javax/servlet/ServletContext

    方法1:把SpringBoot中main方法所在的class不再继承org.springframework.boot.context.web.SpringBootServletInitializer即 ...

  4. tomcat 7 启动报错:java.lang.NoSuchMethodError: javax.servlet.ServletContext.getSessionCookieConfig()Ljavax/servlet/SessionCookieConfig的解决

    现象: tomcat 7 启动报错:java.lang.NoSuchMethodError: javax.servlet.ServletContext.getSessionCookieConfig() ...

  5. SpringBoot报错:java.lang.NoSuchMethodError: javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String;

    错误:java.lang.NoSuchMethodError: javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String ...

  6. Caused by: java.lang.NoSuchMethodError: javax.servlet.ServletContext.getClassLoader()Ljava/lang/ClassLoader;

    运行tomat  报错: Caused by: java.lang.NoSuchMethodError: javax.servlet.ServletContext.getClassLoader()Lj ...

  7. Spring boot Unable to start embedded Tomcat报错 java.lang.NoSuchMethodError: javax.servlet.ServletContext.getVirtualServerName()

    Spring boot Unable to start embedded Tomcat 报错 java.lang.NoSuchMethodError: javax.servlet.ServletCon ...

  8. Spring boot ---- java.lang.NoClassDefFoundError: javax/servlet/ServletContext

    Spring boot ---- java.lang.NoClassDefFoundError: javax/servlet/ServletContext   场景描述 项目中用到spring boo ...

  9. java.lang.NoSuchMethodError: javax.servlet.ServletContext.getContextPath()Ljava/lang/String;

    问题描述:在eclipse3.7中启动tomcat6时一直出现这个错误, java.lang.NoSuchMethodError: javax.servlet.ServletContext.getCo ...

  10. eclipse导入web项目报错 Cannot find the class file for javax.servlet.ServletContext.

    当eclipse中新导入的Java Project的时候,往往会碰到各种各样的问题,下面是个典型的问题: Cannot find the class file for javax.servlet.Se ...

随机推荐

  1. 用Nginx做静态文件的CDN

    这是上个月一次搭建多个静态文件节点的实践,转载自我的博客,欢迎交流. 鉴于监管环境和网站速度之间的矛盾,目前的网络架构方式如下:1.web动态页面(含数据库)架设在位于美国西海岸的数据中心:2.静态文 ...

  2. java Web 启动时自动执行代码的几种方式

    Web容器启动后执行代码的几种方式,其执行顺序为:4===>5===>1===>2===>3,即指定init-method的Bean开始执行,接着实现spring的Bean后置 ...

  3. 二、K3 WISE 开发插件《 工业单据老单客户端插件事件、属性、方法》

    ===================== 目录: 1.插件事件说明如下 2.插件属性说明如下 3.插件方法说明如下 ===================== 1.插件事件说明如下:  序号 事   ...

  4. 【大数据系列】windows下连接Linux环境开发

    一.配置文件 1.core-site.xml <configuration> <property> <name>fs.defaultFS</name> ...

  5. jade(pug)学习笔记(待填充.......)

    深刻认识到总结知识点的重要性,不然遇到似曾相识的问题,要翻老半天的百度才能解决.20171018 pug——文字内部嵌入结构 比如: <a class = "href"> ...

  6. ThinkPad L421 如何进入BIOS?(已解决)

    开机屏幕出现ThinkPad标志时,快速按下 F1键 即可进入BIOS

  7. sencha touch 在线实战培训 第一期 第五节

    2014.1.6晚上8点准时开的课 本期培训一共八节,前三堂免费,后面的课程需要付费才可以观看. 本节内容:             实现皮肤自定义样式的修改 css相关理论: 重写官方样式,美化ap ...

  8. jenkins Email-ext plugin插件中Pre-send Script设置说明

    在使用jenkins Email-ext plugin发送邮件时,项目中使用了SVN去同步,发现每次有同步,都会发送邮件,现只想SVN只更新,不发送邮件通知,这就要在Pre-send中做修改 看看官网 ...

  9. 遮挡剔除 Occlusion Culling(转)

    一.首先介绍下draw call(这个东西越少你的游戏跑的越快): 在游戏中每一个被展示的独立的部分都被放在了一个特别的包中,我们称之为“描绘指令”(draw call),然后这个包传递到3D部分在屏 ...

  10. iOS - 引用计数探讨

    <Objective-C 高级编程> 这本书有三个章节,我针对每一章节进行总结并加上适当的扩展分享给大家.可以从下面这张图来看一下这三篇的整体结构: 注意,这个结构并不和书中的结构一致,而 ...