1)获取web上下文路径

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取ServletContext对象
//this.getServletConfig().getServletContext();
//等同于下面一句,因为创建getServletContext必须要通过getServletConfig对象
ServletContext context = this.getServletContext(); //获取web的上下文路径,
String path = context.getContextPath(); //请求重定向,这样的好处可以让获取的路径更加灵活。不用考虑项目名是否发生了变化。
response.sendRedirect(context.getContextPath()+"/index.jsp");
}
}

2)获取全局参数

public class ServletContextDemo1 extends HttpServlet {
/**
* 获取全局参数
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletContext context = this.getServletContext();
//根据参数名获取参数值
System.out.println(context.getInitParameter("MMM"));
//获取所有的参数名,返回枚举类型
Enumeration<String> emn = context.getInitParameterNames();
while(emn.hasMoreElements()){
String paramName = emn.nextElement();
String paramValue = context.getInitParameter(paramName);
System.out.println(paramName+"="+paramValue);
}
} }

3)和域相关

域:域对象在不同的资源之间来共享数据,保存数据,获取数据。

这个我使用了三个Servlet来说明这个问题,ScopeDemo1用于获取Attribute,ScopeDemo2用于设置Attribute,ScopeDemo3用于删除Attribute。

保存共享数据:

public class ScopeDemo2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//首先创建ServletContext对象
ServletContext context = this.getServletContext();
//保存共享数据
context.setAttribute("name", "zhangsan");//第一个参数为字符串,第二个是Object(也就是任意类型)
System.out.println("设置成功"); } }

获取恭喜数据:

public class ScopeDemo1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//创建ServletContext对象
ServletContext context = this.getServletContext();
//获取共享数据内容
String name = (String)context.getAttribute("nnn");
System.out.println(name);
}
}

删除共享数据:

public class ScopeDemo3 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取ServletContext对象
ServletContext context = this.getServletContext();
//删除共享数据
context.removeAttribute("name");
System.out.println("删除成功");
}
}

Servlet之ServletContext获取web上下文路径、全局参数、和Attribute(域)的更多相关文章

  1. java ,js获取web工程路径

    一.java获取web工程路径 1),在servlet可以用一下方法取得: request.getRealPath(“/”) 例如:filepach = request.getRealPath(“/” ...

  2. 获取web应用路径 // "/" 表示class 根目录

    /** * 获取web应用路径 * @Description : 方法描述 * @Method_Name : getRootPath * @return * @return : String * @C ...

  3. java中获取各种上下文路径的方法小结

    一.获得都是当前运行文件在服务器上的绝对路径在servlet里用:this.getServletContext().getRealPath(); 在struts用:this.getServlet(). ...

  4. 【转】Spring 获取web根目录 (Spring线程获取web目录/路径/根目录,普通类获取web目录)

    不使用Spring,怎样能在Listener启动的Thread中获取web目录,还真不完全确定.其实我觉得实际代码也很简单.就是基于普通的listener,然后在listener中获取web目录并放到 ...

  5. Java获取web项目路径

    File f = new File(WebPath.class.getResource("/").getPath()); String path = f.getParentFile ...

  6. 获取web服务器路径的方法 getResourceAsStream

    1.先获取 serlvetContext对象 2.调用getResourceAsStream  在方法里 "\"表示当前web的根目录  还要拼接上具体的文件路径 ServletC ...

  7. ServletContext获取项目真实路径

    import javax.servlet.ServletContext; import org.springframework.web.context.ServletContextAware; /** ...

  8. JS 获取WEB请求路径

    function getRealPath(){      //获取当前网址,如: http://localhost:8083/myproj/view/my.jsp       var curWwwPa ...

  9. Servlet中如何获取用户提交的查询参数或表单数据?

    ①HttpServletRequest的getParameter()方法. ②HttpServletRequest的getParameterValues()方法. ③HttpServletReques ...

随机推荐

  1. CentOS安装nginx以及负载均衡的搭建

    依赖环境,没有安装的需要安装一下 yum install gcc yum install pcre-devel yum install zlib zlib-devel yum install open ...

  2. 第15课 右值引用(2)_std::move和移动语义

    1. std::move (1)std::move的原型 template<typename T> typename remove_reference<T>::type& ...

  3. Linux后台有个systemd-r进程,占用5355等端口

    编辑配置文件 vim /etc/systemd/resolved.conf 设置LLMNR=0 重启服务: systemctl restart systemd-resolved.service

  4. C#语言正则用法

    string phone =""; string pattern @"|\d{10}"; bool rusurt = false; Console.WriteL ...

  5. 数组.html

    <script > var arr1 = [1, 2, 3, 4, 5, 6 ]; 赋值 var arr2 =Array(1,2,3,4,5,6); var arr3 = new Arra ...

  6. (转)OpenSystemArchitect - 根据数据库表逆向生成数据模型

    原文地址:http://www.cnblogs.com/zhaojin/archive/2011/04/14/2016478.html OpenSystemArchitect - 根据数据库表逆向生成 ...

  7. Mysql的资源(安装版+解压版)

    Mysql的安装(安装版+解压版)! 首先有几个比较好的参考,mysql官网,菜鸟教程: (1)安装版 安装版资源,SQLyog-8.32(sn)(可视化工具), 安装过程基本和安装其它程序没有什么大 ...

  8. .Net 大型分布式基础服务架构横向演变概述(转)

    一. 业务背景 构建具备高可用,高扩展性,高性能,能承载高并发,大流量的分布式电子商务平台,支持用户,订单,采购,物流,配送,财务等多个项目的协作,便于后续运营报表,分析,便于运维及监控. 二. 基础 ...

  9. Python并发编程-事件驱动模型

     一.事件驱动模型介绍                                                                                         ...

  10. Linux架构分布式集群之基础篇

    部署linux环境,安装jdk 1.安装rar命令行 wget http://www.rarlab.com/rar/rarlinux-x64-4.2.0.tar.gz由于在此目录下解压rar后linu ...