ServletContext的使用
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的使用的更多相关文章
- JavaWeb——ServletContext
一.基本概念 说起ServletContext,一些人会产生误解,以为一个servlet对应一个ServletContext.其实不是这样的,事实是一个web应用对应一个ServletContext, ...
- The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory
The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory 这是由于项目里面的一些 ...
- 【原】tomcat 7 启动报错:java.lang.NoSuchMethodError: javax.servlet.ServletContext.getSessionCookieConfig()Ljavax/servlet/SessionCookieConfig的解决
现象: tomcat 7 启动报错:java.lang.NoSuchMethodError: javax.servlet.ServletContext.getSessionCookieConfig() ...
- 如何获得Webapp的根项目路径 即ServletContext.getRealPath() 的输入参数要以"/"开头
ServletContext.getRealPath() 的输入参数要以"/"开头 2014-03-26 15:54 5738人阅读 评论(1) 收藏 举报 版权声明:本文为博主原 ...
- 重温Servlet学习笔记--servletContext对象
一个项目中只有一个ServletContext对象,我们可以在多个servlet中获取这个唯一的对象,使用它可以给多个servlet传递数据,我们通常成servletContext为上下文对象.这个对 ...
- Servlet 之 ServletContext
package cn.jiemoxiaodi.servlet_servletcontext; import java.io.IOException; import java.io.PrintWrite ...
- Tomcat启动后,从spring容器中获取Bean和ServletContext
public static Object getBean(String beanName){ ApplicationContext context = ContextLoader.getCurrent ...
- The method getJspApplicationContext(ServletContext) is undefined for the type
type Exception report message Unable to compile class for JSP: description The server encountered an ...
- 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 ...
- Spring以及SPringmvc相关问题: ServletContext -父子容器
总结如下: 明确了Servlet规范中ServletContext的作用和意义.此外明确一个Tomcat中多个web应用,每个人web应用有唯一的一个ServletContext(全局上下文).[例子 ...
随机推荐
- mysql 数据库基础操作
一 知识储备 MySQL数据库基本操作知识储备 数据库服务器:一台计算机(对内存要求比较高) 数据库管理系统:如mysql,是一个软件 数据库:oldboy_stu,相当于文件夹 表:student, ...
- .NET知识梳理——4.特性Attribute
1. 特性 1.1 特性Attribute 特性就是一个类,继承自Attribute抽象类(该类无抽象方法.避免实例化),约定俗成用Attribute类结尾,标记时可省略掉Attribu ...
- JavaScript的严格检查模式
JavaScript的严格检查模式 前提:IDEA设置为ECMAScript 6语法. 'use strict':严格检查模式,用来预防JS的随意性导致的问题. 比如:直接 i=1;这样定义成了全局变 ...
- HTML的表单标签汇总
HTML的表单标签汇总 表单的元素格式: 1. 账号.密码.提交.重置 语法: <p>账号:<input type="text" name="usern ...
- Python入门5 —— 基本运算符
1.算数运算符('+'.'-'.'*'.'/'.'//'.'%'.'**') print(10 + 3) -- 输出:13 print(10 - 3) -- 输出:7 print(10 * 3) -- ...
- 题解【洛谷P1314】[NOIP2011]聪明的质监员
题面 题解 不难发现,\(W\)增大时,\(Y\)值会随之减小. 于是考虑二分\(W\). 如何\(\mathcal{O}(N)check?\) 每一次前缀和记录一下\(1-i\)之间\(w_i \g ...
- 获取 Android APP 版本信息工具类(转载)
获取 Android APP 版本信息工具类 获取手机APP版本信息工具类 1.获取版本名称 2.获取版本号 3.获取App的名称 package com.mingyue.nanshuibeidiao ...
- 第一篇 网站基础知识 第5章 自己动手实现HTTP协议
第5章 自己动手实现HTTP协议 我们知道HTTP协议是在应用层解析内容的,只需要按照它的报文的格式封装和解析数据就可以了,具体的传输还是使用的Socket,在第4章NioServer的基础上自己做一 ...
- java.awt.Font
显示效果 Font mf = new Font(String 字体,int 风格,int 字号);字体:TimesRoman, Courier, Arial等风格:三个常量 lFont.PLAIN, ...
- Carmichael Numbers (快速幂)
当今计算机科学的一个重要的领域就是密码学.有些人甚至认为密码学是计算机科学中唯一重要的领域,没有密码学生命都没有意义. 阿尔瓦罗就是这样的一个人,它正在设计一个为西班牙杂烩菜饭加密的步骤.他在加 ...