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. Linux内核0.11代码阅读(转)

    最近决定开始阅读Linux 0.11的源代码. 学习Linux操作系统的核心概念最好的方法莫过于阅读源代码.而Linux当前最新的源代码包已经有70MB左右,代码十分庞大,要想深入阅读十分困难.而Li ...

  2. 用MyEclipse远程debug

    第一步 编辑 tomcat下的文件startup.sh文件,我的路径是 /root/apache-tomcat-6.0.24/bin/startup.sh 命令:vim startup.sh将decl ...

  3. eclipse中tomcat正常启动,但浏览器访问不了tomcat首页之tomcat配置问题

    症状: tomcat在eclipse里面能正常启动,而在浏览器中访问http://localhost:8080/不能访问,且报404错误.同时其他项目页面也不能访问. 关闭eclipse里面的tomc ...

  4. ubuntu Redis安装及配置

    1.安装 1.1 下载压缩包:wget http://download.redis.io/releases/redis-5.0.4.tar.gz1.2 解压:tar xzf redis-5.0.4.t ...

  5. android 根据坐标返回触摸到的View

    //根据坐标返回触摸到的Viewprivate View getTouchTarget(View rootView, int x, int y) { View targetView = null; / ...

  6. 洛谷P1223 排队接水

    https://www.luogu.org/problem/P1223 #include<bits/stdc++.h> using namespace std; struct st { i ...

  7. mysql数据库-笔记

    基本概念篇 SQL语言的分类(DDL.DML.DCL.DQL) 对应的英文全程:data (definition.manipulation.control.query)language 参考资料: h ...

  8. K3修改字段名

    在K3的BOS中,自定义字段之后我们往往会修改字段名,便于记忆和理解,但是修改字段名之后,只是数据库中的字段名被修改了,BOS中的字段标识并没有被修改,可以通过以下语句将标识和字段名改成一致. sel ...

  9. 【填坑】python3 manage.py migrate:?: (mysql.W002) MySQL Strict Mode is not set for database connection 'default'

    问题: WARNINGS:?: (mysql.W002) MySQL Strict Mode is not set for database connection 'default'        H ...

  10. Git-免密提交

    全局设置git免密提交,打开git-bash输入命令: git config credential.helper store --global 单独对某个项目仓库设置时不加  --global 设置之 ...