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. 解决在word中不能使用输入法

    打开一个Word文档-------------->单击 文件---->选项---->高级---->取消什么?看图 然后重新启动word

  2. python的json模块介绍

    转载:https://blog.csdn.net/xsj_blog/article/details/51921664 对于数据传递方面,XML是一种选择,还有一种选择是JSON,它是一种轻量级的数据交 ...

  3. 10 Skills Every SharePoint Developer Needs

    10 Skills Every SharePoint Developer Needs(原文) This blog post guides you through the essential skill ...

  4. CentOS之RPM

    yum工具比RPM工具好用,所以直接介绍yum工具来管理RPM包. yum list |head -n 20 列出所有RPM资源. yum search vim  搜索RPM包vim yum inst ...

  5. 通过mysqlclient操作MySQL数据库

    一,安装mysql 如果是windows 用户,mysql 的安装非常简单,直接下载安装文件,双击安装文件一步一步进行操作即可. Linux 下的安装可能会更加简单,除了下载安装包进行安装外,一般的l ...

  6. JavaWeb项目中web.xml有关servlet的基本配置

    JavaWeb项目中web.xml有关servlet的基本配置: 我们注意到,tomcat下的conf中也有一个web.xml文件,没错的,所有的JavaWeb项目中web.xml都继承自服务器下的w ...

  7. HBase分布式集群部署与设计

    先是把cdh版本的hbase上传上来 把安装吧的权限设置一下 解压 把没用的东西干掉 hbase的官网 配置文件 到hbase-site.xml 下面我不基于HA配置了 把hbase分发到其他两个节点 ...

  8. 自动化工具gulp搭建环境(详解)

    src:读取文件和文件夹       dest:生成文件(写文件)       watch:监控文件       task:定制任务         pipe:以流的方式处理文件 bower的安装和使 ...

  9. dubbo-文档

    Srping版Dubbo集成中文地址:https://dubbo.gitbooks.io/dubbo-user-book/content/preface/background.html SpringB ...

  10. js-杂记

    js可计算传值 <p>点击按钮计算 x 的值.</p> <button onclick="myFunction()">点击这里</butt ...