什么是ServletContext?
根据字面意思即Servlet上下文
服务器会为每一个工程创建一个对象,这个对象就是ServletContext对象,这个对象是
全局唯一的,工程内部的所有servlet都共享这个对象,所有应用程序共享对象
下面用一段简单的代码来证明一下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet ( "/ServletContext2" ) public class ServletContext2 extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext servletContext = this .getServletContext(); System.out.println( "ServletContext2的地址:" ); System.out.println(servletContext); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this .doGet(req, resp); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet ( "/ServletContext" ) public class ServletContext extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this .doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { javax.servlet.ServletContext servletContext = this .getServletContext(); System.out.println( "ServletContext2的地址:" ); System.out.println(servletContext); RequestDispatcher servletContext2 = req.getRequestDispatcher( "ServletContext2" ); servletContext2.forward(req,resp); } } |
可以看到二者的地址一模一样
ServletContext对象的生命周期
诞生:web部署到服务器上,启动服务器,此时上下文环境对象创建,只要tomcat服务器不关闭,上下文环境对象就一直
存在
销毁:关闭服务器或者是从web服务器上卸载该程序的时候,该对象销毁
怎么创建ServletContext对象?
方法一:
javax.servlet.ServletContext servletContext = this.getServletContext();
方法二:
javax.servlet.ServletContext servletContext1 = req.getServletContext();
方法三:
javax.servlet.ServletContext servletContext3 = this.getServletConfig().getServletContext();
ServletContext对象的作用与相关方法
1.是一个域对象*
什么是域:域其实就是一个map结构,及存入一个键值对,来存储获取数据
作用范围:本工程内部
域对象可以被本工程内所有的servlet对象共享,因为一个工程只有一个servletcontext对象
方法介绍:
存储数据: void setAttribute(String var1, Object var2);
移除数据: void removeAttribute(String var1);
获取数据: Object getAttribute(String var1);
1.由于取出的是一个Object类型,需要强转
2.如果指定的属性名不存在,则返回null
2.可以获取文件的真实路径
真实路径:即部署在服务器是的路径
String getRealPath() 获取文件的真实路径
不同目录下访问资源的方式不同
1
2
3
4
5
6
7
8
9
|
//src下的资源访问 String realPath = servletContext.getRealPath( "/WEB-INF/classes/a.text" ); System.out.println(realPath); //WEB-INF下的资源访问 String realPath1 = servletContext.getRealPath( "/WEB-INF/b.text" ); System.out.println(realPath1); //web目录下的资源访问 String realPath2 = servletContext.getRealPath( "/c.text" ); System.out.println(realPath2); |
代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet ( "/ServletContext" ) public class ServletContext extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this .doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { javax.servlet.ServletContext servletContext = this .getServletContext(); //src下的资源访问 String realPath = servletContext.getRealPath( "/WEB-INF/classes/a.text" ); System.out.println(realPath); //WEB-INF下的资源访问 String realPath1 = servletContext.getRealPath( "/WEB-INF/b.text" ); System.out.println(realPath1); //web目录下的资源访问 String realPath2 = servletContext.getRealPath( "/c.text" ); System.out.println(realPath2); } } |
结果
3.获得MIME类型
s什么是MIME类型,是在互联网通信过程中定义等等一种文件数据类型
格式:大类型/小类型 image/jpeg
获取: String getMimeType(String file)
代码如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet ( "/ServletContext" ) public class ServletContext extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this .doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { javax.servlet.ServletContext servletContext = this .getServletContext(); String filename= "a.jpg" ; String mimeType = servletContext.getMimeType(filename); System.out.println(mimeType); } } |
结果
以上就是ServletContext对象的一些基础知识,如有错误还请各位批评指正,喜欢我的文章可以点赞呀,也可以关注我,我会经常跟新文章
以上就是深入了解Java ServletContext的详细内容,更多关于Java ServletContext的资料请关注服务器之家其它相关文章!
原文链接:https://www.cnblogs.com/pjhaymy/p/13424324.html