ServletContext:

ServletContext表示Servlet应用程序。每个Web应用程序只有一个上下文。在将一个应用程序同时部署到多个容器的分布式环境中,每台Java虚拟机上的Web应用都会有一个ServletContext对象。

通过在ServletConfig中调用getServletContext方法,可以获得ServletContext。

有了ServletContext,就可以共享从应用程序中的所有资料处访问到的信息,并且可以动态注册Web对象。前者将对象保存到ServletContext中的一个内部的Map中。保存在ServletContext中的对象被称作属性。

1:实现数据共享 setAttribute(key,value)  getAttribute(key) ,获取域对象

2:获取全局配置信息: getInitParameter() 

3:获得应用下任何资源的路径

例一:通过调用GenericServlet的 getServletContext对象得到ServletContext对象,获取域对象

先运行myservlet1.java,在运行myservlet2.java

myservlet1.java

public class myservlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//通过调用GenericServlet的 getServletContext对象得到ServletContext对象
//先访问demo1,在访问demo2
ServletContext application=this.getServletContext();
application.setAttribute("name", "tom");

System.out.println(application.getClass().getName());
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}

myservlet2.java

public class myservlet2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name=(String) this.getServletContext().getAttribute("name"); if(name==null){
System.out.println("你不能直接访问这个类");
}
System.out.println(name);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}

  运行效果图:

例二:获取全局配置信息

获取encoding的配置信息

myservlet3.java

public class myservlet3 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String encoding=this.getServletContext().getInitParameter("encoding");
System.out.println(encoding);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}

 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<context-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</context-param>
<servlet>
<servlet-name>myservlet</servlet-name>
<servlet-class>com.zk.myservlet.myservlet</servlet-class>
</servlet>
<servlet>
<servlet-name>myservlet2</servlet-name>
<servlet-class>com.zk.myservlet.myservlet2</servlet-class>
</servlet>
<servlet>
<servlet-name>myservlet3</servlet-name>
<servlet-class>com.zk.myservlet.myservlet3</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myservlet</servlet-name>
<url-pattern>/demo1</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>myservlet2</servlet-name>
<url-pattern>/demo2</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>myservlet3</servlet-name>
<url-pattern>/demo3</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

例三:获取资源路径

使用this.getServletContext().getRealPath获取文件的资源路径

public class myservlet1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String apath=this.getServletContext().getRealPath("/WEB-INF/a.properties");//参数一定要以斜杠开头
System.out.println(apath);
//创建一个properties
Properties pro=new Properties();
pro.load(new FileInputStream(apath));
System.out.println(pro.getProperty("akey")); String bpath=this.getServletContext().getRealPath("/WEB-INF/classes/b.properties");
System.out.println(bpath);
Properties pro2=new Properties();
pro2.load(new FileInputStream(bpath));
System.out.println(pro2.getProperty("key")); String cpath=this.getServletContext().getRealPath("/WEB-INF/classes/com/zk/myservlet/c.properties");
System.out.println(cpath);
Properties pro3=new Properties();
pro3.load(new FileInputStream(cpath));
System.out.println(pro3.get("ckey"));
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}

  运行结果图:

ServletContext的使用的更多相关文章

  1. JavaWeb——ServletContext

    一.基本概念 说起ServletContext,一些人会产生误解,以为一个servlet对应一个ServletContext.其实不是这样的,事实是一个web应用对应一个ServletContext, ...

  2. The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory

    The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory 这是由于项目里面的一些 ...

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

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

  4. 如何获得Webapp的根项目路径 即ServletContext.getRealPath() 的输入参数要以"/"开头

    ServletContext.getRealPath() 的输入参数要以"/"开头 2014-03-26 15:54 5738人阅读 评论(1) 收藏 举报 版权声明:本文为博主原 ...

  5. 重温Servlet学习笔记--servletContext对象

    一个项目中只有一个ServletContext对象,我们可以在多个servlet中获取这个唯一的对象,使用它可以给多个servlet传递数据,我们通常成servletContext为上下文对象.这个对 ...

  6. Servlet 之 ServletContext

    package cn.jiemoxiaodi.servlet_servletcontext; import java.io.IOException; import java.io.PrintWrite ...

  7. Tomcat启动后,从spring容器中获取Bean和ServletContext

    public static Object getBean(String beanName){ ApplicationContext context = ContextLoader.getCurrent ...

  8. The method getJspApplicationContext(ServletContext) is undefined for the type

    type Exception report message Unable to compile class for JSP: description The server encountered an ...

  9. 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 ...

  10. Spring以及SPringmvc相关问题: ServletContext -父子容器

    总结如下: 明确了Servlet规范中ServletContext的作用和意义.此外明确一个Tomcat中多个web应用,每个人web应用有唯一的一个ServletContext(全局上下文).[例子 ...

随机推荐

  1. centos 7 源码安装openresty

    Openresty 官网 http://openresty.org Openresty源码下载页面 http://openresty.org/en/download.html Openresty 简易 ...

  2. 使用Image Sharp 对二维码图片中间加入logo图片

    1.在NuGet包源加入Image Sharp包源 2.在需要用到Image Sharp的项目中安装以下的插件 3.调用图片上绘制图片的方法

  3. java 学习(day2) 时钟类

    看过python的面向对象,所以入手java总的来说还是比较快的. 看视频学习之后写了一个简单的时钟 一个时钟有两部分组成小时和分钟.再大一些还有月,日. 分析一下就是,上述这些属性到达一个值之后,就 ...

  4. JS高级---案例:验证用户输入的是不是中文名字

    案例:验证用户输入的是不是中文名字 [\u4e00-\u9fa5] <!DOCTYPE html> <html lang="en"> <head> ...

  5. 3ds Max File Format (Part 3: The department of redundancy department; Config)

    Now we'll have a look at the Config stream. It begins like follows, and goes on forever with various ...

  6. bugku 这么多数据包

    看到之后有点懵逼 然后下载 下载之后发现是一个pacp后缀的流量数据包 然后用wireshark 然后只想到了 http过滤 然后发现不对 然后参考其他人的博客 经大佬提示, 一般 getshell ...

  7. H5与native 普及

    H5与native 普及: H5是基于web,native基于客户端native是使用原生系统内核的,相当于直接在系统上操作.,是我们传统意义上的软件,更加稳定.但是H5的APP先得调用系统的浏览器内 ...

  8. vue 3.0 项目搭建移动端 (七) 安装Vant

    # 通过 npm 安装 npm i vant -S 安装完配置 babel.config.js module.exports = { presets: ['@vue/app'], plugins: [ ...

  9. zabbix4.2配置监控TCP连接状态

    1.使用命令查看TCP连接状态 (1)过去常用命令:netstat -antp [root@ansible-control zabbix]# netstat -antp Active Internet ...

  10. Spectral clustering谱聚类

    Basic knowledge: degree matrix; similarity matrix, and Adjacency matrix; 无向带权图模型 G=<V,E>G=< ...