- 1、介绍 ServletContext
- 2、用法
- (1)getInitParameterNames 和 getInitParameter
- (2)getMajorVersion 和 getMinorVersion
- (3)getRealPath 和 getContextPath
- (4)getRequestDispatcher
- (5)getMimeType 和 getResourceStream
- (6)setAttribute 和 removeAttribute 和 getAttribute
ServletContext官方叫servlet上下文。服务器会为每一个工程创建一个对象,这个对象就是ServletContext对象。这个对象全局唯一,而且工程内部的所有servlet都共享这个对象。所以叫全局应用程序共享对象。
2、用法以下所有post方法都是都是注册于同一个servlet
(1)getInitParameterNames 和 getInitParametertest com.workhah.servlet.ServletContextTest test /test
- 获取所有参数名称列表
java.util.Enumeration getInitParameterNames() - 根据指定的参数名获取参数值
java.lang.String getInitParameter(String name)
web.xml
t1 1 t2 2
servlet
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 获取servletcontext
ServletContext servletContext = getServletContext();
// 先获取所有全局配置参数名称
Enumeration names = servletContext.getInitParameterNames();
// 遍历迭代器
while(names.hasMoreElements()){
// 获取每个元素的参数名字
String parameName = names.nextElement();
// 根据参数名字获取参数值
String paramevalue = servletContext.getInitParameter(parameName);
// 打印
System.out.println(parameName+"="+paramevalue);
}
}
输出结果为:
(2)getMajorVersion 和 getMinorVersiont1=1
t2=2
- 返回此 servlet 容器支持的 Java Servlet API 的主要版本。
int getMajorVersion() - 返回此 servlet 容器支持的 Servlet API 最小版本号。
int getMinorVersion()
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 获取servletcontext
ServletContext servletContext = getServletContext();
System.out.println(servletContext.getMajorVersion());
System.out.println(servletContext.getMinorVersion());
}
(3)getRealPath 和 getContextPath
-
获取当前工程名字
String getRealPath(String path) -
根据相对路径获取服务器上资源的绝对路径
String getContextPath()
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 获取servletcontext
ServletContext servletContext = getServletContext();
// 页面显示内容
resp.getOutputStream().write(("工程名字:"+ servletContext.getContextPath() + "n"
+ servletContext.getRealPath("/file/computer.png")
).getBytes());
}
结果为:
RequestDispatcher getRequestDispatcher(String name)
返回具有指定名字或路径的servlet或JSP的RequestDispatcher。如果不能创建RequestDispatch,返回null。如果指定路径,必须以 “/” 开头,并且是相对于servlet上下文的顶部。这就是我们常说的请求转发
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 获取servletcontext
ServletContext servletContext = getServletContext();
// 请求转发到index.jsp(url不变)
servletContext.getRequestDispatcher("/index.jsp").forward(req, resp);
}
结果:
- 返回指定文件名的MIME类型。典型情况是基于文件扩展名,而不是文件本身的内容(它可以不必存在)。如果MIME类型未知,可以返回null。 返回值有 text/plain、text/html、image/jpeg、image/png、application/json等等。
String getMimeType(String fileName)
了解更多MIME可以参考这里 - 默认从web根目录下取资源,并以字节流的形式返回
InputStream getResourceAsStream(String path)
具体实例点击这里
(6)setAttribute 和 removeAttribute 和 getAttribute- 增加一个指定名称和值的新属性,或者把一个现有的属性设定为指定的值。
void setAttribute(String s,Object o) - 获取指定名称的属性的值
Object getAttribute(String s) - 移除指定名称的属性
void removeAttribute(String s)
servletContext的setAttribute、getAttribute、removeAttribute和request、session的类似,只是生命周期不同,它在服务器关闭时周期才结束。其实servletContext和application是一样的,就相当于一个类创建了两个不同名称的变量。两者的区别就是application用在jsp中,servletContext用在servlet中。



