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(全局上下文).[例子 ...
随机推荐
- 在bootstrap的column中的formatter里不能传递row参数吗?
row 是一个对象 你需要把它转成字符串用JSON.stringify(row),这样就是字符串了,但是还有问题,你需要给加密一下encodeURI(JSON.stringify(row)),然后方 ...
- UI自动化测试的Page Object模式
在UI级的自动化测试框架中,当页面样式改变或者页面元素属性改变,那么代码也要随之进行修改,如何做到高效快速的修改代码来适应这些改变呢,这个时候可以引入Page Object模式,也是页面对象设计模式. ...
- BK: Data mining: concepts and techniques (1)
Chapter 1 data mining is knowledge discovery from data; The knowledge discovery process is an iterat ...
- Vue.js 源码构建(三)
Vue.js 源码是基于 Rollup 构建的,它的构建相关配置都在 scripts 目录下. 构建脚本 通常一个基于 NPM 托管的项目都会有一个 package.json 文件,它是对项目的描述文 ...
- JS高级---正则表达式练习身份证号码
写正则表达式, 根据字符串来写正则表达式进行匹配 经验: 1.找规律 2.不要追求完美 身份证的正则表达式 15位或者18位 ([1-9][0-9]{14})|([1-9][0-9]{16}[0 ...
- spring security和java web token整合
思路: spring security 1.用户输入用户名密码. 2.验证:从库中(可以是内存.数据库等)查询该用户的密码.角色,验证用户名和密码是否正确.如果正确,则将填充Authenticatio ...
- HandlerMethod解析
api写HandlerMethod的作用: Encapsulates information about a handler method consisting of a method and a b ...
- C#中常见的winform控件命名规范 转
我们知道Button 常常简称为btn,那么Winform中的其它控件呢,这篇文章在C#的winform控件命名规范 的基础上对一些控件的名称的简称进行了整理. 1. 标准控件 NO. 控件类型简写 ...
- Codeforces Round #600 (Div. 2) D。 Harmonious Graph
#include<iostream> using namespace std ; ; int p[N]; int cnt; int find(int x) { if(p[x]!=x) p[ ...
- 记manjaro图形驱动删除后的一次补救
#一.前言 众所周知,NVIDIA的闭源驱动在Linux上的兼容性不是很好,再加上我不玩游戏,于是我就想卸载独显只留核显.我以为我装了独显和核显两种驱动,原本想直接删除独显驱动,没想到删除的是bumb ...