ServletContext
来源:互联网 发布:淘宝衣服能买吗 编辑:程序博客网 时间:2024/06/03 15:59
本文主要从以下四个方面来讲解ServletContext。
- 如何获取ServletContext
- getContextPath()获取当前web应用的路径
- getInitParameter()获取web应用的初始化参数
- 域对象的有关方法
- 转发
- 获取web应用中的资源文件
如何获取ServletContext
public class ContextDemo extends HttpServlet public abstract class HttpServlet extends GenericServlet public abstract class GenericServlet implements Servlet, ServletConfig, java.io.Serializable{ public ServletContext getServletContext() { ServletConfig sc = getServletConfig(); if (sc == null) { throw new IllegalStateException( lStrings.getString("err.servlet_config_not_initialized")); } return sc.getServletContext(); }
我们自己编写的Servlet继承于HttpServlet,HttpServlet又继承于GenericServlet,通过GenericServlet的源码可以看到提供了getServletContext的方法,所以我们在自己编写的Servlet方法中获取ServletContext的正确姿势如下:
this.getServletContext();
getContextPath()获取当前web应用的路径
当我们使用tomcat发布了我们的项目,我们通过什么方式来获取呢?
//得到web应用路径String contextPath=this.getServletContext().getContextPath();//request.getContextPath();
但是我们得到web应用的路径有什么用呢?当我们转发或者重定向到项目的另一个Servlet或者页面的时候,我们就必须要使用web应用路径了。如果我们在页面中硬编码,当项目名称改变的时候,你是不是要修改更多的代码呢?因此我们就可以使用这个方法了!
response.sendRedirect(this.getServletContext().getContextPath()+"/index.html");
getInitParameter()
当我们想要为整个web应用配置一些初始值或者项目启动的时候加载一些资源怎么处理呢?比如Spring框架中加载配置文件。
我们在web.xml中进行配置。
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/spring/applicationContext-*.xml</param-value></context-param>
这样我们就可以获取:
String contextConfigLocation=this.getServletContext().getInitParameter("contextConfigLocation");
域对象
域对象的作用就是用于保存数据,获取数据。ServletContext域对象为了不同的Servlet之间可以共享数据。通过以下方法我们可以保存数据,获取数据,删除数据。
- setAttribute
- getAttribute
- removeAttribute
ServletContext域对象的作用域为整个web应用中有效。所有的域对象如下:
- HttpServletRequet
- ServletContext
- HttpSession
- PageContext
转发
this.getServletContext().getRequestDispatcher("/index.jsp").forward(req, resp);// request.getRequestDispatcher("/index.jsp").forward(request, response);
关于转发和重定向的区别请参考()
获取web应用中的资源文件
当我们要确定一个资源的路径时候,首先我们要确定是目标资源是给谁使用的。
- 给服务器使用的: / 表示当前web应用的根目录。
- 给浏览器使用的:表示webapps的根目录。
/** * 转发:面向服务器的。所以直接在web应用下 */ request.getRequestDispatcher("/index.jsp").forward(request, response); /** * 重定向:面向浏览器的。 */ response.sendRedirect("/WebLearning/index.jsp"); /** * 读取资源
当我们读取资源的时候 .(点)代表什么呢? 表示相对路径中的当前路径,相对于Java命令行运行的目录。在Java项目中,点代表Java项目的根目录下开始。在web项目项目中,点代表在tomcat/bin目录下开始,所以不能使用这种相对路径。
/** * 读取资源:src下会自动发布到WEB/INF/classes */// File file =new File("./src/db.properties"); 错误写法。String path=this.getServletContext().getRealPath("/WEB-INF/classes/db.properties");System.out.println(path);InputStream in=this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
- ServletContext
- ServletContext
- servletcontext
- servletContext
- servletContext
- servletcontext
- ServletContext
- ServletContext
- servletContext
- ServletContext
- ServletContext
- servletContext
- ServletContext
- servletContext
- ServletContext
- servletContext
- ServletContext
- servletcontext
- 更新Gradle
- QT之 scene graph 的局部刷新尝试(一)
- scrapy中解决pipeliine不能过滤中文的问题
- HTML5 canvas图形绘制基础(矩形,线条,渐变色,圆形,图片,多边形)
- jquery实现input点击输入框弹出日期
- ServletContext
- C#中virtual和abstract的区别
- 线段树
- Android之用AccessibilityService实现红包插件
- Toad学习总结
- C# SQL 整数变量
- 搭建自己的Blog (Github+Hexo)
- [006-Makefile-笔记] Makefile的条件判断
- 浅谈DOM解析