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]通过IP地址加入游戏
- [UE4]移除UI(User Widget)并销毁
1.移除UI,使用“Remove from Parent”方法 2.最后一步给UI变量赋值的时候,如果保持默认选择“Select Assets”,则会把UI变量销毁(赋值为null).
- Redis登陆服务器和批量删除指定的key
ps -ef |grep redis cd /opt/app/redis/bin ./redis-cli -h 192.168.0.67 -p 7001 -a 'hub2c!Redis'./redis ...
- vue 路由 以及默认路由跳转
https://router.vuejs.org/ vue路由配置: 1.安装 npm install vue-router --save / cnpm install vue-router --sa ...
- springmvc使用步骤
1.导包 2.在web.xml文件中配置前端控制器dispatchServlet 3.创建springmvc配置文件springmvc.xml 4.编写controller 5.在springmvc中 ...
- 挂载本地iso镜像
挂载本地iso镜像 [root@linux-node1 ~]# mkdir -p /disk/iso [root@linux-node1 ~]# cd /disk/iso/ [root@linux-n ...
- python中变量的缓存机制
同一文件中, 变量的缓存机制 (在此范围内的相同值内存地址一样) Number: int: -5 ~ 正无穷 float: 非负数 bool: ...
- CRM N:1 关系或者字段无法删除
点开详细信息查看那些实体引用了该组件.查看内容如下: 1 查看窗体上有无该字段; 2 查看视图中有无该字段; 3 查看试图的 筛选条件; 4 查看试图的 查找列; 5 发布之后再试试.
- sql-datediff
SQL中DateDiff的用法 DATEDIFF返回跨两个指定日期的日期和时间边界数. 语法DATEDIFF ( datepart , startdate , enddate ) 参数datepart ...
- 网络基础和python
·五层协议 物理层,数据链路层,网络层,传输层,应用层 ·用户上网流程 1.本机获取 2.打开浏览器,,输入网址. 3.dns协议(基于udp协议) 4.HTTP部分的内容 5 TCP协议 6 IP协 ...