ServletContext对象惯用操作
WEB容器在启动时,它会为每个WEB应用程序都创建一个对应的ServletContext对象,它代表当前web应用。ServletConfig对象中维护了ServletContext对象的引用,开发人员在编写servlet时,可以通过ServletConfig.getServletContext方法获得ServletContext对象。由于一个WEB应用中的所有Servlet共享同一个ServletContext对象,因此Servlet对象之间可以通过ServletContext对象来实现通讯。ServletContext对象通常也被称之为context域对象。
操作一:获取全局配置参数
有两种方式获取ServletContext对象,第一种是通过ServletConfig对象获取
super.getServletConfig().getServletContext();
第二种方式是直接获取,GenericServlet中提供了这个方法,实际上也是调用哪个了ServletConfig
@Override public ServletContext getServletContext() { return getServletConfig().getServletContext(); }
通过ServletContext获取全局配置参数
public class ParamContext extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String param = getServletContext().getInitParameter("param"); System.out.println(param); } }
web.xml中的配置,这种全局参数所有Servlet都可以访问到
<context-param> <param-name>param</param-name> <param-value>I'm param</param-value> </context-param>
操作二:获取文件的MIME类型
Serlvet会读取Tomcat/conf/web.xml中所配置MIME类型,文件下载时会用到
public class MIMEContext extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String doc = super.getServletContext().getMimeType("file.doc"); String jpg = super.getServletContext().getMimeType("file.jpg"); String txt = super.getServletContext().getMimeType("file.txt"); System.out.println(doc + "\t" + jpg + "\t" + txt); } }
操作三:日志信息
Serlvet容器启动时会在console中输出大量信息,通过SerlvetContext也可以添加自己的日志信息
public class ParamContext extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { super.getServletConfig().getServletContext(); String param = getServletContext().getInitParameter("param"); System.out.println(param); }
操作四:Servlet间共享数据
SerlvetContext对象本生代表着属性范围,可以通过Servlet对象调用Attribute设置参数的方式,在多个Servlet间共享数据
编写一个Servlet统计访问该Servlet的次数
public class VisitContext extends HttpServlet { @Override public void init() throws ServletException { super.getServletContext().setAttribute("visit", 0); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext context = super.getServletContext(); Integer count = (Integer) context.getAttribute("visit"); count++; context.setAttribute("visit", count); } }
在另外一个Servlet中获取,该访问次数
public class ShareContext extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Integer count = (Integer) super.getServletContext().getAttribute("visit"); System.out.println(count); } }
操作五:Web工程中读取资源文件
在Servlet中读取文件必须使用磁盘的绝对路径,在src下建议个文件,使用Serlvet获取该路径
public class ResourceContext extends HttpServlet { protected void doGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp) throws javax.servlet.ServletException, java.io.IOException { String file = "/flie1"; String realPath = super.getServletContext().getRealPath(file); System.out.println(realPath); }; }
如果只是获取classpath下的文件,可以使用类加载器,Hibernate框架加载文件就是用这种方法,和ServletContext完全解耦,getReal()、getFile()一样
public class ResourceContext extends HttpServlet { protected void doGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp) throws javax.servlet.ServletException, java.io.IOException { String file = "/WEB-INF/classes/file1.txt";//src下 System.out.println(ResourceContext.class.getResource(file)); System.out.println(ResourceContext.class.getResource(file).getPath()); System.out.println(ResourceContext.class.getResource(file).getFile()); } }
工程下的文件无法获取,WebRoot以外的文件不会发布到Tomcat中
getServletContext().getRealPath("/2.txt");//WebRoot下 getServletContext().getRealPath("/WEB-INF/3.txt");//WEB-INF下 getServletContext().getRealPath("/WEB-INF/classes/4.txt");//src下