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. ArcGIS for android访问天地图

    底图采用Web Mercator投影坐标系 获取元数据信息:http://t0.tianditu.com/img_w/wmts?SERVICE=WMTS&REQUEST=GetCapabili ...

  2. SCCM 2012 R2实战系列之二:前提工作准备

    在上一篇中,我们完成了SQL Server 2012的安装和配置.现在跟大家分享SCCM安装前的准备工作. 2.1 SCCM 2012 R2 准备工作 2.1.1 创建并分配System Manage ...

  3. Python多进程vs多线程

    多任务的两种方式:多进程和多线程. 如果用多进程实现Master-Worker,主进程就是Master,其他进程就是Worker. 如果用多线程实现Master-Worker,主线程就是Master, ...

  4. Android重打包+重新签名工具Apktool Box

    可实现apk反编译+重新打包+重新签名,界面如下 : 部分引用自开源代码:http://github.com/Bu4275/AutoAPKTool

  5. 基于IDEA环境下的Spark2.X程序开发

    我们选择在线安装 这个是windows下的scala,直接双击安装就可以了 安装好之后可以验证一下 这个是我本地的jdk1.8安装包,直接双击安装 安装完成后可以验证一下 https://archiv ...

  6. CS229 6.18 CNN 的反向传导算法

    本文主要内容是 CNN 的 BP 算法,看此文章前请保证对CNN有初步认识. 网络表示 CNN相对于传统的全连接DNN来说增加了卷积层与池化层,典型的卷积神经网络中(比如LeNet-5 ),开始几层都 ...

  7. python脚本参数传递

    环境:python 库:optparse 效果:python  xxx.py  -parameter1  参数1  -parameter2 参数2 .... 废话少说,直接上代码ok?  xxx.py ...

  8. What Your Computer Does While You Wait.CPU的等待有多久?

    原文标题:What Your Computer Does While You Wait 原文地址:http://duartes.org/gustavo/blog/ [注:本人水平有限,只好挑一些国外高 ...

  9. python return 返回多个值

    python return 返回多个值,实际返回的是一个tuple: #!/usr/bin/env python # -*- coding: utf-8 -*- def rt(): return 1, ...

  10. uva-387-暴力枚举

    题意: 给你一些小方块,问是不是能组成一个4X4的大方块,所有方块全部要使用,裸枚举 #include <iostream> #include <stdio.h> #inclu ...