Servlet之ServletContext获取web上下文路径、全局参数、和Attribute(域)
来源:互联网 发布:sql多张表合并 union 编辑:程序博客网 时间:2024/06/10 10:20
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("删除成功");}}
0 0
- Servlet之ServletContext获取web上下文路径、全局参数、和Attribute(域)
- servlet都有一个servletConfig对象;四个config对象可以调用的方法;ServletContext对象之获取web项目信息;设置全局初始化参数的配置
- Servlet,Listener和Filter 获取ServletContext (application上下文环境)
- Servlet--ServletContext(三)获取web.xml配置参数
- Web前端开发之定义全局的上下文路径
- (18)servletContext应用:获取web应用的初始化参数、实现servlet转发、利用servletContext对象读取资源文件
- Servlet之ServletContext获取获取web项目的资源文件
- servlet struts 取上下文路径 和 WEB-INF路径
- Servlet(19)web.xml 配置之Servlet参数配置和读取资源文件,获取文件全路径
- Servlet上下文ServletContext接口
- web项目的servlet和filter中获取spring上下文
- ServletContext 获取上下文对象
- Servlet路径、ServletConfig、ServletContext和乱码问题(转载)
- Servlet之ServletConfig和ServletContext
- Servlet初始化参数(ServletConfig)与Servlet上下文初始化参数(ServletContext)区别
- Servlet获取配置信息(ServletConfig)和ServletContext
- servlet初始化参数和上下文初始化参数
- Servlet(4)ServletContext接口 (获取web.xml中所有Servlet共有的信息)
- 蓝桥杯 地宫取宝(DFS+记忆化搜索)
- 零基础二十分钟搭建SAE微信个人号后台
- 2017.春节.深圳
- Leetcode-447. Number of Boomerangs
- codevs2181 田忌赛马
- Servlet之ServletContext获取web上下文路径、全局参数、和Attribute(域)
- 学生管理系统
- 网页编程自学历程一:编写一个静态网页框架
- [二分 后缀自动机 单调队列优化DP] BZOJ 2806 [Ctsc2012]Cheat
- BZOJ 1069: [SCOI2007]最大土地面积 凸包,旋转卡壳
- 佛教
- lua的table类型
- hdu1846-Brave Game(BashGame入门)
- HTML入门