Servlet之ServletContext获取web上下文路径、全局参数、和Attribute(域)
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(域)的更多相关文章
- java ,js获取web工程路径
一.java获取web工程路径 1),在servlet可以用一下方法取得: request.getRealPath(“/”) 例如:filepach = request.getRealPath(“/” ...
- 获取web应用路径 // "/" 表示class 根目录
/** * 获取web应用路径 * @Description : 方法描述 * @Method_Name : getRootPath * @return * @return : String * @C ...
- java中获取各种上下文路径的方法小结
一.获得都是当前运行文件在服务器上的绝对路径在servlet里用:this.getServletContext().getRealPath(); 在struts用:this.getServlet(). ...
- 【转】Spring 获取web根目录 (Spring线程获取web目录/路径/根目录,普通类获取web目录)
不使用Spring,怎样能在Listener启动的Thread中获取web目录,还真不完全确定.其实我觉得实际代码也很简单.就是基于普通的listener,然后在listener中获取web目录并放到 ...
- Java获取web项目路径
File f = new File(WebPath.class.getResource("/").getPath()); String path = f.getParentFile ...
- 获取web服务器路径的方法 getResourceAsStream
1.先获取 serlvetContext对象 2.调用getResourceAsStream 在方法里 "\"表示当前web的根目录 还要拼接上具体的文件路径 ServletC ...
- ServletContext获取项目真实路径
import javax.servlet.ServletContext; import org.springframework.web.context.ServletContextAware; /** ...
- JS 获取WEB请求路径
function getRealPath(){ //获取当前网址,如: http://localhost:8083/myproj/view/my.jsp var curWwwPa ...
- Servlet中如何获取用户提交的查询参数或表单数据?
①HttpServletRequest的getParameter()方法. ②HttpServletRequest的getParameterValues()方法. ③HttpServletReques ...
随机推荐
- [UE4]枚举Enum和Switch Enum
- 高通9X07模块QMI架构使用入门
QMI(Qualcomm Message Interface) 高通用来替代OneRPC/DM的协议,用来与modem通信.本文是摸索高通QMI机制一点经验,重点解读了如果建立拨号连接,仅供参考.qm ...
- U3D学习09-物体简单控制及视角观察
一.Character Control非刚体 1.场景初始化,注意调整CC的轴心,不需要碰撞,且删除CC子物体的碰撞.2.移动: 获取X,Z轴变化,定义变量h,v: 定义移动 ...
- Spring IoC中各个注解的理解和使用
一.把在Spring的xml文件中配置bean改为Spring的注解来配置bean 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的 ...
- python 实现排序算法(三)-选择排序和冒泡排序
#/usr/bin/env python #coding:utf-8 #@auther="livermorium" ''' 选择排序 从数据中选择最小值,排在位置首位 再从剩余未排 ...
- SurFS:共享式和分布式集群各取所长
http://www.ccidnet.com/2016/0811/10168835.shtml 一个集群系统可以做成三层定义,也就是后端存储访问层.沟通协作层.前端数据访问层,如果愣是要给每个层起个洋 ...
- Echarts动态加载饼状图实例(二)
一.引入echarts.js文件(下载页:http://echarts.baidu.com/download.html) 二.HTML代码: <div class="ui-contai ...
- 安装部署elasticsearch
ELK下载:https://www.elastic.co/downloads/ Beats:负责收集系统数据,可以直接发送到es中,也可以通过logstash中转 logstash:收集日志,为bea ...
- day9笔记整理,记忆
函数的使用:一 定义函数的三种形式 1.1 无参函数 1.2 有参函数 1.3 空函数二 调用函数的三种形式 2.1 语句形式 def foo(): print('from foo') ...
- 简单说明一下Token ,Cookie,Session
在Web应用中,HTTP请求是无状态的.即:用户第一次发起请求,与服务器建立连接并登录成功后,为了避免每次打开一个页面都需要登录一下,就出现了cookie,Session. Cookie Cookie ...